Parameter problem with Gridview updating using SQL Datasource & Stored Procedures

Posted on 2008-10-20
Medium Priority
Last Modified: 2013-11-08

I am trying to use the gridview and datasource control to view and edit database information. However, I am having a problem when trying to update the information.   The datasource uses two different stored procedures, as you can see in the code.  

The problem that seems to be thrown is once I have clicked the update button, it shows the following error:
"Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Procedure or function spUpdateRecipients has too many arguments specified."

The update stored procedure has three parameters:
@active int
@configNum int
@user varchar(50)

The datasource was set up using the IDE not in code, but it only has two parameters put in for some reasons.  I thought this might have been the issue, but upon adding in the third it still gives the same error...

                            <asp:Parameter Name="active" Type="Int32" />
                            <asp:Parameter Name="configNum" Type="Int32" />
<asp:Parameter Name="user" Type="String" />

If anyone could give me some ideas of how to fix this it would be great.

<%@ Page Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="ModifyRecipients.aspx.cs" Inherits="ReportRecipients2008.ModifyRecipients" Title="Untitled Page" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div style="width:100%">
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" RepeatDirection="Horizontal">
                        <asp:ListItem Value="0">Study Number</asp:ListItem>
                        <asp:ListItem Value="1">Protocol ID</asp:ListItem>
                    <asp:MultiView ID="MultiView1" runat="server">
                        <asp:View ID="vwStudyNum" runat="server">
                            <asp:SqlDataSource ID="dsCountries" runat="server" 
                                ConnectionString="<%$ ConnectionStrings:GPMConnectionString %>" 
                                SelectCommand="SELECT DISTINCT [CountryDescription] FROM [Countries]">
                            <cc1:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender1" runat="server" 
                                TargetControlID="tbStudyNumber1" WatermarkText="Enter Study Number"></cc1:TextBoxWatermarkExtender>
                            <asp:DropDownList ID="ddlCountries" runat="server" AppendDataBoundItems="True" 
                                DataSourceID="dsCountries" DataTextField="CountryDescription" 
                                <asp:ListItem>All Countries / None</asp:ListItem>
                            <asp:TextBox ID="tbStudyNumber1" runat="server"></asp:TextBox>
                            <asp:Button ID="btnSearch" runat="server" OnClick="btnSearch_Click" 
                                Text="Search" />
                            <br /><br />
                        <asp:View ID="vwProtocolNum" runat="server">
                            <asp:TextBox ID="tbProtocolId2" runat="server" ReadOnly="True"></asp:TextBox>
                            <asp:Label ID="Label4" runat="server" Text="Enter protocol ID"></asp:Label>
                    <br />
                    <asp:GridView  BorderWidth="0px" ID="grdvUserInfo" runat="server" 
                        DataSourceID="dsRecips" BackColor="White" CellPadding="4" 
                        ForeColor="Black" GridLines="Vertical" Width="500px" 
                        AutoGenerateColumns="False" onrowdatabound="grdvUserInfo_RowDataBound" 
                        DataKeyNames="Recip,Config_Num" EmptyDataText="No data returned">
                        <FooterStyle BackColor="#CCCC99" />
                        <RowStyle BackColor="#CCCCCC" ForeColor="Black" BorderColor="Black" BorderStyle="Solid" />
                            <asp:CommandField ButtonType="Image" CancelImageUrl="~/Images/cancel.png" 
                                EditImageUrl="~/Images/edit.png" HeaderText="Modify" ShowCancelButton="False" 
                                ShowEditButton="True" UpdateImageUrl="~/Images/save.png">
                                <ControlStyle Height="16px" Width="16px" />
                            <asp:BoundField DataField="Recip" HeaderText="Recip" ReadOnly="True" 
                                SortExpression="Recip" />
                            <asp:TemplateField HeaderText="Active" SortExpression="Active">
                                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Active") %>'></asp:TextBox>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Active") %>'></asp:Label>
                            <asp:BoundField DataField="Config_Num" HeaderText="Config_Num" SortExpression="Config_Num" 
                                Visible="True" ReadOnly="True" />
                        <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
                        <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="DimGray" Font-Bold="True" ForeColor="White" />
                        <AlternatingRowStyle BackColor="#E0E0E0" />
                    <asp:SqlDataSource ID="dsRecips" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:ReportingConnectionString %>" 
                        SelectCommandType="StoredProcedure" UpdateCommand="spUpdateRecipients" 
                            <asp:Parameter Name="country" Type="String" />
                            <asp:Parameter Name="jobId" Type="Int32" />
                            <asp:Parameter Name="active" Type="Int32" />
                            <asp:Parameter Name="configNum" Type="Int32" />
            <asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1"
                    <div id="loadScreen">
                        <div id="loadScreenDetail">
                            <div id="loadScreenDetailMessage">                    
	                            <img height="15" src="Images/ajax-loader.gif" alt="loading image"/>
                                Updating, please wait...

