在网页中我正在使用一些添加按钮的html控件(文本框)。使用for循环我计算这些控件并将它们的数据插入到数据库中,但是当进程启动时,只插入前两个记录,然后抛出此异常。所有列都出现在代码中,除了sr.no和autonumbered之外,它也是主键。
protected void _btnSendRequest_Click(object sender, EventArgs e)
{
using (CertDataContext context = new CertDataContext())
{
Tech_Request_File j = new Tech_Request_File();
for (int i = 0; i < Request.Form.Count; i++)
{
string FileName = "_txtFileName" + i;
string FileValue = Request.Form[FileName];
string FilePath = "_txtFilePath" + i;
string PathValue = Request.Form[FilePath];
if (FileValue != null && PathValue != null)
{
j.user_id = Convert.ToInt32(_txtID.Text);
j.incident_id = Convert.ToInt32(_txtIncidentID.Text);
j.status = 1;
j.tech_id = Convert.ToInt32(Request.QueryString["id"]);
j.file_name = FileValue;
j.file_path = PathValue;
try
{
context.Tech_Request_Files.InsertOnSubmit(j);
context.SubmitChanges();
}
catch (Exception ex)
{
Response.Write("<SCRIPT LANGUAGE=\"JavaScript\">alert(\" " + ex.Message + "<br />" + ex.Source + "<br />" + ex.InnerException + "<br />" + ex.HResult + " \")</SCRIPT>");
}
}
else
{
Response.Write("<SCRIPT LANGUAGE=\"JavaScript\">alert(\" Request sent successfully. \")</SCRIPT>");
break;
}
}
}
答案 0 :(得分:1)
您正试图在Tech_Request_File
循环中重复填充并插入相同的for
实例。您只需要为循环中的每次迭代实例化一个新的Tech_Request_File
实体:
<击> 撞击>
<击>Tech_Request_File j = new Tech_Request_File();
击> <击> 撞击>
for (int i = 0; i < Request.Form.Count; i++)
{
Tech_Request_File j = new Tech_Request_File();
// ...