[英]How to retrieve asp:CheckBox value from SQL db?
I have a table of CheckBoxes that are inserted into a SQL db as 'True' and 'False'. 我有一个CheckBoxes表,它们以“ True”和“ False”的形式插入到SQL数据库中。 However, I would like to retrieve those values again with a load event, but I'm not able to get them.
但是,我想通过load事件再次检索这些值,但无法获取它们。 This is my code:
这是我的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (auditChecklist != null)
{
//for loading audit checklist
getAuditChecklist();
}
}
I tried to retrieve them with the function below, which I thought it might do it: 我尝试使用下面的函数来检索它们,我认为它可以做到这一点:
private void getAuditChecklist()
{
//int i = 0;
SqlCommand cmd = null;
string conn = ConfigurationManager.ConnectionStrings["PSCV1ConnectionString"].ConnectionString;
string queryString = @"SELECT * FROM AUDIT_CHECKLIST " +
"WHERE SITE_ID = @SiteID";
using (SqlConnection connection =
new SqlConnection(conn))
{
SqlCommand command =
new SqlCommand(queryString, connection);
connection.Open();
cmd = new SqlCommand(queryString);
cmd.Connection = connection;
cmd.Parameters.Add(new SqlParameter("@SiteID", //the name of the parameter to map
System.Data.SqlDbType.NVarChar, //SqlDbType value
20, //The width of the parameter
"Site_ID")); //The name of the column source
//Fill the parameter with the value retrieved
//from the text field
cmd.Parameters["@SiteID"].Value = foo.Site_ID;
SqlDataReader reader = cmd.ExecuteReader();
if (CheckBox1.Checked == true)
{
while (reader.Read())
{
cmd.Parameters.Add(new SqlParameter("@Mount", //the name of the parameter to map
System.Data.SqlDbType.NVarChar, //SqlDbType value
20, //The width of the parameter
"Mount")); //The name of the column source
//Fill the parameter with the value retrieved
//from the text field
cmd.Parameters["@Mount"].Value = "True";
}
}
else
{
cmd.Parameters.Add(new SqlParameter("@Mount", //the name of the parameter to map
System.Data.SqlDbType.NVarChar, //SqlDbType value
20, //The width of the parameter
"Mount")); //The name of the column source
//Fill the parameter with the value retrieved
//from the text field
cmd.Parameters["@Mount"].Value = "False";
}
reader.Close();
}
}
Thanks a lot for the help! 非常感谢您的帮助!
If I got your Question properly...The problem is in your datatypes . 如果我正确地收到您的问题 ...问题出在您的数据类型中 。 I think its a bit field.
我认为这有点领域。
sqlCommand.Parameters.Add(new SqlParameter("@Mount", SqlDbType.Bit));
sqlCommmand.Parameters["@Mount"].Value = 1;
You mention that your values are inserted as 'True' and 'False' , yet you are passing 1 and 0 into an nvarchar field. 您提到将值插入为'True'和'False' ,但是您正在将1和0传递到nvarchar字段中。 Is the database field a nvarchar or a bit?
数据库字段是nvarchar还是有点?
If it's a bit field, change you parameter to a bit data type: 如果是位字段,请将您的参数更改为位数据类型:
sqlCommand.Parameters.Add(new SqlParameter("@Mount", SqlDbType.Bit));
sqlCommand.Parameters["@Mount"].Value = 1;
If it's an nvarchar field, you should rethink your schema because you should really be using a bit data type. 如果是nvarchar字段,则应重新考虑架构,因为您实际上应该使用位数据类型。 But, for the sake of the question, you could do that like so:
但是,出于问题的考虑,您可以这样做:
sqlCommand.Parameters.Add(new SqlParameter("@Mount", SqlDbType.NVarChar));
sqlCommand.Parameters["@Mount"].Value = "True";
However, you are adding the @Mount
parameter, but your queryString
makes no reference to a @Mount
parameter. 但是,您要添加
@Mount
参数,但是queryString
没有引用@Mount
参数。 I'm not sure what your intention is with that, but I imagine that is where your issue lies. 我不确定您的意图是什么,但是我想这就是您的问题所在。
I would recommend you start HERE to begin reading data from the database. 我建议您从此处开始以开始从数据库读取数据。
This how I did to read/get checkmark values from the database:
这是我从数据库中读取/获取检查标记值的方法:
private void getAuditChecklist()
{
SqlCommand cmd = null;
string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string queryString = @"SELECT Mount, Braker, Access, Conn_Net, Log_Book, Pictures, Floor, Cb_Lenght, Channel FROM AUDITOR_CHECKLIST " +
"WHERE SITE_ID = @SiteID";
using (SqlConnection connection =
new SqlConnection(conn))
{
SqlCommand command =
new SqlCommand(queryString, connection);
connection.Open();
cmd = new SqlCommand(queryString);
cmd.Connection = connection;
cmd.Parameters.Add(new SqlParameter("@SiteID", //the name of the parameter to map
System.Data.SqlDbType.NVarChar, //SqlDbType value
20, //The width of the parameter
"SITE_ID")); //The name of the column source
//Fill the parameter with the value retrieved
//from the text field
cmd.Parameters["@SiteID"].Value = foo.Site_ID;
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
CheckBox1.Checked = (reader.GetBoolean(reader.GetOrdinal("Mount")));
CheckBox2.Checked = (reader.GetBoolean(reader.GetOrdinal("Braker")));
CheckBox3.Checked = (reader.GetBoolean(reader.GetOrdinal("Access")));
CheckBox4.Checked = (reader.GetBoolean(reader.GetOrdinal("Conn_Net")));
CheckBox5.Checked = (reader.GetBoolean(reader.GetOrdinal("Log_Book")));
CheckBox6.Checked = (reader.GetBoolean(reader.GetOrdinal("Pictures")));
CheckBox8.Checked = (reader.GetBoolean(reader.GetOrdinal("Floor")));
CheckBox9.Checked = (reader.GetBoolean(reader.GetOrdinal("Cb_lenght")));
CheckBox10.Checked = (reader.GetBoolean(reader.GetOrdinal("Channel")));
}
reader.Close();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.