Open in new window

Question by:Artform04
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 13

Expert Comment

ID: 22758268
a. can you post the header part of your sproc.

b.You say you added the third param yet i dont see it in the code you posted.

c.Can you post your code behind

I notice nothing is binding to your 'configNum'  and  'user' params....
only 'active'
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Active") %>'></asp:TextBox>
unless your doing this in the code behind..

Author Comment

ID: 22758519
Hey thanks for the reply.  In response to what you said...

a) here is the SPROC header:
USE [Reporting]
/****** Object:  StoredProcedure [dbo].[spUpdateRecipients]    Script Date: 10/20/2008 15:50:08 ******/

ALTER PROCEDURE [dbo].[spUpdateRecipients]
@active int,
@configNum int,
@user varchar(50)

b) In the original question, I showed you waht I put, however it didnt make any difference so I reverted it back to the IDE generated version (two params)
c) The code file is attached below.
d) As regards the binding, I converted the "Active" field to a template field as I jsut wanted to check it was using "bind" and not "eval".  The other items are boundfields which I believe are automatically bound with the datafield property e.g. DataField="Recip"

Thanks, hope the material provided can help you help me...
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
namespace ReportRecipients2008
    public partial class ModifyRecipients : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            if (!Page.IsPostBack)
                RadioButtonList1.SelectedIndex = 0;
                MultiView1.ActiveViewIndex = RadioButtonList1.SelectedIndex;
        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
            MultiView1.ActiveViewIndex = RadioButtonList1.SelectedIndex;
            grdvUserInfo.Visible = false;
        protected void btnSearch_Click(object sender, EventArgs e)
            //set the parameter value for the select
            dsRecips.SelectParameters["jobId"].DefaultValue = tbStudyNumber1.Text;
            if (ddlCountries.SelectedIndex == 0)
                dsRecips.SelectParameters["country"].DefaultValue = "";
                dsRecips.SelectParameters["country"].DefaultValue = ddlCountries.SelectedValue;
            //get the new information
            grdvUserInfo.Visible = true;
        protected void grdvUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)
            if (Page.IsPostBack) {
                    e.Row.BorderWidth = 0;
                    if (e.Row.Cells[1].Text == "1")
                        e.Row.BackColor = System.Drawing.Color.LightGreen;
                        e.Row.Cells[1].Text = "Y";
                    else if (e.Row.Cells[1].Text == "0")
                        e.Row.Cells[1].Text = "N";
                catch (Exception)
                 // no problem, just trying to look at cells when there is no data so ignore

Open in new window


Accepted Solution

Artform04 earned 0 total points
ID: 22765630
I have discovered the problem.  Visual Studio is automatically adding in Key's as parameters on top of the ones in the code, resulting in too many parameters.

To resolve this you need to either remove the doubled parameters in the ASPX codefile (the duplicate of the key)


In the datasource.updating event, delete the problem parameter.  The code i used for this is attached...

protected void dsRecips_Updating(object sender, SqlDataSourceCommandEventArgs e)
            int indexParam = 0;
            bool isFound = false;
            //search for the problem additional parameter which is added automatically by VS
            foreach (System.Data.SqlClient.SqlParameter xP in e.Command.Parameters)
                //check if this is the trouble extra parameter
                if (xP.ParameterName == "@ConfigNum" && xP.SqlValue == null)
                    //this is the problem parameter, set to found and exit the loop
                   isFound = true;
                //the param wasnt found so increment the index counter
            //if the parameter was found, remove it from the parameters collection
            if (isFound == true)

Open in new window


Featured Post

Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question