Solved

Why does Update and Delete not work?

Posted on 2006-10-31
3
2,744 Views
Last Modified: 2007-11-27
I am trying my first test in ASP.Net, but have a problem with the delete and update commands. The Select and Insert routines work as expected.

1. When I click the Edit link, it opens the editable view as planned.  But when I click the Update link, it closes the editable form without complaining, but does not update the data.  

2. When I click the Delete link, an error screen appears with the message "Must declare the scalar variable "@id".  But the id parameter is declared in the <DeleteParameters> section.

Any suggestions on either problem?  The page code is listed below.

Thanks.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!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 runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestConn %>"
            DeleteCommand="DELETE Members WHERE id=@id"
            InsertCommand="INSERT INTO Members([First_Name], [Last_Name], [Role_Id]) VALUES (@First_Name,@Last_Name,@Role_Id)"
            SelectCommand="SELECT Members.id, Members.First_Name, Members.Last_Name, Roles.Role, Members.Role_Id FROM Members INNER JOIN Roles ON Members.Role_Id = Roles.id ORDER BY Members.id"
            UpdateCommand="UPDATE Members SET First_Name=@First_Name, Last_Name=@Last_Name, Role_Id=@Role_ID WHERE id=@id">
            <DeleteParameters>
                <asp:Parameter Name="id" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="First_Name" />
                <asp:Parameter Name="Last_Name" />
                <asp:Parameter Name="Role_ID" />
                <asp:Parameter Name="id" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="First_Name" />
                <asp:Parameter Name="Last_Name" />
                <asp:Parameter Name="Role_Id" />
            </InsertParameters>
        </asp:SqlDataSource>
   
    </div>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:TestConn %>"
            SelectCommand="SELECT [id], [Role] FROM [Roles]"></asp:SqlDataSource>
        <asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False"
            DataSourceID="SqlDataSource1" Height="50px" Width="125px">
            <Fields>
                <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
                    SortExpression="id" />
                <asp:BoundField DataField="First_Name" HeaderText="First_Name" SortExpression="First_Name" />
                <asp:BoundField DataField="Last_Name" HeaderText="Last_Name" SortExpression="Last_Name" />
                <asp:BoundField DataField="Role" HeaderText="Role" SortExpression="Role" />
                <asp:TemplateField HeaderText="Role Type" SortExpression="Role_Id">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2"
                            DataTextField="Role" DataValueField="id" SelectedValue='<%# Bind("Role_Id") %>'>
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <InsertItemTemplate>
                        &nbsp;<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2"
                            DataTextField="Role" DataValueField="id" SelectedValue='<%# Bind("Role_Id") %>'>
                        </asp:DropDownList>
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Role") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
            </Fields>
        </asp:DetailsView>
    </form>
</body>
</html>
0
Comment
Question by:Roger_Hanggi
  • 2
3 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 125 total points
ID: 17843496
The SqlDataSource object can derive parameters from the SelectCommand.  Try not specifying the InsertCommand, DeleteCommand, and UpdateCommand, and their matching parameter lists.

Bob
0
 

Author Comment

by:Roger_Hanggi
ID: 17852157
Bob, I tried an example of using a SQLDataSource without the InsertCommand, DeleteCommand, and UpdateCommand (or their parameters).  I then added a DetailsView and AutoGenerate options for insert, delete and update buttons to True to display the links for these functions.  But when I click any of these, I receive the error message "Updating is not supported by data source 'SqlDataSource1' unless UpdateCommand [or DeleteCommand or InsertCommand] is specified. "  Any ideas?
0
 

Author Comment

by:Roger_Hanggi
ID: 17854849
I found the answer in searching through MS documents.  I had to set the ConflictDetection property for the SQLDataSource1 to CompareAllValues, and also the OldValuesParameterFormatString property to old_{0}.  Then I could change the command strings for the update and delete commands to:
DeleteCommand="DELETE Members WHERE id=@old_id"
UpdateCommand="UPDATE Members SET First_Name=@First_Name, ... WHERE id=@old_id"

Now it works.  But TheLearnedOne is correct that I could delete the parameters for all the commands.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now