我有这个代码隐藏按钮,当按下按钮时,它会在SQL中显示表格。 我似乎不知道为什么它不显示任何表格的问题?
我添加了这行代码来检查BatchID是否超出了sql表
的范围 if (read.Read())
{
GridView1.DataSource = read;
GridView1.DataBind();
}
else
{
lbl_NoBatchID.Text = "BatchID out of range";
}
protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.ToString() =="ER00 - File Header")
{
using (SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["DBcon"]))
{
if (String.IsNullOrEmpty(TextBox_ID.Text.ToString()))
{
lbl_NoBatchID.Text = "Please enter BatchID!";
}
else
{
try
{
lbl_NoBatchID.Text = "";
SqlCommand sqlCommand = new SqlCommand("Select * from tbl_WinApps_FileHeader Where BatchID =" + TextBox_ID.Text.ToString());
sqlCommand.Connection = con;
con.Open();
SqlDataReader read = sqlCommand.ExecuteReader();
if (read.Read())
{
GridView1.DataSource = read;
GridView1.DataBind();
}
else
{
lbl_NoBatchID.Text = "BatchID out of range";
}
}
catch (Exception)
{
}
}
}
}
答案 0 :(得分:1)
确保你做两件事。
1。)在DataReader
之后关闭Databind()
。
2。)为AutoGenerateColumns="True"
设置GridView
。