RadioButtonList 值的空引用异常

发布于 2024-12-24 19:30:44 字数 2097 浏览 1 评论 0原文

我有一个 DataList,它显示一些带有 RadioButtonList 的帖子,其中包含从 1 到 5 的 5 个选择,但是当我尝试获取 theRadioButtonList 的选定值时,它会抛出 Null 异常,这就是我的代码: error @ 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.

I have a DataList that Displays some Posts with a RadioButtonList inside where it contains 5 choices from 1 to 5, however when i try to get the selected value of theRadioButtonList, it throws Null Exception, thats my code:
error @ 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();
        }

that's the error:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独自唱情﹋歌 2024-12-31 19:30:44

我怀疑您的问题在于那些没有选择值的单选按钮列表。在这种情况下,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();
    }

I suspect your issue is with those radiobuttonlists that do not have a value selected. When this is the case selecteditem will be null, but you are not testing for this.

Here is a rewrite that also fixes potential issues with the connections and commands not being disposed and of excessive opening and closing of the connection:

    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();
    }
听闻余生 2024-12-31 19:30:44

在我看来,您找不到控件 RadioButtonList1

RadioButtonList RadioButtonList1 = (RadioButtonList)DataList2.FindControl("RadioButtonList1"); 

if(RadioButtonList1 == null) throw new Exception("Can't find control RadioButtonList1");

string choice = RadioButtonList1.SelectedItem.Value; 

Looks to me like you're not finding the control RadioButtonList1

RadioButtonList RadioButtonList1 = (RadioButtonList)DataList2.FindControl("RadioButtonList1"); 

if(RadioButtonList1 == null) throw new Exception("Can't find control RadioButtonList1");

string choice = RadioButtonList1.SelectedItem.Value; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文