Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting stored procedure error from gridview 1

Status
Not open for further replies.

DCSage

Programmer
Mar 5, 2010
74
US
Hello.

I have a simple gridview that employs add/edit/delete events.

My delete event is working perfectly, but I am getting an error in update event. I checked my actual stored procedure and all of my parameters are present. I am not sure why i am getting the error.

I am doing most of work on the code-behind and I am using a sqldatasource on the front. should I also make sure that I add the parameters within the sqldatasource? I tried this as well and I jsut get all sorts of errors. I am ready to throw my computer. Sorry fo rht elong post.

Error:
Code:
Procedure or function updateInventory has too many arguments specified.
  at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
  at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
  at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
  at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
  at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
  at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
  at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
  at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
  at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
  at System.Web.UI.WebControls.SqlDataSourceView.ExecuteDbCommand(DbCommand command, DataSourceOperation operation)
  at System.Web.UI.WebControls.SqlDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues)
  at System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback)
  at System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation)
  at System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup)
  at System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e)
  at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
  at System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e)
  at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
  at System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e)
  at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
  at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
  at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
  at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
  at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


Update function and statement:

Code:
 private void UpdateRecord(int recID, string equip, string build, string loc, string brand, string room, string floor, string dep, string depcode, string serial, string model, string comments)
    {
        string mc = ConfigurationManager.ConnectionStrings["SiteDB"].ConnectionString;
         using (SqlConnection conn = new SqlConnection(mc))
         {
             using (SqlCommand comm = new SqlCommand("updateInventory", conn))
             {
                 comm.CommandType = CommandType.StoredProcedure;

                 try
                 {
                     comm.Connection.Open();

                     comm.Parameters.AddWithValue("@inventoryID", recID);
                     comm.Parameters.AddWithValue("@equipment", equip);
                     comm.Parameters.AddWithValue("@bldg", build);
                     comm.Parameters.AddWithValue("@equipLoc", loc);                                      comm.Parameters.AddWithValue("@brand", brand);
                     comm.Parameters.AddWithValue("@roomNo", room);
                     comm.Parameters.AddWithValue("@floor", floor);
                     comm.Parameters.AddWithValue("@dept", dep);
                     comm.Parameters.AddWithValue("@deptcode", depcode);
                     comm.Parameters.AddWithValue("@SerialNo", serial);
                     comm.Parameters.AddWithValue("@ModelNo", model);
                     comm.Parameters.AddWithValue("@comments", comments);
                     comm.ExecuteNonQuery();
                 }
                 catch (Exception ex)
                 {
                     string msg = "Insert/Update Error:";
                     msg += ex.Message;
                     throw new Exception(msg);
                 }
                 finally
                 {
                     comm.Connection.Close();
                 }                 
             }
         }


Row-updating event

Code:
    protected void iGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //access edited values from the gridview and cast 
        try
        {
            int record;
            string building;
            string location;
            string equipment;
            string brand;
            string roomno;
            string floor;
            string department;
            string deptno;
            string serial;
            string model;
            string comments;

            Label inventoryID = (Label)iGrid.Rows[e.RowIndex].Cells[2].FindControl("idlbl");
            DropDownList drpBlding = (DropDownList)iGrid.Rows[e.RowIndex].Cells[7].FindControl("drpBuilding");
            DropDownList drpLocation = (DropDownList)iGrid.Rows[e.RowIndex].Cells[8].FindControl("drploctype");
            DropDownList drpDepart = (DropDownList)iGrid.Rows[e.RowIndex].Cells[11].FindControl("drpDept");
            DropDownList drpEquip = (DropDownList)iGrid.Rows[e.RowIndex].Cells[3].FindControl("drpBrand");
            DropDownList drpBrandt = (DropDownList)iGrid.Rows[e.RowIndex].Cells[4].FindControl("drpBrandType");
            TextBox txtRoom = (TextBox)iGrid.Rows[e.RowIndex].Cells[9].FindControl("txtEditRoom");
            DropDownList drpFloor = (DropDownList)iGrid.Rows[e.RowIndex].Cells[10].FindControl("drpFloor");
            TextBox txtModel = (TextBox)iGrid.Rows[e.RowIndex].Cells[5].FindControl("txtModel");
            TextBox txtSerial = (TextBox)iGrid.Rows[e.RowIndex].Cells[6].FindControl("txtSerial");
            TextBox txtComments = (TextBox)iGrid.Rows[e.RowIndex].Cells[12].FindControl("txtComments");

            record = Convert.ToInt32(inventoryID.Text);
            building = drpBlding.SelectedValue.ToString();
            location = drpLocation.SelectedValue.ToString();
            equipment = Convert.ToString(drpEquip.SelectedItem.ToString());
            brand = drpBrandt.SelectedValue.ToString();
            roomno = txtRoom.Text.ToString();
            floor = drpFloor.SelectedValue.ToString();
            department = drpDepart.SelectedItem.ToString();
            deptno = drpDepart.SelectedValue.ToString();
            serial = txtSerial.Text.ToString();
            model = txtModel.Text.ToString();
            comments = txtComments.Text.ToString();


            //call update method
            UpdateRecord(record, equipment, building, location, brand, roomno, floor, department, deptno, serial, model, comments);

            iGrid.EditIndex = -1;
            iGrid.DataBind();

            lbldirections.Text = "Update Successful";
        }
        catch (Exception el)
        {
            Response.Write(el);
        }
    }
 
I am confused as to why you have a sqldatasource AND using stored procedures.
My advice is to get rid of the sql datasource. They only add a layer of confusion because in the event of an error, or something is not working as expected, you cannont debug them.
Since you are already using stored procedures, use them for each function, Select, Insert, Update, and Delete.
 
so i will then have to programmatically bind, add my controls and whatnot to the gridview then right (in the code-behind)?
 
nothing changes in the gridview. all you do is bind to a datatable.
 
thank you jbenson001. I had a lot of errors in my code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top