我有一个DataList,它显示了一个RadioButtonList里面它包含从5到5的5个选择,但是当我尝试获取选定的RadioButtonList的值,它会引发空异常,这是我的代码: 错误@string choice = RadioButtonList1.SelectedItem.Value;
RadioButtonList值的空引用异常
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (DataListItem item in DataList2.Items)
{
RadioButtonList RadioButtonList1 = (RadioButtonList)DataList2.FindControl("RadioButtonList1");
string choice = RadioButtonList1.SelectedItem.Value;
Label post_IDLabel = (Label)item.FindControl("post_IDLabel");
int post_ID = Convert.ToInt32(post_IDLabel.Text);
int value = Convert.ToInt32(choice.ToString());
string connStr = ConfigurationManager.ConnectionStrings["MyDbConn"].ToString();
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand("rate", conn);
cmd.CommandType = CommandType.StoredProcedure;
string email = Session["email"].ToString();
int course_ID = Convert.ToInt32(Request.QueryString["courseID"]);
cmd.Parameters.Add(new SqlParameter("@course_ID", course_ID));
cmd.Parameters.Add(new SqlParameter("@postID", post_ID));
cmd.Parameters.Add(new SqlParameter("@myemail", email));
cmd.Parameters.Add(new SqlParameter("@rate", value));
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Response.Write(choice);
}
DataList2.DataBind();
}
这就是错误:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
您可以显示ASPX代码? – 2012-01-01 05:56:54