1

I have a gridview which has a Checkbox Template Field, I am using C# code and basically when the user checks all the Checkboxs in the datagrid view I want to add these values into an SQL database with a datatype field Bit. Here is my code so far;

protected void SaveRegisterButton_Click(object sender, EventArgs e)
{
            SqlConnection connection;
            SqlCommand command;
            int numRowsAdded;
            int id;
            Boolean AttendanceChecked;
    foreach (GridViewRow row in GridView2.Rows)
    {
        if (((CheckBox)row.FindControl("AttendanceCheckBox")).Checked)
        {


            try
            {
                // Connecting to the database using the connection string in the web.config file
                connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["RegisterConnectionString"].ConnectionString);

                // Create an INSERT Sql statement
                // To prevent an Sql injection attack, we add parameters with names starting with an @ symbol
                command = new SqlCommand("INSERT INTO Attendance(Present, StudentID, LessonID) VALUES(@AttendanceChecked, @StudentID, @LessonID)",


                // Replace the parameters with the actual values read from the form
                //command.Parameters.AddWithValue("AttendanceChecked", AttendanceCheckBox);

How do I pass the value of the checkbox control into the sql database field with data type Bit? Any help is appreciated, thanks in advance!

1 Answer 1

1

First of all the below code will throw a null reference exception if it does not find the checkbox (e.g. if it is a header/footer).

if (((CheckBox)row.FindControl("AttendanceCheckBox")).Checked)

The only thing you can store in a bit field is 1 or 0 whereas the "value" of a checkbox is text. Do you actually want to chose which field to save to based on the value then the value to save based on whether the checkbox is checked? Please elaborate.

Further to your update try the below code:

foreach (GridViewRow row in GridView2.Rows)
    {
    CheckBox AttendanceCheckBox = row.FindControl("AttendanceCheckBox") as CheckBox;
    if (AttendanceCheckBox != null)
    {
       try
       {                
           connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["RegisterConnectionString"].ConnectionString);
           SqlCommand command = new SqlCommand("INSERT INTO Attendance(Present, StudentID, LessonID) VALUES(@AttendanceChecked, @StudentID, @LessonID)");
           command.Parameters.AddWithValue("@AttendanceCheck",AttendanceCheckBox.Checked);

You will also need to add the values for @StudentID and @LessonID using the same command.Parameters.AddWithValue method.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, I mean that when the checkbox is checked I want to save 1 in the data type field Bit, and when it is not checked I want to save 0 in the data type field Bit. Thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.