Click here to Skip to main content
16,017,376 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
i try to apprve dcuments but the following error occur

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"]));

                   
                    mySqlCommand.Parameters.Add("@UserID", SqlDbType.Int).Value = Convert.ToInt32(GrdFileApprove.DataKeys[row.RowIndex]["UserID"]);

                    mySqlCommand.Parameters.Add("@DocID", SqlDbType.Int).Value = Convert.ToInt32(GrdFileApprove.DataKeys[row.RowIndex]["DocID"]);

                    mySqlCommand.Parameters.Add("@ApproveID", SqlDbType.Int).Value = DropDownListcontrol.SelectedValue;

                    //docc.approve(Convert.ToInt32(Session["UserID"]), Convert.ToInt32(Session["DocID"]), Convert.ToInt32(Session["ApproveID"]), Convert.ToString(Session["Login2"]));

                    mySqlCommand.ExecuteNonQuery(); 
                }
                else
                {
                    apfi.Text = "Error";
                }

                
            }


            if (mySQLconnection.State == ConnectionState.Open)
            {
                mySQLconnection.Close();
            }
        }


error in this line

C#
mySqlCommand.Parameters.Add("@UserID", SqlDbType.Int).Value = Convert.ToInt32(GrdFileApprove.DataKeys[row.RowIndex]["UserID"]);
Posted

Hey there,

Its quite obvious in your code, you have declared an SqlCommand variable
SqlCommand mySqlCommand; but used it without creating its object here:
C#
mySqlCommand.Parameters.Add("@UserID", SqlDbType.Int).Value = Convert.ToInt32(GrdFileApprove.DataKeys[row.RowIndex]["UserID"]);

and you are creating an object of SqlCommand here:
C#
SqlCommand cmd = new SqlCommand("approved", mySQLconnection);
but never used it.

Change the above line to:
C#

mySqlCommand = new SqlCommand("approved", mySQLconnection);

Let me know if it helps,

Azee...
 
Share this answer
 
Change "cmd" to "mySqlCommand" or vice versa.

SqlCommand mySqlCommand = new SqlCommand("approved", mySQLconnection);
                    
                    docc.approve(Convert.ToInt32(Session["UserID"]), Convert.ToInt32(Session["DocID"]), Convert.ToInt32(Session["ApproveID"]), Convert.ToString(Session["Login2"]));
 
Share this answer
 

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