for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
string toemail = GridView1.Rows[i].Cells[2].Text;
string FID1 = GridView1.Rows[i].Cells[0].Text;
GridViewRow row = GridView1.Rows[i];
CheckBox Ckbox = (CheckBox)row.FindControl("CheckBoxMark1");
if (Ckbox.Checked == true)
{
sendMail(toemail);
//ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Email send Succesfully')</script>");
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Email sent Succesfully on " + test + "')</script>");
cn1.Open();
//cmd4.CommandText = "Insert into TrackingFaculty_det (EmailsentDate) values (@EmailsentDate) WHERE FID=@FID";
cmd4.CommandText = "update TrackingFaculty_det SET EmailsentDate=@Email WHERE FID=@FID ";
cmd4.CommandType = CommandType.Text;
cmd4.Connection = cn1;
cmd4.Parameters.Add("@Email", SqlDbType.DateTime, 8);
cmd4.Parameters["@Email"].Value = sdt;
cmd4.Parameters.Add("@FID",SqlDbType.VarChar,10);
cmd4.Parameters["@FID"].Value = FID1;
cmd4.ExecuteNonQuery();
cn1.Close();
}
}
//这是c#
代码的一部分以及asp.net
如果for loop
执行updation
,database
for loop
应该在update
中完成,The variable name '@Email' has already been declared. Variable names must be unique within a query batch or stored procedure.
执行{{1}}多次{{1}}次问题,则会出现问题会给出{{1}}的错误。对此有什么解决方法吗?
答案 0 :(得分:3)
只需将循环的不变部分移到循环外部即可。这意味着,SqlCommand的初始化,连接的打开以及参数集合的创建 然后在循环内部只需更改参数值并执行。 (这总是比在循环内重新创建相同的参数列表更快)
SqlCommand cmd4 = new SqlCommand("update TrackingFaculty_det " +
"SET EmailsentDate=@Email WHERE FID=@FID", cn1);
cn1.Open();
cmd4.Parameters.Add("@Email", SqlDbType.DateTime, 8);
cmd4.Parameters.Add("@FID",SqlDbType.VarChar,10);
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
.....
if (Ckbox.Checked == true)
{
....
cmd4.Parameters["@Email"].Value = sdt;
cmd4.Parameters["@FID"].Value = FID1;
cmd4.ExecuteNonQuery();
}
}
cn1.Close();
答案 1 :(得分:2)
尝试清除命令参数 -
cmd4.Parameters.Clear();
然后添加参数
cmd4.Parameters.Add("@Email", SqlDbType.DateTime, 8);
所以你的最终代码是 -
cmd4.Parameters.Clear(); // <- Just add this line
cmd4.Parameters.Add("@Email", SqlDbType.DateTime, 8);
答案 2 :(得分:1)
cmd4对象。
cmd4.Parameters.Add("@Email", SqlDbType.DateTime, 8);
cmd4.Parameters["@Email"].Value = sdt;
cmd4.Parameters.Add("@FID",SqlDbType.VarChar,10);
cmd4.Parameters["@FID"].Value = FID1;
您将相同的参数多次添加到“cmd4”
的参数列表中在再次添加相同参数之前清除参数
cmd4.Parameters.Clear();
答案 3 :(得分:1)
检查Run Stored Procedure with table data as parameter
或者第二个是使用下面的示例创建字符串,并在sql server
中创建exec(字符串)sqlstr= "update TrackingFaculty_det SET EmailsentDate=2@2.com WHERE FID=1; update TrackingFaculty_det SET EmailsentDate=1@1.com WHERE FID=@FID ;pdate TrackingFaculty_det SET EmailsentDate=2@2.com WHERE FID=1;"