Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

C#
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
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
SQL
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
C#
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
SQL
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
Posted
Comments
Azee 7-Oct-13 14:58pm    
Debug and check the value of DocID that you are trying to insert in Approval table, it should be present in Primary key values of DocumentInfo table.
Diya Ayesa 7-Oct-13 15:05pm    
yes it is present in documentinfo table.. and there are also several documents in dcumentinfo table
Azee 7-Oct-13 15:08pm    
Take a closer look, that is the only reason this Exception could occur, where exactly the Exception is occuring, here:
docc.approve(Convert.ToInt32(Session["UserID"]), Convert.ToInt32(Session["DocID"]), Convert.ToInt32(Session["ApproveID"]), Convert.ToString(Session["Login2"]));
or here:
cmd.ExecuteNonQuery(); ?
Diya Ayesa 7-Oct-13 15:12pm    
in above code..forget about cmd.ExeucuteNonquery();
an when i set a breakpoint it show me user '10' and docid '0' approveid '0' approveby 'sundus'
Azee 7-Oct-13 15:15pm    
That is the problem. docid '0', I am sure there is no docid in DocumentInfo table with value 0. Why are you getting values from Session for docc.approve? when did you store them there?

1 solution

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:
C#
docc.approve(Convert.ToInt32(Session["UserID"]), Convert.ToInt32(Session["DocID"]), Convert.ToInt32(Session["ApproveID"]), Convert.ToString(Session["Login2"]));

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:
C#
docc.approve(Convert.ToInt32(Session["UserID"]), Convert.ToInt32(Session["DocID"]), Convert.ToInt32(Session["ApproveID"]), Convert.ToString(Session["Login2"]));


and add another parameter in the below code for AppoveBy :
C#
cmd.Parameters.Add("@ApproveBy", SqlDbType.VarChar, 50).Value = Convert.ToString(Session["Login2"]);


Let me know if it helps

Azee...
 
Share this answer
 
v2
Comments
ZurdoDev 7-Oct-13 15:38pm    
+5
Diya Ayesa 7-Oct-13 15:48pm    
ok i add this one..

SqlCommand cmd = new SqlCommand("approved", mySQLconnection);
cmd.Parameters.Add("@ApproveBy", SqlDbType.VarChar, 50).Value = 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();


it show me error in this line
cmd.Parameters.Add("@UserID", SqlDbType.Int).Value = Convert.ToInt32(GrdFileApprove.DataKeys[row.RowIndex]["UserID"]);

error
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Azee 7-Oct-13 16:09pm    
in this line, this Exception could occur if row.RowIndex does not lie within the indexes of the GridView Rows, and since its in a loop, this should not be a problem.
what is row.RowIndex when you debug?

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