插入值从复选框模板字段到SQL数据库字段类型位 [英] Inserting value from Checkbox Template Field into sql database field type Bit

查看:120
本文介绍了插入值从复选框模板字段到SQL数据库字段类型位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有都有一个复选框模板字段一个gridview,我使用C#code,基本上当用户检查所有Checkboxs在数据网格视图我想这些数值与数据类型字段位添加到SQL数据库。
这里是我的code为止;

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);

我如何通过复选框控件的值与数据类型位SQL数据库领域?任何帮助是AP preciated,先谢谢了!

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!

推荐答案

所有低于code将抛出一个空引用异常,如果它没有找到复选框的第一(例如,如果它是一个页眉/页脚)。

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)

您可以在一个位字段存储的唯一事情是1或0,而一个复选框的价值是文字。难道你真的想选择哪个字段保存到基于该值,则该值以节省基于该复选框是否被选中?请详细说明。

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.

另外你更新试试下面code:

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);

您还需要添加@StudentID的值,并使用相同的 command.Parameters.AddWithValue 方法@LessonID。

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

这篇关于插入值从复选框模板字段到SQL数据库字段类型位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