Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error :Cannot Find Table 0

I am seeing an error regarding cannot find table 0, where the dataset doesn't contain the table data. This is my code to fetch rows from a table:

    DataTable dt = new DataTable();
    SqlConnection sqlconnection;
    sqlconnection = new SqlConnection(@"connectionstring");
    sqlconnection.Open();
    string sql = "Select (supplier_id,name) from supplier_info where supplier_id= '"+supplierid+"'";
    SqlCommand cmd = new SqlCommand(sql, sqlconnection);
    cmd.CommandType = CommandType.Text;
    SqlDataAdapter adpt = new SqlDataAdapter(cmd);
    DataSet dtset = new DataSet();
    adpt.Fill(dt);
    dt = dtset.Tables[0];
    DataRow dr;
    dr = dt.Rows[0];
    sqlconnection.Close();
    return dr;

Business logic code:

  Cust_datalogic dc = new Cust_datalogic(); //datalogic class
  DataTable dt = new DataTable();
  DataRow dr;
  dr=dc.GetSupplierInfo(id);    //method 
  Supplier_BLL bc = new Supplier_BLL();  //business logic class
  bc.Ssup_ID = dr[0].ToString();
  bc.Ssup_name = dr[1].ToString();
  return bc;
like image 219
Bhargav Amin Avatar asked Feb 01 '14 08:02

Bhargav Amin


1 Answers

Your problem area in your code is

adpt.Fill(dt);  //You are filling dataTable 
dt = dtset.Tables[0]; // You are assigning Table[0] which is null or empty 

Change it to

adpt.Fill(dtset); //Fill Dataset
dt = dtset.Tables[0]; //Then assign table to dt

OR

Your can directly use SqlDataAdapter to Fill DataTable thus no need of DataSet.

Just remove DataSet, use

SqlDataAdapter adpt = new SqlDataAdapter(cmd);
adpt.Fill(dt);
DataRow dr = dt.Rows[0];
like image 129
Satpal Avatar answered Oct 10 '22 20:10

Satpal