Solved

Why does Update and Delete not work?

Posted on 2006-10-31
3
2,749 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

820 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