By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,767 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

Gridview and Updating

P: n/a
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

Apr 20 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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...spx#twowaybind
http://www.webswapp.com/codesamples/.../gridview.aspx
http://www.webswapp.com/codesamples/...s/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

Apr 20 '06 #2

P: n/a
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.SqlDataSourceCommandEven tArgs) 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

Apr 21 '06 #3

P: n/a
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...spx#twowaybind
http://www.webswapp.com/codesamples/.../gridview.aspx
http://www.webswapp.com/codesamples/...s/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

Apr 21 '06 #4

P: n/a
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...spx#twowaybind
http://www.webswapp.com/codesamples/.../gridview.aspx
http://www.webswapp.com/codesamples/...s/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

Apr 21 '06 #5

P: n/a
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

Apr 21 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.