Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i try to approve documents thrugh following code
C#
if (e.CommandName == "_Approve")
        {
            //using (SqlConnection con = DataAccess.GetConnected())
            using (SqlConnection con = new 
          SqlConnection(ConfigurationManager.ConnectionStrings["mydms"]
           .ConnectionString))
            {
                try
                {
                    con.Open();
                    int rowindex = Convert.ToInt32(e.CommandArgument);
                    GridViewRow row = (GridViewRow)
                   ((Control)e.CommandSource).NamingContainer;
                    Button Prove_Button = (Button)row.FindControl("BtnApprove");
                    SqlCommand cmd = new SqlCommand("approve", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@UserID", UserID));
                    cmd.Parameters.Add(new SqlParameter("@DocID", DocID));
                    cmd.Parameters.Add(new SqlParameter("@ApproveID", ApproveID));
                    int result = cmd.ExecuteNonQuery();
                    if (result != 0)
                    {
                        GrdFileApprove.DataBind();
                    }
                }

                catch
                {
                    apfi.Text = "Not Approve";



                }
                finally
                {
                    con.Close();
                }
            }
        }


        else if (e.CommandName == "_Reject")
        {
            using (SqlConnection con = new 
          SqlConnection(ConfigurationManager.ConnectionStrings
        ["mydms"].ConnectionString))
            {
                try
                {
                    con.Open();
                    int rowindex = Convert.ToInt32(e.CommandArgument);
                    GridViewRow row = (GridViewRow)
                    ((Control)e.CommandSource).NamingContainer;
                    LinkButton Prove_Button = (LinkButton)row.FindControl("Button1");
                    SqlCommand cmd = new SqlCommand("sprejectapprove", con);

                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@UserID",UserID));
                    cmd.Parameters.Add(new SqlParameter("@DocID", DocID));
                    cmd.Parameters.Add(new SqlParameter("@ApproveID", ApproveID));
                    int result = cmd.ExecuteNonQuery();
                    if (result != 0)
                    {
                        GrdFileApprove.DataBind();
                    }
                }

                catch 
                {
                    apfi.Text = "Rejct";
                }
                finally
                {
                    con.Close();
                }
            }
        }



and store procedure of approve is
@UserID int,
  @DocID int,
 @ApproveID int
as
insert into Approval(UserID,DocID,ApproveID)
VALUES(@UserID,@DocID,@ApproveID)



when i debug the code and click an accept button it show me error in catch in approve part

"The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Approval_ApproveType". The conflict occurred in database "DMSFYPP", table "dbo.ApproveType", column 'ApproveID'. The statement has been terminated"


any help


thanku
Posted
Comments
Mike Meinz 1-Oct-13 10:01am    
There is a foreign key declared in the Approval table. The ApproveID value inserted into a new row in the Approve table must match an ApproveId in the ApproveType table. The ApproveID you are inserting is not in the ApproveType table.
Diya Ayesa 1-Oct-13 10:07am    
there is approveid in both tables approval and approvetype..
Diya Ayesa 1-Oct-13 10:26am    
here is erd link

http://i41.tinypic.com/zyhir8.png
Pradeep Shukla 1-Oct-13 10:36am    
It's not about the ColumnName (ApprovalId), the data you are inserting as ApprovalID in the table Approval does not match with any of the values in the ApprovalType table.
Diya Ayesa 1-Oct-13 10:46am    
so its mean i change my sp?

1 solution

Quote:
"The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Approval_ApproveType". The conflict occurred in database "DMSFYPP", table "dbo.ApproveType", column 'ApproveID'. The statement has been terminated"

Let me explain the issue.

There are two tables.

  • Approval
  • ApproveType


There is a Relationship between them named as "FK_Approval_ApproveType".

When you write the following code...
SQL
Insert into Approval(UserID, DocID, ApproveID)
VALUES(@UserID, @DocID, @ApproveID)

the value of @ApproveID should be present in Table ApproveType.
That means a record for the @ApproveID should exist in the table.

For example if you are inserting row for ApproveID - 3, then there should be a Record existing for that ID in ApprovalType Table.

You can know what is the Approval Type you are sending by putting a Break Point inside the function and checking the ApproveID.
 
Share this answer
 
Comments
Diya Ayesa 1-Oct-13 11:42am    
i already enter the record in table approvetype i.e in approvetype

approveid approvetype status
1 approve true
2 reject true
3 pending true..
Diya Ayesa 1-Oct-13 11:43am    
this sp create procedure approveee @UserID int, @DocID int, @ApproveID int as Update Approval SET ApproveID=@ApproveID ,UserID=@UserID where DocID=@DocID
is right or wrong?
Yes, it seems correct. Is there any issues?
Diya Ayesa 1-Oct-13 11:51am    
yesss...now this error comes :(

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
Now the same type of Problem with table DocumentInfo.

You don't have a record for that DocID in DocumentInfo Table. Please check.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900