簡體   English   中英

基於GridView中選中的復選框在sqlDatabase的Bit字段中插入1或0

[英]Inserting 1 or 0 in Bit field in sqlDatabase based on Checkbox checked in GridView

我有一個帶有Checkbox Template字段的gridview,我使用的是C#代碼,基本上,當用戶在datagrid視圖中選中所有Checkbox時,我想向一個SQL數據庫中添加1或0的數據類型字段Bit基於復選框是否被檢查。 到目前為止,這是我的代碼;

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", SqlDbType.Bit).Value = AttendanceChecked;
                //command.Parameters.AddWithValue("@AttendanceChecked", CheckBox("Attend
                //com.Parameters.Add("@Approved", SqlDbType.Bit).Value = status;

我正在努力實施項目的這一部分,任何幫助將不勝感激! 提前致謝...

嘗試這個:

command.Parameters["@AttendanceChecked"].Value = AttendanceChecked ? 1 : 0;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM