-2

I have a table that has Edit and Delete links in each row. I am supposed to be able to click either Edit or Delete and it would do it. The edit link works but the delete has this error:

Deleting is not supported by data source 'SqlDataSource1' unless DeleteCommand is specified.

My tables data source is sqlDataSource1.

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:PayrollSystem_DBConnectionString %>" 
        ProviderName="<%$ ConnectionStrings:PayrollSystem_DBConnectionString.ProviderName %>"           
        SelectCommand="SELECT [UserID], [UserName], [UserPassword], [SecurityLevel] FROM [tblUserLogin]">
    </asp:SqlDataSource>

</div>
    <div align="center">
    <asp:Label ID="Label1" runat="server" Text="Manage Users"></asp:Label>
<p>
    <asp:Label ID="Label2" runat="server" Text="User Name:"></asp:Label>
    <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
</p>
<p>
    <asp:Label ID="Label3" runat="server" Text="Password:"></asp:Label>
    <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
</p>
        <p>
            <asp:Label ID="Label4" runat="server" Text="Security Level:"></asp:Label>
            <asp:DropDownList ID="drpdwnlstSecurityLevel" runat="server" 
                onselectedindexchanged="drpdwnlstSecurityLevel_SelectedIndexChanged">
                <asp:ListItem>A</asp:ListItem>
                <asp:ListItem>U</asp:ListItem>
            </asp:DropDownList>
</p>
        <p>
            <asp:Button ID="btnAddUser" runat="server" onclick="btnAddUser_Click1" 
    Text="Add User" /> 


</p>
        <p>
            <asp:Label ID="lblError" runat="server"></asp:Label>
</p>

    </div>
<p>
    &nbsp;</p>
            <div align="center">
<asp:GridView ID="tblUserLogin" runat="server" AutoGenerateColumns="False" 
    DataSourceID="AccessDataSource1">
    <Columns>
        <asp:BoundField DataField="UserID" HeaderText="UserID" InsertVisible="False" 
            SortExpression="UserID"></asp:BoundField>
        <asp:BoundField DataField="UserName" HeaderText="UserName" 
            SortExpression="UserName"></asp:BoundField>
        <asp:BoundField DataField="UserPassword" HeaderText="UserPassword" 
            SortExpression="UserPassword"></asp:BoundField>
        <asp:BoundField DataField="SecurityLevel" HeaderText="SecurityLevel" 
            SortExpression="SecurityLevel"></asp:BoundField>
        <asp:CommandField ShowEditButton="True"></asp:CommandField>
        <asp:CommandField ShowDeleteButton="True"></asp:CommandField>




    </Columns>
</asp:GridView>
                </form>
</body>

Mike
  • 2,293
  • 13
  • 42
  • 56
  • 2
    a simple google search would have probably given you the answer? – BrokenGlass Nov 30 '11 at 19:08
  • 1
    You didn't specify your delete parameter. Where is your UserID coming from? Is the DataKey specified in your control. Don't worry about the down vote. I'll up vote you in the morning as I am already out of votes. No one should be down voted for asking a question. – gsirianni Nov 30 '11 at 20:33
  • You didn't specify your delete parameter. Where is your UserID coming from? Is the DataKey specified in your control. Don't worry about the down vote. I'll up vote you in the morning as I am already out of votes. No one should be down voted for asking a question. – gsirianni Nov 30 '11 at 20:33
  • Also, what type of CompositeControl are you using? GriView, DataGrid...? – gsirianni Nov 30 '11 at 20:33
  • @BrokenGlass: the problem was a missing primary key in the database. That's why the Update, Insert and Delete commands could not be generated. Quite unfair to downvote such a question – slfan Nov 30 '11 at 22:11
  • Given that by now more info was added to the question I have removed my downvote. I do stand by that initial downvote though - the question only gave basic information about the problem, no way to reproduce it and didn't show any effort or research of the person asking the question. *It deserved to get downvoted* – BrokenGlass Nov 30 '11 at 22:35
  • @BrokenGlass: Thanks for removing the downvote. The poor guy had no clue about SqlDataSource and how to use it. Sometimes it is difficult to ask the right question. I agree with you that at the beginning the question wasn't clear, but he with his knowledge he wasn't able to google the answer. He used the wizard to generate the code, but didn't understand it. I spent an hour to find out what his real problem was and help him to solve it for a job that normally takes me 30s. – slfan Dec 01 '11 at 19:44

1 Answers1

1

The table doesn't have a primary key, that's why you could not autogenerate the update and delete statments.

It's probably something like this:

<asp:SqlDataSource
    id="AccessDataSource1"
    runat="server"
    DataSourceMode="DataSet"
    ConnectionString="<%$ ConnectionStrings:PayrollSystem_DBConnectionString %>"  
    SelectCommand="SELECT [UserID], [UserName], [UserPassword], [SecurityLevel] FROM tblUserLogin"
    DeleteCommand="DELETE FROM [tblUserLogin] WHERE UserID=@UserID">
 <DeleteParameters>
    <asp:Parameter Name="UserID" Type="Int32" />
 </DeleteParameters>

</asp:SqlDataSource>

You have to add a DeleteCommand to your SqlDataSource. Of course you have to change the ConnectionString and the tableNames. In your DataGridView you have a CommandField to show the DeleteButton. That's why your SqlDataSource requires a DeleteCommand.

If you want to do it by the wizard, you can configure the SqlDataSource by clicking on the little arrow on top right. Under advanced options you can generate insert, update and delete commands, because you probably need them as well.

There is a nice tutorial about SqlDataSource

slfan
  • 8,950
  • 115
  • 65
  • 78
  • Thanks, I added my html code above. I'm not sure what or where to add. – Mike Nov 30 '11 at 19:45
  • Sorry, I think I have it all now. – Mike Nov 30 '11 at 19:52
  • I tried inserting it, but I think I either placed it in the wrong spot or I need to replace something. I was getting errors. Where should I place it? Thanks – Mike Nov 30 '11 at 19:59
  • you have to place the DeleteCommand inside the element just after the SelectCommand as I showed in my code. The error message might be because the Parameter UserID is not declared yet. I will add some code for this, but as I said you better autogenerate everything because you might need update and insert as well. – slfan Nov 30 '11 at 20:04
  • I changed my code above to show what I did. Now when I run it I get an error that says: Keyword not supported: 'provider' – Mike Nov 30 '11 at 20:15
  • In your original code you had a provider, I can't see it anylonger. The provider should be in the connection string in your web.config. Maybe the connection string is wrong. – slfan Nov 30 '11 at 20:19
  • I changed it above. I have three errors. 1. The server tag is not well formed. 2. Literal expressions like %$ ConnectionStrings:PayrollSystem_DBConnectionString %>" are not allowed. 3. %$ ConnectionStrings:PayrollSystem_DBConnectionString.ProviderName %>" are not allowed. – Mike Nov 30 '11 at 20:27
  • You have two opening SqlDataSource Controls in your listing! One is enough. Delete the first three lines in your displayed code. – slfan Nov 30 '11 at 20:34
  • Sorry to be a bother. I was reading through the link you sent. I'm just not getting it. I posted my original html code above. Can you input your code into it so I can see where it goes? Thanks so much for your time. – Mike Nov 30 '11 at 20:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5477/discussion-between-slfan-and-mike) – slfan Nov 30 '11 at 20:59