[英]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.