简体   繁体   中英

Inserting value from Checkbox Template Field into sql database field type Bit

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!

First of all the below code will throw a null reference exception if it does not find the checkbox (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM