Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
C#
protected void btnsubmit_Click(object sender, EventArgs e)
        {
            string strSelectOptions = string.Empty;


            CheckBox ch = sender as CheckBox;
           
                if (ch.Checked)
                   
                    strSelectOptions += ch.Text + ",";

                if (strSelectOptions != string.Empty)
                {

                    strSelectOptions = strSelectOptions.Remove(strSelectOptions.Length - 1);
                    int pos = strSelectOptions.LastIndexOf(",");
                    if (pos != -1)
                    {
                        strSelectOptions = strSelectOptions.Remove(pos, 1);
                        strSelectOptions = strSelectOptions.Insert(pos, " & ");
                    }
                
                    lblresult.Text = "Selected Options:" + strSelectOptions;
                           }
                else
                {
                    lblresult.Text = "No Option Selected";
                }

            }
Posted
Updated 24-Jul-15 22:59pm
v3
Comments
Afzaal Ahmad Zeeshan 25-Jul-15 4:59am    
On which line? Did you try debugging the application?

1 solution

Look at your code:
C#
protected void btnsubmit_Click(object sender, EventArgs e)
        {
            string strSelectOptions = string.Empty;


            CheckBox ch = sender as CheckBox;

                if (ch.Checked)

hat is a button click event handler, so the chances are that the sender is a button, not a CheckBox.
So this line:
C#
CheckBox ch = sender as CheckBox;
will set ch to null, and any attempt to use a property or method will fail with a null reference exception.

Probably, you need to re-think what you are doing, or use the CheckBox name directly instead of trying to use the sender parameter.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900