locked
NullReferenceException was unhandled by user code ERROR RRS feed

  • Question

  • User1734666507 posted

    Hi, 

    I want to display an error message if the user did not make a selection from the radiobuttonlist. Now i am getting an error.

    If the user did not select any selection, the error "Label1" will be shown, else it will store the selection into the database.

    This is the code:

    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            //Inserting user selection into database
            string dbName = Session["SessName"].ToString();
            string dbClass = Session["SessClass"].ToString();
            string dbLogID = Session["SessLogID"].ToString();
    
            string ans = rblS2a.SelectedItem.Text;            //This line was highlighted as the error
            string ans2 = rblS2a2.SelectedItem.Text;
    
            SqlConnection connUpdate = new SqlConnection();
            connUpdate.ConnectionString = ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString;
            connUpdate.Open();
           
            bool flag = false;
            foreach (ListItem item in rblS2a.Items)
            {//     flag = true;
                if (item.Selected)
                {
                    SqlCommand cmdchk = new SqlCommand("SELECT COUNT(*) FROM StdAnswerTbl WHERE LoginID = '" + dbLogID + "'", connUpdate);
                    int chk = Convert.ToInt32(cmdchk.ExecuteScalar());
                    if (chk == 0)
                    {
                        cmdchk.CommandText = "INSERT INTO StdAnswerTbl(LoginID, Stn2a1, Stn2a2) VALUES ('" + dbLogID + "', '" + ans + "', '" + ans2 + "')";
                        cmdchk.CommandType = CommandType.Text;
                        cmdchk.Connection = connUpdate;
                    }
                    else
                    {
                        cmdchk.CommandText = "UPDATE StdAnswerTbl SET Stn2a1 = '" + ans + "', Stn2a2 = '" + ans2 + "' WHERE LoginID = '" + dbLogID + "'";
                        cmdchk.CommandType = CommandType.Text;
                        cmdchk.Connection = connUpdate;
                    }
    
                    int result = cmdchk.ExecuteNonQuery();
                    connUpdate.Close();
                    ImageButton2.Enabled = true;
                    Response.Redirect("Activity2b.aspx");
                }
            }
            if (flag == false)
            {
                Label1.Visible = true;
            }
        }

    This is the error message:

    NullReferenceException was unhandled by user code

    Object reference not set to an instance of an object.

    Any help is greatly appreciated! Thank You!

    Friday, September 30, 2011 3:07 AM

Answers

  • User-68639941 posted

    if you have not selected any item in a radiobuttonlit then the below line will give error, to avoid you can use if st.

    rblS2a.SelectedItem

    String ans="";

    if(rblS2a.SelectedItem != null)

     ans= rblS2a.SelectedItem.Text;

    else

    Label1.Text ="Radio button should be selected";

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 30, 2011 3:14 AM

All replies

  • User-68639941 posted

    if you have not selected any item in a radiobuttonlit then the below line will give error, to avoid you can use if st.

    rblS2a.SelectedItem

    String ans="";

    if(rblS2a.SelectedItem != null)

     ans= rblS2a.SelectedItem.Text;

    else

    Label1.Text ="Radio button should be selected";

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 30, 2011 3:14 AM
  • User-126497635 posted

    Try this:-


    <asp:RadioButtonList ID="rblS2a" runat="server" /> <asp:CustomValidator ValidationGroup="Validation" Display="Dynamic" Text="Select one" runat="server" OnServerValidate="Validator_ServerValidate" /> <asp:ImageButton ValidationGroup="Validation" OnClick="ImageButton2_Click" runat="server" />


    protected void Validator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = (from ListItem item in rblS2a.Items
                            where item.Selected
                            select item).Any();
    
        }
    
        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            if (Page.IsValid)
            {
                //Inserting user selection into database
                string dbName = Session["SessName"].ToString();
                string dbClass = Session["SessClass"].ToString();
                string dbLogID = Session["SessLogID"].ToString();
    
                string ans = rblS2a.SelectedItem.Text;            //This line was highlighted as the error
                string ans2 = rblS2a2.SelectedItem.Text;
    
                SqlConnection connUpdate = new SqlConnection();
                connUpdate.ConnectionString = ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString;
                connUpdate.Open();
    
    
                foreach (ListItem item in rblS2a.Items)
                {//     flag = true;
                    if (item.Selected)
                    {
                        SqlCommand cmdchk = new SqlCommand("SELECT COUNT(*) FROM StdAnswerTbl WHERE LoginID = '" + dbLogID + "'", connUpdate);
                        int chk = Convert.ToInt32(cmdchk.ExecuteScalar());
                        if (chk == 0)
                        {
                            cmdchk.CommandText = "INSERT INTO StdAnswerTbl(LoginID, Stn2a1, Stn2a2) VALUES ('" + dbLogID + "', '" + ans + "', '" + ans2 + "')";
                            cmdchk.CommandType = CommandType.Text;
                            cmdchk.Connection = connUpdate;
                        }
                        else
                        {
                            cmdchk.CommandText = "UPDATE StdAnswerTbl SET Stn2a1 = '" + ans + "', Stn2a2 = '" + ans2 + "' WHERE LoginID = '" + dbLogID + "'";
                            cmdchk.CommandType = CommandType.Text;
                            cmdchk.Connection = connUpdate;
                        }
    
                        int result = cmdchk.ExecuteNonQuery();
                        connUpdate.Close();
                        ImageButton2.Enabled = true;
                        Response.Redirect("Activity2b.aspx");
                    }
                }
            }
        }
    Friday, September 30, 2011 3:20 AM
  • User1734666507 posted

    where do i place this if else? must i delete my current codes?

    Friday, September 30, 2011 3:27 AM
  • User710115794 posted

    just check for radioubutton's selected index first before assigning the selected value to a string. if nothing is selected then radiobutton.selectedindex is equal to -1.

    Friday, September 30, 2011 3:27 AM
  • User710115794 posted

    just put the if else blovk here

    bool flag=false;

    if(rblS2a.selectindex!=-1)

    {

    flag=true;

       string ans = rblS2a.SelectedItem.Text;            //This line was highlighted as the error 
           
    string ans2 = rblS2a2.SelectedItem.Text; 
     }

    Friday, September 30, 2011 3:30 AM
  • User1734666507 posted

    THANKS EVERYONE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    Friday, September 30, 2011 4:01 AM