【发布时间】:2013-08-02 08:25:48
【问题描述】:
我正在尝试调试上述错误。下面是我的代码。
private SqlConnection SQLConn(string name)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings[name].ConnectionString;
return conn;
}
protected void rb2_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn = SQLConn("Plastics");
try
{
string selectSQL = "SELECT [Description], [Code], [Change] FROM [plastics]";
SqlCommand cmd = new SqlCommand(selectSQL, conn);
conn.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
}
catch (SqlException Exception)
{
// catch exception
Response.Write("An error occured");
}
finally
{
conn.Close();
}
}
GridView1.DataSource = cmd.ExecuteReader(); 出现错误
我必须实例化什么?
【问题讨论】:
-
检查
if (GridView1 != null) -
非常感谢您的快速响应。我输入if语句后没有出现错误,但gridview不显示。
-
您的 .aspx 文件中有一个名为 GridView1 的 GridView 吗?
-
您的“SqlDataReader”是否为空?
-
在调试器中运行代码会发生什么?