[英]Incorrect syntax near 'mainMenuNum'
Here I am getting values of textbox which are created dynamically 在这里,我得到了动态创建的文本框的值
protected void Submit(object sender, EventArgs e)
{
var textboxValues = new List<string>();
var textboxValues1 = new List<string>();
if (Request.Form.HasKeys())
{
Request.Form.AllKeys.Where(i => i.Contains("Textbox")).ToList().ForEach(i => { textboxValues.Add(Request.Form[i]); });
Request.Form.AllKeys.Where(j => j.Contains("Textbox1")).ToList().ForEach(j => { textboxValues1.Add(Request.Form[j]); });
}
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["IvrContext"].ConnectionString);
conn.Open();
Column data type is nvarchar
. 列数据类型为
nvarchar
。 I can't figure out what the problem with this is; 我不知道这是什么问题。 please help!
请帮忙!
SqlCommand com = new SqlCommand("INSERT INTO menudatas [mainMenuNum] values (@mainMenuNum)", conn);
for(int x = 0; x<textboxValues.Count(); x++)
{
com.Parameters.AddWithValue("@mainMenuNum", textboxValues[x]);
com.ExecuteNonQuery();
}
SqlCommand com1 = new SqlCommand("INSERT INTO menudatas [mainMenuText] values (@mainMenuText)" , conn);
for (int y = 0; y < textboxValues1.Count(); y++)
{
com.Parameters.AddWithValue("@mainMenuText" , textboxValues1[y]);
com1.ExecuteNonQuery();
container.Visible = false;
}
conn.Close();
}
Well, you keep adding a parameter - with the same name every time - to your SqlCommand
. 好吧,您一直在向
SqlCommand
添加一个参数-每次都具有相同的名称。 You need to define the parameter once ; 您需要一次 定义参数; before the loop - and then just assign the value to it in the loop:
在循环之前-然后在循环中为其分配值 :
SqlCommand com = new SqlCommand("INSERT INTO menudatas (mainMenuNum) values (@mainMenuNum)", conn);
// define your parameter ONCE before the loop
com.Parameters.Add("@mainMenuNum", SqlDbType.VarChar, 20); // adapt the datatype to match your situation
for(int x = 0; x<textboxValues.Count(); x++)
{
// only assign the VALUE to the parameter repeatedly, in the loop
com.Parameters["@mainMenuNum"].Value = textboxValues[x]);
com.ExecuteNonQuery();
}
SqlCommand com1 = new SqlCommand("INSERT INTO menudatas (mainMenuText) values (@mainMenuText)" , conn);
// define your parameter ONCE before the loop - and add it to the **correct** SqlCommand "com1" - not "com" ....
com1.Parameters.Add("@mainMenuText", SqlDbType.VarChar, 100); // adapt the datatype to match your situation
for (int y = 0; y < textboxValues1.Count(); y++)
{
// only assign the VALUE to the parameter repeatedly, in the loop
com.Parameters["@mainMenuText"].Value = textboxValues1[y]);
com1.ExecuteNonQuery();
container.Visible = false;
}
conn.Close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.