错误:找不到表0

时间:2013-04-22 18:47:45

标签: asp.net

 con.Open();
 SqlDataAdapter da4 = new SqlDataAdapter("select * from tblbooking where bookid =(select max(bookid) from tblbooking)", con);
 DataSet ds4 = new DataSet();
 pay = ds4.Tables[0].Rows[0]["costoftickets"].ToString();
 Label4.Text = "Amount to Pay : " + pay + " INR.";
 da4.Fill(ds4);
 DetailsView1.DataSource = ds4;
 DetailsView1.DataBind();
 con.Close();

1 个答案:

答案 0 :(得分:0)

在访问其内容之前,您尚未填充DataSet,因此当然没有数据。

con.Open(); 
SqlDataAdapter da4 = new SqlDataAdapter("select * from tblbooking where bookid =(select max(bookid) from tblbooking)", con); 
DataSet ds4 = new DataSet(); 
da4.Fill(ds4); // this needs to go before accessing the data
pay = ds4.Tables[0].Rows[0]["costoftickets"].ToString(); 
Label4.Text = "Amount to Pay : " + pay + " INR."; 
DetailsView1.DataSource = ds4; 
DetailsView1.DataBind(); 
con.Close();

请注意,如果查询未返回任何数据,您仍可能会收到错误。