Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# RadioButtonList值的空引用异常_C#_Asp.net_Datalist_Radiobuttonlist - Fatal编程技术网

C# RadioButtonList值的空引用异常

C# RadioButtonList值的空引用异常,c#,asp.net,datalist,radiobuttonlist,C#,Asp.net,Datalist,Radiobuttonlist,我有一个数据列表,其中显示了一些帖子,里面有一个RadioButtonList,其中包含从1到5的5个选项,但是当我尝试获取RadioButtonList的选定值时,它抛出Null异常,这是我的代码: 错误@string choice=RadioButtonList1.SelectedItem.Value protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e) {

我有一个数据列表,其中显示了一些帖子,里面有一个RadioButtonList,其中包含从1到5的5个选项,但是当我尝试获取RadioButtonList的选定值时,它抛出Null异常,这是我的代码: 错误@
string choice=RadioButtonList1.SelectedItem.Value

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.

在我看来你找不到控制按钮列表1


我怀疑你的问题是那些没有选择值的RadioButtonList。在这种情况下,selecteditem将为null,但您没有为此进行测试

这里的重写还修复了未处理的连接和命令以及过度打开和关闭连接的潜在问题:

    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string connStr = ConfigurationManager.ConnectionStrings["MyDbConn"].ToString();
        using (SqlConnection conn = new SqlConnection(connStr))
        {
            using (SqlCommand cmd = new SqlCommand("rate", conn))
            {
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.Add("@course_ID", System.Data.SqlDbType.Int);
                cmd.Parameters.Add("@postID", System.Data.SqlDbType.Int);
                cmd.Parameters.Add("@myemail", System.Data.SqlDbType.Int);
                cmd.Parameters.Add("@rate", System.Data.SqlDbType.VarChar);
                conn.Open();

                foreach (DataListItem item in DataList2.Items)
                {
                    RadioButtonList RadioButtonList1 = (RadioButtonList)DataList2.FindControl("RadioButtonList1");
                    if (RadioButtonList1.SelectedItem != null)
                    {
                        string choice = RadioButtonList1.SelectedItem.Value;
                        Label post_IDLabel = (Label)item.FindControl("post_IDLabel");

                        cmd.Parameters["@course_ID"].Value = Convert.ToInt32(Request.QueryString["courseID"]);
                        cmd.Parameters["@postID"].Value = Convert.ToInt32(post_IDLabel.Text);
                        cmd.Parameters["@myemail"].Value = Session["email"] as string;
                        cmd.Parameters["@rate"].Value = Convert.ToInt32(RadioButtonList1.SelectedItem.Value);

                        cmd.ExecuteNonQuery();
                        Response.Write(choice);
                    }
                }
            }
        }
        DataList2.DataBind();
    }
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string connStr = ConfigurationManager.ConnectionStrings["MyDbConn"].ToString();
        using (SqlConnection conn = new SqlConnection(connStr))
        {
            using (SqlCommand cmd = new SqlCommand("rate", conn))
            {
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.Add("@course_ID", System.Data.SqlDbType.Int);
                cmd.Parameters.Add("@postID", System.Data.SqlDbType.Int);
                cmd.Parameters.Add("@myemail", System.Data.SqlDbType.Int);
                cmd.Parameters.Add("@rate", System.Data.SqlDbType.VarChar);
                conn.Open();

                foreach (DataListItem item in DataList2.Items)
                {
                    RadioButtonList RadioButtonList1 = (RadioButtonList)DataList2.FindControl("RadioButtonList1");
                    if (RadioButtonList1.SelectedItem != null)
                    {
                        string choice = RadioButtonList1.SelectedItem.Value;
                        Label post_IDLabel = (Label)item.FindControl("post_IDLabel");

                        cmd.Parameters["@course_ID"].Value = Convert.ToInt32(Request.QueryString["courseID"]);
                        cmd.Parameters["@postID"].Value = Convert.ToInt32(post_IDLabel.Text);
                        cmd.Parameters["@myemail"].Value = Session["email"] as string;
                        cmd.Parameters["@rate"].Value = Convert.ToInt32(RadioButtonList1.SelectedItem.Value);

                        cmd.ExecuteNonQuery();
                        Response.Write(choice);
                    }
                }
            }
        }
        DataList2.DataBind();
    }