如何使用此功能将数据库插入列表框中的多个项目
你好
iam尝试添加更多感谢文件,但我收到错误消息
hello
iam trying to add more thank one filed but i get error
foreach (ListItem lst in test_names.Items)
{
if (lst.Selected)
{
string selectedValue = lst.Value;
string patient_id = Request.QueryString["id"];
SqlConnection cnm = new SqlConnection(ConfigurationManager.ConnectionStrings["lap_connection"].ConnectionString);
SqlCommand cmd = new SqlCommand("INSERT INTO [patient_test_data] (test_id,test_date,pat_id) VALUES ('" + selectedValue + "','" + DateTime.Now.ToString("yyyy-MM-dd") + "','" + patient_id + "');", cnm);
cmd.Connection = cnm;
cmd.CommandType = CommandType.Text;
cnm.Open();
cmd.ExecuteScalar();
cnm.Close();
ScriptManager.RegisterStartupScript(this, this.GetType(), "show_error", "showSuccess('Test Has Been Added Successfully',2000);", true);
}
}
这是我得到的错误
INSERT语句中的列多于VALUES子句中指定的值. VALUES子句中的值数必须与INSERT语句中指定的列数匹配.
我不知道如何使用此功能插入
OMG是我吗:@让我杀死我的自我OMG !!!!我怎么没看到这个-_-谢谢,顺便说一句,让我们继续下一步
现在我想创建一个Daynamic控件3标签和3文本框,取决于值来自sql数据读取器列数"
这是我的代码
this is the error that i got
There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.
i don''t know how to insert with this function
OMG is it me :@ let me kill my self OMG !!!! how the hell i didn''t see this -_- thanks BTW so let us go to the next step
now i want to create a daynamic control 3 label and 3 textbox depended on value comes from sql data reader "number of columns"
this is my code
<pre lang="c#">cn = new SqlConnection(ConfigurationManager.ConnectionStrings["lap_connection"].ConnectionString);
cmd = new SqlCommand("SELECT COUNT( pat_id) AS patient_test FROM patient_test_data WHERE dbo.patient_test_data.test_date >= cast(getdate() as Date) AND dbo.patient_test_data.pat_id = 4;", cn);
cmd.CommandType = CommandType.Text;
cmd.Connection.Open();
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (dr.Read())
{
int number_test = int.Parse(dr["patient_test"].ToString());
for (int number = int.Parse(dr["patient_test"].ToString()); number <= number_test; number++)
{
TextBox txt = new TextBox();
//txt.Text = dr["Col1"].ToString();
TextBox txt2 = new TextBox();
//txt2.Text = dr["Col2"].ToString();
TextBox txt3 = new TextBox();
//txt3.Text = dr["Col3"].ToString();
this.Controls.Add(txt);
this.Controls.Add(txt2);
this.Controls.Add(txt3);
}
}
我只是我不知道我收到了这个错误集合已被修改;枚举操作可能无法执行.
谢谢Advanced
i just i don''t know i got this error Collection was modified; enumeration operation may not execute.
thanks in advanced
您的sql命令INSERT INTO [patient_test_data] (test_id,test_date,pat_id,data_id)
期望提供4个值.您只提供了其中的3个...
test_id< = selectedValue
test_date< =现在
pat_id< = Patient_id
data_id< = ????????????????
顺便说一句,在创建SQL命令时,请警惕SQL注入攻击的风险-请参阅此参考 http://bobby-tables.com/ [^ ]
[摘自我的解决方案,网址为如何添加所选内容列表框中的项目可将每个选定的项目数据库存储到新记录中 [
Your sql commandINSERT INTO [patient_test_data] (test_id,test_date,pat_id,data_id)
is expecting 4 values to be supplied. You have only supplied 3 of them...
test_id <= selectedValue
test_date <= Now
pat_id <= patient_id
data_id <= ????????????????
As an aside, when you are creating SQL commands be wary of the risk of SQL Injection Attacks - see this reference http://bobby-tables.com/[^]
[Copied from my solution at how to add selected items in listbox to database every selected item to new record[^]]