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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with DetailsView and UpdateCommand

Status
Not open for further replies.

Techdawgs

Programmer
Nov 12, 2004
14
US
I am trying to update the data in my database using the updatecommand in details view but have not had any luck. Everytime I run it I don't get any errors but it does not process the update. I have tried several different variations of the code and keep running into dead ends.

Here is my code:
<%@ Page MasterPageFile="Classy.master" Explicit="True" Language="VB" Debug="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

<asp:Content runat="server" ID="HeaderContent" ContentPlaceHolderID="PageHeader">
Edit Ad</asp:Content>

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="Body" >

<script runat="server">
Protected Sub DetailsView1_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdatedEventArgs)
Response.Redirect("default.aspx")
End Sub

Protected Sub DetailsView1_ItemDeleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewDeletedEventArgs)
Response.Redirect("default.aspx")
End Sub

Protected Sub DetailsView1_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewCommandEventArgs)
If e.CommandName = "Cancel" Then
Response.Redirect("default.aspx")
End If
End Sub
</script>

To make changes, click Edit, make your changes, then click Update. To delete
this ad, just click the Delete button.
<br />
<br />
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="100%" AutoGenerateRows="False" DataKeyNames="AdNum" DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333" GridLines="None" OnItemUpdated="DetailsView1_ItemUpdated" OnItemDeleted="DetailsView1_ItemDeleted" OnItemCommand="DetailsView1_ItemCommand">
<Fields>
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Category" HeaderText="Category" SortExpression="Category" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
<asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
<asp:BoundField DataField="UserPassword" HeaderText="UserPassword" SortExpression="UserPassword" />
<asp:CommandField ButtonType="Button" ShowDeleteButton="True" ShowEditButton="True" />
</Fields>
<RowStyle BackColor="#FFFBD6" />
<FieldHeaderStyle BackColor="#FFFF99" Font-Bold="True" />
</asp:DetailsView>
&nbsp;
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:classydbConnectionString %>"
DeleteCommand="DELETE FROM [Ads] WHERE [AdNum] = ? AND [Title] = ? AND [Category] = ? AND [Description] = ? AND [Price] = ? AND [Phone] = ? AND = ? AND [State] = ? AND [UserPassword] = ?"
ProviderName="<%$ ConnectionStrings:classydbConnectionString.ProviderName %>"
SelectCommand="SELECT [AdNum], [Title], [Category], [Description], [Price], [Phone], [Email], [State], [UserPassword] FROM [Ads] WHERE ([AdNum] = ?)"
UpdateCommand="UPDATE [Ads] SET [Title] = @Title, [Category] = @Category, [Description] = @Description, [Price] = @Price, [Phone] = @Phone, [Email] = @Email, [State] = @State, [UserPassword] = @UserPassword WHERE [AdNum] = @AdNum">
<SelectParameters>
<asp:QueryStringParameter Name="AdNum" QueryStringField="AdNum" Type="Int32" />
</SelectParameters>
<DeleteParameters>
<asp:parameter Name="AdNum" Type="Int32" />
<asp:parameter Name="Title" Type="String" />
<asp:parameter Name="Category" Type="String" />
<asp:parameter Name="Description" Type="String" />
<asp:parameter Name="Price" Type="Decimal" />
<asp:parameter Name="Phone" Type="String" />
<asp:parameter Name="Email" Type="String" />
<asp:parameter Name="State" Type="String" />
<asp:parameter Name="UserPassword" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="Title" Type="String" />
<asp:parameter Name="Category" Type="String" />
<asp:parameter Name="Description" Type="String" />
<asp:parameter Name="Price" Type="Decimal" />
<asp:parameter Name="Phone" Type="String" />
<asp:parameter Name="Email" Type="String" />
<asp:parameter Name="State" Type="String" />
<asp:parameter Name="UserPassword" Type="String" />
<asp:parameter Name="AdNum" Type="Int32" />
</UpdateParameters>

</asp:SqlDataSource>

</asp:content>
 
Are you creating the code or letting the sqldatasource wizard create it? What database are you using? The delete command looks suspicious.

JIm
 
I am actually teaching myself ASP 2.0. This code came from the book that I am using and I am typing it and debugging it using Visual Web Developer Express Edition. It is an Access database.
 
Try creating another datasource and use the wizard. Then you can compare what it generates to your code and see any differences.

Jim
 
Another minor point which might hellp Techdawgs. Keep in mind too (and I find this helpful) that following Jim's suggestion you can always reduce the number of fields to one or two until the 'thing' is working. I find (not sure about others) that always starting form the "simplest" level and working outward (and utilzing the process of elmination in the other direction) is good advice.
 
Thanks for everyones help. I went back and used the datasource wizard and found my missing code.

OldValuesParameterFormatString="original_{0}" ProviderName="<%$ ConnectionString:DataConnectionString.ProviderName %>"

I needed something to tell the server how to label the old data.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top