Error: cannot find table 0

I see an error regarding cannot find table 0 where the dataset does not contain table data. This is my code for extracting 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; 
+6
source share
1 answer

Your problem area in your code

 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

You can directly use the SqlDataAdapter to populate the DataTable, so there is no need for a DataSet.

Just uninstall DataSet, use

 SqlDataAdapter adpt = new SqlDataAdapter(cmd); adpt.Fill(dt); DataRow dr = dt.Rows[0]; 
+4
source

All Articles