1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Deleting based off of an input Value in FormView?

Discussion in 'C#' started by WayTooAwesome, Jul 19, 2006.

  1. #1
    Howdy,

    I'm currently creating page where users can edit their own information, or, depending on if they're an admin or not, insert and delete new records. However, I have encountered a problem that even Google cannot fix (Le gasp!).

    I'm using a FormView, and since the user can also delete a record if they can insert them, I've decided to put a Linkbutton with CommandName="Delete" within the InsertItemTemplate. In theory, it the user will type the user-to-be-deleted's username into a textbox and click on the linkbutton, at which point that uisername will be run through my sqldatasource and its associated record will be deleted.

    Problem being, it doesn't work. When I click on the button that's supposed to delete it, nothing happens at all except for the quick little postback. I've tried all sorts of stuff, but none of it has worked. Here's my frontend code:

              <asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" DataSourceID="SqlDataSource1" OnDataBound="dudelol">
                <EditItemTemplate>
    
                <!-- EditItemTemplate Stuffs -->
                
                </EditItemTemplate>
                <ItemTemplate>
    
                <!-- ItemTemplate stuffs -->
                </ItemTemplate>
    
                <InsertItemTemplate>
    
                <!-- InsertItemTemplate stuffs -->
    
                <tr>
                    <td colspan="2">
                        <br /><asp:Label Text="Delete" runat="server" ID="DeleteLabel" Visible="false"></asp:Label><br />
                        <asp:Label runat="server" Text="Username: " ID="DeleteUsername" Visible="false"></asp:Label>
                        <asp:TextBox ID="Deletebox" runat="server" Text='<%# Bind("UsernameDelete") %>' Visible="false"></asp:TextBox><br />
                        <asp:LinkButton ID="Delete" runat="server" Text="Delete" CommandName="Delete" Visible="false"></asp:LinkButton>
                    </td>
                </tr>                    
                </InsertItemTemplate>
              </asp:FormView>
    Code (markup):
    And here's my SqlDataSource:

            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString %>"
                SelectCommand="SELECT * FROM [Employee] e WHERE (([Username] = @Username) AND ([Password] = @Password))" DeleteCommand="DELETE FROM [Employee] WHERE Username = @UsernameDelete" InsertCommand="EmployeeInsert" InsertCommandType="StoredProcedure" UpdateCommand="This is the massive update command that's not necessary">
                <SelectParameters>
                    <asp:SessionParameter Name="Username" SessionField="Username" Type="String" />
                    <asp:SessionParameter Name="Password" SessionField="Password" Type="String" />
                </SelectParameters>
                <UpdateParameters>
                    <!-- Update Parameters -->
                </UpdateParameters>
                <DeleteParameters>
                    <asp:Parameter Name="UsernameDelete" Type="String" />
                </DeleteParameters>
                <InsertParameters>
                    <asp:Parameter Name="ManagerID" Type="Decimal" />
                    <asp:Parameter Name="Rank" Type="String" />
                    <asp:Parameter Name="Admin" Type="Decimal" />
                    <asp:Parameter Name="FName" Type="String" />
                    <asp:Parameter Name="LName" Type="String" />
                    <asp:Parameter Name="Title" Type="String" />
                    <asp:Parameter Name="Phone" Type="String" />
                    <asp:Parameter Name="URL" Type="String" />
                    <asp:Parameter Name="Responsibilities" Type="String" />
                    <asp:Parameter Name="MonthlyGoals" Type="String" />
                    <asp:Parameter Name="Misc" Type="String" />
                    <asp:Parameter Name="Picture" Type="String" />
                    <asp:Parameter Name="Username" Type="String" />
                    <asp:Parameter Name="Password" Type="String" />
                </InsertParameters>
            </asp:SqlDataSource>
    Code (markup):
    Any suggestions would be greatly appreciated. =D

    Thanks!
     
    WayTooAwesome, Jul 19, 2006 IP
  2. Darrin

    Darrin Peon

    Messages:
    123
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Where is the event handler for the Delete button?

    Do you have some server side code that looks like this:

    this.Delete.Click += new System.EventHandler(this.Delete_Click);

    If not, what happens in the click event that calls a method to do the deleting?
     
    Darrin, Jul 19, 2006 IP
  3. WayTooAwesome

    WayTooAwesome Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I'm pretty new to this kinda stuff, but when I was having it Delete the record it was currently viewing, the CommandName="Delete" portion of the Linkbutton would run the DeleteCommand.

    Do I need to run it through an eventhandler to somehow change the delete query?
     
    WayTooAwesome, Jul 19, 2006 IP
  4. WayTooAwesome

    WayTooAwesome Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Alright, what you said gave me the idea of setting the query to a variable in my backend... I think it's a step in the right direction, but it still doesn't work. Here's what I changed:

    For this part, I only really changed the Deletebox's Text attribute, so that it wasn't bound to anything.

                        <br /><asp:Label Text="Delete" runat="server" ID="DeleteLabel"></asp:Label><br />
                        <asp:Label runat="server" Text="Username: " ID="DeleteUsername"></asp:Label>
                        <asp:TextBox ID="Deletebox" runat="server" Text=""></asp:TextBox><br />
                        <asp:LinkButton ID="Delete" runat="server" Text="Delete" CommandName="Delete" OnClientClick="return confirm('Delete Record?');"></asp:LinkButton>
    Code (markup):
    Here's what I changed the DeleteCommand to in my SqlDataSource

    DeleteCommand="DELETE FROM [Employee] WHERE Username = '<%# DeleteUser() %>'"
    Code (markup):
    And this is the String I created for the DeleteCommand in my backend:

        protected String DeleteUser()
        {
            String Username = ((TextBox)FormView1.FindControl("Deletebox")).Text;
    
            return (Username);
        }
    Code (markup):

    Thanks.
     
    WayTooAwesome, Jul 19, 2006 IP
  5. Darrin

    Darrin Peon

    Messages:
    123
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I see.

    Yes, I think you will still need to somehow inform your datasource that you deleted something in the postback.

    Also you may want to check that you form is not being reloaded on the postbacks as well. If it is loaded up like new on the postback during the form load event or sooner, you will lose the post back data that applied to the delete, if that makes sense??
     
    Darrin, Jul 19, 2006 IP
  6. WayTooAwesome

    WayTooAwesome Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I may be interpreting you incorrectly here, but you're saying that it's posting before the string pulls the data and thusly it's grabbing a whole lot of nothing?

    I tested this out by putting a Response.Write(Username) within that protected String DeleteUser() , and nothing was written on the page after I used my LinkButton... Does that mean that you're correct, or that I'm a retard for putting a Response.Write within a function that is unable to do such a thing? ^_~
     
    WayTooAwesome, Jul 19, 2006 IP
  7. Darrin

    Darrin Peon

    Messages:
    123
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Ha! Well, I certainly don't think you're a retard, but that doesn't necessarily mean I am correct either. ;-)

    I think there are a couple things to check out here.

    Before that, if you want to check values like that quickly, try System.Diagnostics.Debug.WriteLine("text to write"); (Freehand, case may not be correct... I can't do anything without intellisense anymore...).

    One is too see when the data is reloading on your postback. In your server side code, do you call a databind() on your FormView to bind the datasource to it? If so, does this happen everytime the page loads, or only if(!isPostback)? The page load will fire before any postback events, so if the data is bound to the formview control before the postback events for delete happen, you will lose any data in the viewstate about what was supposed to be deleted.

    The other is the actual delete event. I always code in more of my own stuff instead of taking full advantage of the formview and bound data, so maybe the event is not required, it's just built in. If not, you would have to look at the events on the form view or the button and have an event tied to the click event of the button or some delete event of the formview.
     
    Darrin, Jul 19, 2006 IP
  8. WayTooAwesome

    WayTooAwesome Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Alright, I threw that WriteLine within my string DeleteUser() (I'm still not sure if that's even supposed to work), but nothing was written.

    The FormView is bound to the DataSource through its "DataSourceID" attribute, and I know that's workin' because there are no problems with viewing, inserting, or updating.

    How the DeleteCommand is supposed to work (From what I've gathered), is by just clicking a button and it deletes whatever record you're currently bound to - as opposed to deleting a record based off of the value of a Textbox.

    It sounds like I may have to code this one out, but I have no idea how to do that, so any help would be greatly appreciated. ^_^

    Thanks.
     
    WayTooAwesome, Jul 20, 2006 IP
  9. WayTooAwesome

    WayTooAwesome Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Just an update...

    I tried adding an OnClick method that would set a Session variable, hoping that the OnClick would trigger before the CommandName... Apparently it doesn't, because when I call it in the function for my DeleteCommand, it comes up as null.
     
    WayTooAwesome, Jul 20, 2006 IP
  10. Niggy_G

    Niggy_G Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I've just been banging my head against a wall for ages trying to figure out why the delete command on a formView would not work.

    I examined one working formView against the broken one, the difference between the two was that the working one used an SqlDataSource with the following property: OldValuesParameterFormatString="original_{0}

    When I added the property to the SqlDataSource in use by the other formView the delete command started working.

    Make sure you have this property in your SqlDataSource if you are using delete queries

    Hope this helps.
     
    Niggy_G, Feb 9, 2007 IP