Avatar of moe57
moe57
 asked on

how to get the selected value of check-box in asp.net

I am trying to get the selected value of the check-box but it is always showing zero in the back-end table even when i select the check-box. The data type for the check-box is bit. I am not sure what am i doing wrong here.
 using (SqlConnection con = new SqlConnection(strConnString))
            {
                using (SqlCommand cmd = new SqlCommand(@"insert into myTable(UserID, Name) values( @UserID, @Name)"))                                                                                            

                {
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.Parameters.AddWithValue("@UserID", tempUser.ToString());

                        cmd.Parameters.AddWithValue("@Name", chckNM.Checked);

                        cmd.Connection = con;
                        con.Open();
                        cmd.ExecuteNonQuery();

                    }

Open in new window

here is the checkbox
<asp:CheckBox ID="chckNM" runat="server" />

Open in new window

ASP.NETC#ASP

Avatar of undefined
Last Comment
moe57

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Peter Hutchison

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
moe57

ASKER
thanks
moe57

ASKER
sorry even though i closed the question but i hope you could help me.  I want to retrieve the data and this is the syntax i am want to use but it is not quite right yet
checkNM.value = dtSETS_row.Field<bool>("Name");

Open in new window

when the page loads, i want the checkbox to show whatever value in the database but the chckNM.value is not working for me..
Peter Hutchison

Change the line to:
checkNM.Checked = dtSETS_row.Field<bool>("Name");
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
moe57

ASKER
thanks