在数据库中插入语句 [英] insert statement in database
问题描述
尝试通过下拉列表提交文件,当管理员在gridview中选择批准/拒绝,然后点击提交按钮然后执行操作我在提交按钮中输入代码
try to submit documents through dropdownlist when admin select approve/reject in gridview once then click on submit button then action performed i code in submit button
protected void Button1_Click(object sender, EventArgs e)
{
string connStr = ConfigurationManager.ConnectionStrings["mydms"].ConnectionString;
SqlConnection mySQLconnection = new SqlConnection(connStr);
if (mySQLconnection.State == ConnectionState.Closed)
{
mySQLconnection.Open();
}
// DropDownList drdList;
// SqlCommand mySqlCommand;
foreach (GridViewRow row in GrdFileApprove.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
DropDownList DropDownListcontrol = row.FindControl("DropDownList4") as DropDownList;
SqlCommand cmd = new SqlCommand("approved", mySQLconnection);
docc.approve(Convert.ToInt32(Session["UserID"]), Convert.ToInt32(Session["DocID"]), Convert.ToInt32(Session["ApproveID"]), Convert.ToString(Session["Login2"]));
cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = Convert.ToInt32(GrdFileApprove.DataKeys[row.RowIndex]["UserID"]);
cmd.Parameters.Add("@DocID", SqlDbType.Int).Value = Convert.ToInt32(GrdFileApprove.DataKeys[row.RowIndex]["DocID"]);
cmd.Parameters.Add("@ApproveID", SqlDbType.Int).Value = DropDownListcontrol.SelectedValue;
cmd.ExecuteNonQuery();
}
else
{
apfi.Text = "Error";
}
}
if (mySQLconnection.State == ConnectionState.Open)
{
mySQLconnection.Close();
}
}
image
< a href =http://i.stack.imgur.com/BGVdM.png> http://i.stack.imgur.com/BGVdM.png [ ^ ]
还有管理员点击提交按钮时的批准表各个列填写
http://i.stack.imgur.com/mfuK8.png [ ^ ]
这里是sp
image
http://i.stack.imgur.com/BGVdM.png[^]
and there is also approval table when admin click on submit button respective columns filled
http://i.stack.imgur.com/mfuK8.png[^]
here is sp
ALTER procedure [dbo].[approved]
@UserID int,
@DocID int,
@ApproveID int,
@AppoveBy varchar(50)
as
insert Approval (UserID,DocID,ApproveID,AppoveBy)
values(@UserID,@DocID,@ApproveID,@AppoveBy)
这里是函数
here is function
public void approve(int userid, int docid, int approveid, string ApproveBy)
{
db.ExecuteNonQuery("approved",new object[]{userid,docid,approveid,ApproveBy});
}
但它显示错误
but it show me error
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Approval_DocumentInfo". The conflict occurred in database "DMSFYPP", table "dbo.DocumentInfo", column 'DocID'.
The statement has been terminated.
任何解决方案请
any solution please
推荐答案
嘿那里,
如上面的评论中所述,你是将docid设为'0',这就是为什么插入表Approval导致异常的原因。
您在本声明中使用Session中的值,它在循环中似乎不对,你将为所有行插入相同的值:
Hey there,
As stated in your comments above you are getting the docid as '0', which is the reason why insertion in table Approval is causing the Exception.
You are using the values from the Session in this statement, which does not seem right as its in a loop and you'll be inserting same values for all rows:
docc.approve(Convert.ToInt32(Session["UserID"]), Convert.ToInt32(Session["DocID"]), Convert.ToInt32(Session["ApproveID"]), Convert.ToString(Session["Login2"]));
首先,我建议你使用 GridView DataKeys
获取UserID,并从批准ID中使用DOCID和DropDownList值。
其次你可以删除这个方法调用:
Firstly, I would suggest you to use the GridView DataKeys
for UserID and DOCID and DropDownList value from approval ID.
Secondly you could just remove this method call:
docc.approve(Convert.ToInt32(Session["UserID"]), Convert.ToInt32(Session["DocID"]), Convert.ToInt32(Session["ApproveID"]), Convert.ToString(Session["Login2"]));
并在下面的代码中添加另一个参数 AppoveBy
:
cmd.Parameters.Add("@ApproveBy", SqlDbType.VarChar, 50).Value = Convert.ToString(Session["Login2"]);
让我知道它是否有帮助
Azee ......
Let me know if it helps
Azee...
这篇关于在数据库中插入语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!