我有一個GridView的複選框模板字段,我使用C#代碼,基本上當用戶檢查DataGrid視圖中的所有複選框我想根據複選框被選中或不選中,將1或0添加到具有數據類型字段位的SQL數據庫中。這是我的代碼到目前爲止;插入1或0在基於GridView中選中的複選框的sqlDatabase
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;
我正在努力實現我的項目的這一部分,任何幫助將不勝感激!在此先感謝...
究竟是什麼,你有問題?哪一部分? – Oded 2011-04-21 13:09:09
向數據庫的位域添加'true'(1)或'false(0)'。基於複選框被選中,我該如何實現這一點? – user715115 2011-04-21 13:10:35
選中的複選框將對應於「真」。如果選中,則向數據庫插入1,否則爲0.我仍然不知道您遇到問題的位置。 – Oded 2011-04-21 13:12:46