Solved

Why does Update and Delete not work?

Posted on 2006-10-31
3
2,752 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Form Data Disappears Using Back Button 4 65
"Emulate" TAB key when press Enter Key 3 91
asp.net web app 3 57
Send parameters via datatable ajax call 4 42
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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