asp.net C# update page updates all rows with same information

SystemApe
SystemApe used Ask the Experts™
on
Created an update page that is listing a row based on ID
Update goes through fine and but it duplicates itself to all rows except for the ID since its not in the UPDATE statement...below is the code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ReferenceUpdate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ReferenceView_ItemUpdate(object sender,
        DetailsViewUpdatedEventArgs e)
    {
        if (e.AffectedRows == 1)
        {
            Response.Redirect("~/ReferenceView.aspx");
        }
    }
    protected void ReferenceView_ItemCommand(object sender,
        DetailsViewCommandEventArgs e)
    {
        if (e.CommandName == "Cancel")
        {
            Response.Redirect("~/ReferenceView.aspx");
        }
    }
}

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ReferenceUpdate.aspx.cs" Inherits="ReferenceUpdate" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
 
    <form id="form1" runat="server">
 
    <div>
    <p >
    
        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
            DataSourceID="SqlDataSource1" Height="50px" Width="125px" 
            CellPadding="4" ForeColor="#333333" GridLines="None" DefaultMode="Edit"
            OnItemUpdated="ReferenceView_ItemUpdate" 
            OnItemCommand="ReferenceView_ItemCommand">
            <FooterStyle BackColor="#1C5E55" ForeColor="White" Font-Bold="True" />
            <CommandRowStyle BackColor="#C5BBAF" Font-Bold="True" />
            <RowStyle BackColor="#E3EAEB" />
            <FieldHeaderStyle BackColor="#D0D0D0" Font-Bold="True" />
            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
            <Fields>
                <asp:BoundField DataField="url" HeaderText="url" SortExpression="url" />
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" 
                    SortExpression="CategoryID" />
                <asp:BoundField DataField="ReferenceID" HeaderText="ReferenceID" 
                    SortExpression="ReferenceID" />
                    <asp:CommandField ShowEditButton="True" />
                    </Fields>
            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#7C6F57" />
            <AlternatingRowStyle BackColor="White" />
        </asp:DetailsView>
    
    </p>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:H:\DOTNET\APP_DATA\LIBRARY.MDFConnectionString1 %>" 
        ProviderName="<%$ ConnectionStrings:H:\DOTNET\APP_DATA\LIBRARY.MDFConnectionString1.ProviderName %>" 
        SelectCommand="SELECT [url], [Title], [CategoryID], [ReferenceID] FROM [References] WHERE [ReferenceID] = @ReferenceID" 
        UpdateCommand="UPDATE [References] SET [url] = @url, [Title] = @Title, [CategoryID] = @CategoryID">
        <SelectParameters>
            <asp:QueryStringParameter Name="ReferenceID"
                QueryStringField="ReferenceID" Type="Char" />
        </SelectParameters>
    </asp:SqlDataSource>
    </div>
    
 
    </form>
    
 
</body>
</html>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
>> UpdateCommand="UPDATE [References] SET [url] = @url, [Title] = @Title, [CategoryID] = @CategoryID"

I expect you will need a WHERE clause inside your SQL statement and <UpdateParameters> in your ASPX code.  Since this is a details view, it will probably be an <asp:QueryStringParameter>

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.update.aspx
http://geekswithblogs.net/AzamSharp/archive/2006/08/30/89789.aspx

Possibly something like:

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:H:\DOTNET\APP_DATA\LIBRARY.MDFConnectionString1 %>" 
        ProviderName="<%$ ConnectionStrings:H:\DOTNET\APP_DATA\LIBRARY.MDFConnectionString1.ProviderName %>" 
        SelectCommand="SELECT [url], [Title], [CategoryID], [ReferenceID] FROM [References] WHERE [ReferenceID] = @ReferenceID" 
        UpdateCommand="UPDATE [References] SET [url] = @url, [Title] = @Title, [CategoryID] = @CategoryID WHERE [ReferenceID] = @ReferenceID">
        <SelectParameters>
            <asp:QueryStringParameter Name="ReferenceID"
                QueryStringField="ReferenceID" Type="Char" />
        </SelectParameters>
        <UpdateParameters>
            <asp:QueryStringParameter Name="ReferenceID"
                QueryStringField="ReferenceID" Type="Char" />
        </UpdateParameters>
    </asp:SqlDataSource>

Open in new window

Author

Commented:
Mad Genius!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial