Gridview and Updating

G

Guest

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
 
G

Guest

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
 
G

Guest

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
 
G

Guest

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
 
G

Guest

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top