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;
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
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With