我想調試上述錯誤。以下是我的代碼。錯誤:System.NullReferenceException:對象引用未設置爲對象的實例
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();上得到一個錯誤。
我必須實例化什麼?
檢查'如果(GridView1!= NULL)' – Bolu
非常感謝我把後之後出現快速response.No錯誤的if語句,但GridView的不顯示。 –
您的.aspx文件中是否有名爲GridView1的GridView? – sisve