Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7004659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:13:38+00:00 2026-05-27T21:13:38+00:00

I have a DataList that Displays some Posts with a RadioButtonList inside where it

  • 0

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.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:13:39+00:00Added an answer on May 27, 2026 at 9:13 pm

    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();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a datalist that contains a gridview inside its itemtemplate. on the item-data_bound
I have been given some 'reports' from another piece of software that contains data
I have a DataList control that displays a set of elements. Can anyone point
I have Datalist that is inside an updatepanel and it is in panel in
Kinda hard to explain, but i'll try. I have a datalist that is populated
I have a datalist inside a usercontrol that gets loaded into a page where
I have a datalist that shows information from a SQL table perfectly. Is there
in my web application i have a datalist in that i am binding some
I have a datalist that receives values from a MySQL database. This datalist can
I have a DataList populated from XML and some cells contain only &nbsp ;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.