Gridview and Updating

Discussion in 'ASP .Net' started by =?Utf-8?B?TWljaGFlbA==?=, Apr 20, 2006.

  1. Hi Everyone,
    I've been having a problem with the Gridview control. I have posted a few
    messages relating to the issues, but I have a general question. Does the
    Dataview control have to be bound to a datasource that also contains the
    update/delete Sql statements? The resaon I ask is that I've done just about
    all I can do and can't update the grid. In the RowUpdating event I'm getting
    no values in the newvalues or oldvalues vars. I do bind a dataTable that was
    produced by a single table stored proc to the datasource property. What am I
    missing, I've been working on this problem a little to long, so ANY help
    would be great.
    Thanks
    Mike
    =?Utf-8?B?TWljaGFlbA==?=, Apr 20, 2006
    #1
    1. Advertising

  2. Hi Michael,

    If you were using templates make sure that you are using 2-way databinding
    expression such as Bind, otherwise, start from a blue print of a code that
    works and build on it. Here are some starting demos:
    http://www.asp.net/QuickStart/aspnet/doc/data/templates.aspx#twowaybind
    http://www.webswapp.com/codesamples/aspnet20/dependentlists/gridview.aspx
    http://www.webswapp.com/codesamples/aspnet20/updateparameters/default.aspx

    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "Michael" wrote:

    > Hi Everyone,
    > I've been having a problem with the Gridview control. I have posted a few
    > messages relating to the issues, but I have a general question. Does the
    > Dataview control have to be bound to a datasource that also contains the
    > update/delete Sql statements? The resaon I ask is that I've done just about
    > all I can do and can't update the grid. In the RowUpdating event I'm getting
    > no values in the newvalues or oldvalues vars. I do bind a dataTable that was
    > produced by a single table stored proc to the datasource property. What am I
    > missing, I've been working on this problem a little to long, so ANY help
    > would be great.
    > Thanks
    > Mike
    >
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Apr 20, 2006
    #2
    1. Advertising

  3. Hi Phillip,
    Thank you so much for the reply. After looking over your suggested reading
    and reading some other posts I got the impression that the Edit/Delete
    functions only work when you are using a Datasource attached to the
    DatasourceId property. Is this correct? Based on that, I have redesigned the
    for some using a SqlDataSource attached to the DatasourceId. But I'm getting
    the following error when trying to update a row:
    Procedure or function Admin_UpdatePOItem has too many arguments specified.
    Description: An unhandled exception occurred during the execution of the
    current web request. Please review the stack trace for more information about
    the error and where it originated in the code.
    Exception Details: System.Data.SqlClient.SqlException: Procedure or function
    Admin_UpdatePOItem has too many arguments specified.
    Source Error:
    An unhandled exception was generated during the execution of the current web
    request. Information regarding the origin and location of the exception can
    be identified using the exception stack trace below.

    Now, I have setup the gridview as follows:
    <asp:GridView ID="grdPOs" runat="server" AllowPaging="True"
    AutoGenerateColumns="False"
    BackColor="White" BorderColor="#CC9966" BorderStyle="None"
    BorderWidth="1px"
    CellPadding="4" DataKeyNames="ItemId" Height="68px" PageSize="5"
    Width="642px" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"
    AutoGenerateSelectButton="True" DataSourceID="PODetailDataSource">
    <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
    <Columns>
    <asp:BoundField DataField="ItemID" HeaderText="Item ID"
    Visible="False" />
    <asp:BoundField DataField="POrderId" HeaderText="PO Id"
    Visible="False" />
    <asp:BoundField DataField="Page" HeaderText="Page">
    <ItemStyle HorizontalAlign="Right" />
    </asp:BoundField>
    <asp:BoundField DataField="CatalogId" HeaderText="Catalog #">
    <ItemStyle HorizontalAlign="Right" Width="80px" />
    </asp:BoundField>
    <asp:BoundField DataField="ItemDescription"
    HeaderText="Description">
    <ItemStyle Width="250px" />
    </asp:BoundField>
    <asp:BoundField DataField="Qty" HeaderText="Qty">
    <ItemStyle HorizontalAlign="Right" />
    </asp:BoundField>
    <asp:BoundField DataField="UnitPrice"
    DataFormatString="{0:C}" HeaderText="UnitPrice">
    <ItemStyle HorizontalAlign="Right" Width="70px" />
    </asp:BoundField>
    <asp:BoundField DataFormatString="{0:0.00}"
    HeaderText="Total" InsertVisible="False" />
    </Columns>
    <RowStyle BackColor="White" ForeColor="#330099" />
    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True"
    ForeColor="#663399" />
    <PagerStyle BackColor="#FFFFCC" ForeColor="#330099"
    HorizontalAlign="Center" />
    <HeaderStyle BackColor="#990000" Font-Bold="True"
    ForeColor="#FFFFCC" />
    </asp:GridView>
    And the datasource is as follows:
    <asp:SqlDataSource ID="PODetailDataSource" runat="server"
    ConnectionString="<%$ ConnectionStrings:pODetailsConnectionString %>"
    SelectCommand="admin_GetPODetails"
    SelectCommandType="StoredProcedure" DeleteCommand="Admin_DeletePOItem"
    DeleteCommandType="StoredProcedure" InsertCommand="Admin_AddPOItems"
    InsertCommandType="StoredProcedure" UpdateCommand="Admin_UpdatePOItem"
    UpdateCommandType="StoredProcedure">
    <SelectParameters>
    <asp:ControlParameter ControlID="cmbPORequests"
    Name="POrderId" PropertyName="SelectedValue"
    Type="Int32" />
    </SelectParameters>
    <DeleteParameters>
    <asp:parameter Name="ItemId" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
    <asp:parameter Name="ItemID" Type="Int32" />
    <asp:parameter Name="CatalogId" Type="String" />
    <asp:parameter Name="ItemDescription" Type="String" />
    <asp:parameter Name="Qty" Type="Int32" />
    <asp:parameter Name="UnitPrice" Type="Decimal" />
    <asp:parameter Name="Page" Type="String" />
    </UpdateParameters>
    <InsertParameters>
    <asp:parameter Name="POrderId" Type="Int32" />
    <asp:parameter Name="CatalogeId" Type="String" />
    <asp:parameter Name="ItemDescription" Type="String" />
    <asp:parameter Name="Qty" Type="Int32" />
    <asp:parameter Name="UnitPrice" Type="Decimal" />
    <asp:parameter Name="Page" Type="String" />
    </InsertParameters>
    </asp:SqlDataSource>
    The Stored proc is written as follows:
    CREATE PROCEDURE [dbo].[Admin_UpdatePOItem]
    @ItemID int,
    @CatalogId varchar(20),
    @ItemDescription varchar(250),
    @Qty int,
    @UnitPrice money,
    @Page varchar(10)
    AS

    Update OrderItems Set
    CatalogId = @CatalogId,
    ItemDescription = @ItemDescription,
    Qty = @Qty,
    UnitPrice = @UnitPrice,
    Page = @Page
    Where ItemID = @ItemID
    GO

    The research that I've done so far I have found that there is an extra
    parameter, so I setup a function to remove the parameter but I still get the
    error. Here is the code to delete.
    'Serves to tell me what parameters and delete the extra one.
    Protected Sub PODetailDataSource_Updating(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles
    PODetailDataSource.Updating
    Dim param As DbParameter
    Dim DeleteParam as DbParameter
    Debug.WriteLine("Starting Test")
    Debug.WriteLine("The command has " & e.Command.Parameters.Count.ToString
    & " parameters altogether.")
    For Each param In e.Command.Parameters
    if param.ParameterName.ToString = "@POrderId" then
    DeleteParam = param
    End If
    if param.ParameterName.ToString.Length > 1 then
    Debug.WriteLine("parameter " & param.ParameterName.ToString & "
    => " & (param.Value.ToString & "") & "<br>")
    end if
    Next
    if not isnothing(DeleteParam) then
    e.Command.Parameters.Remove(DeleteParam)
    end if
    Debug.WriteLine("Starting Second Test")
    Debug.WriteLine("The command has " & e.Command.Parameters.Count.ToString
    & " parameters altogether.")
    For Each param In e.Command.Parameters
    if param.ParameterName.ToString = "@POrderId" then
    DeleteParam = param
    End If
    if param.ParameterName.ToString.Length > 1 then
    Debug.WriteLine("parameter " & param.ParameterName.ToString & "
    => " & (param.Value.ToString & "") & "<br>")
    end if
    Next
    End Sub
    After the code is run (before deleting the parameter) I get the following
    parameter:
    The command has 8 parameters altogether.
    parameter @CatalogId => 254-547<br>
    parameter @ItemDescription => Testing<br>
    parameter @Qty => 5<br>
    parameter @UnitPrice => 24.1500<br>
    parameter @Page => 55<br>
    parameter @ItemId => 3<br>
    parameter @POrderId => 1<br> ****Param to remove *****
    Starting Second Test
    The command has 7 parameters altogether.
    parameter @CatalogId => 254-547<br>
    parameter @ItemDescription => Testing<br>
    parameter @Qty => 5<br>
    parameter @UnitPrice => 24.1500<br>
    parameter @Page => 55<br>
    parameter @ItemId => 3<br>
    Sorry for the long message, but I figured you may need to know this info.
    What do you think. Thanks for the help.
    Michael
    =?Utf-8?B?TWljaGFlbA==?=, Apr 21, 2006
    #3
  4. Hi Phillip,
    I've been testing and researching further. I actually was setting the
    DataKeyNames property as follows:

    Dim lKeys as String() = {"ItemId", "POrderId"}
    grdPOs.DataKeyNames = lKeys

    When I removed the POrderId value the POrderId didn't show up in the debug
    output, but still getting the same error. If I look at the number of
    parameters that printed out, that number is the same the stored proc. I'm
    alittle confused. What the heck is going on here. Thanks
    Michael

    "Phillip Williams" wrote:

    > Hi Michael,
    >
    > If you were using templates make sure that you are using 2-way databinding
    > expression such as Bind, otherwise, start from a blue print of a code that
    > works and build on it. Here are some starting demos:
    > http://www.asp.net/QuickStart/aspnet/doc/data/templates.aspx#twowaybind
    > http://www.webswapp.com/codesamples/aspnet20/dependentlists/gridview.aspx
    > http://www.webswapp.com/codesamples/aspnet20/updateparameters/default.aspx
    >
    > --
    > HTH,
    > Phillip Williams
    > http://www.societopia.net
    > http://www.webswapp.com
    >
    >
    > "Michael" wrote:
    >
    > > Hi Everyone,
    > > I've been having a problem with the Gridview control. I have posted a few
    > > messages relating to the issues, but I have a general question. Does the
    > > Dataview control have to be bound to a datasource that also contains the
    > > update/delete Sql statements? The resaon I ask is that I've done just about
    > > all I can do and can't update the grid. In the RowUpdating event I'm getting
    > > no values in the newvalues or oldvalues vars. I do bind a dataTable that was
    > > produced by a single table stored proc to the datasource property. What am I
    > > missing, I've been working on this problem a little to long, so ANY help
    > > would be great.
    > > Thanks
    > > Mike
    > >
    =?Utf-8?B?TWljaGFlbA==?=, Apr 21, 2006
    #4
  5. Hi Again,
    One more thing. Based on my stored proc and parameters I should have only 6
    parameters, but the PODetailDataSource_Updating events reports 7 parameters
    and after tracing it down I have a "@" as on of the parametername. WHY? That
    would be the extra and its not suppose to be there. Have any ideas.
    Thanks
    Michael


    "Phillip Williams" wrote:

    > Hi Michael,
    >
    > If you were using templates make sure that you are using 2-way databinding
    > expression such as Bind, otherwise, start from a blue print of a code that
    > works and build on it. Here are some starting demos:
    > http://www.asp.net/QuickStart/aspnet/doc/data/templates.aspx#twowaybind
    > http://www.webswapp.com/codesamples/aspnet20/dependentlists/gridview.aspx
    > http://www.webswapp.com/codesamples/aspnet20/updateparameters/default.aspx
    >
    > --
    > HTH,
    > Phillip Williams
    > http://www.societopia.net
    > http://www.webswapp.com
    >
    >
    > "Michael" wrote:
    >
    > > Hi Everyone,
    > > I've been having a problem with the Gridview control. I have posted a few
    > > messages relating to the issues, but I have a general question. Does the
    > > Dataview control have to be bound to a datasource that also contains the
    > > update/delete Sql statements? The resaon I ask is that I've done just about
    > > all I can do and can't update the grid. In the RowUpdating event I'm getting
    > > no values in the newvalues or oldvalues vars. I do bind a dataTable that was
    > > produced by a single table stored proc to the datasource property. What am I
    > > missing, I've been working on this problem a little to long, so ANY help
    > > would be great.
    > > Thanks
    > > Mike
    > >
    =?Utf-8?B?TWljaGFlbA==?=, Apr 21, 2006
    #5
  6. Hi Again,
    Just wanted to let you know that I have it working, but I still had to catch
    the "@" parameter and delete it from the list. I still don't know why that
    one was there, if you do, please let me know. But at least its working now.
    By the way, I should not have to worry about some other out of the blue
    parameter that I will have to delete later, right?
    Thanks again Phillip for pointing me in the right direction.
    Michael
    =?Utf-8?B?TWljaGFlbA==?=, Apr 21, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UA==?=
    Replies:
    1
    Views:
    2,199
    Michelle Hlaing
    Nov 30, 2004
  2. Michael Kellogg
    Replies:
    0
    Views:
    863
    Michael Kellogg
    Apr 6, 2006
  3. H5N1
    Replies:
    0
    Views:
    2,698
  4. tstr
    Replies:
    0
    Views:
    349
  5. news.rcn.com
    Replies:
    2
    Views:
    1,103
    Roedy Green
    Dec 10, 2007
Loading...

Share This Page