Problem with editing a datagrid

Posted on 2005-04-18
Last Modified: 2012-05-05
I'm having trouble getting the data that i change in a datagrid to update the database.

My update routine now works correctly if i set the function manually

e.g. general.UpdateUsers("chrisjones", "Chris Jones", False, False, False, False, False, False, False, True, False, True, False)

However when i try and apply the same technique to the cells in the datagrid i am getting nothing.

Here is the code.



++++Update command+++++

    Private Sub dgUsers_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgUsers.UpdateCommand

        Dim username = e.Item.Cells(1).Text

        Dim i As Integer
        Dim _item As DataGridItem
        Dim dr As DataRow

        i = dgUsers.EditItemIndex

        _item = dgUsers.Items(i)
        Dim fullnameTB As TextBox = _item.FindControl("txtFullname")

        general.UpdateUsers(username, fullnameTB.Text, False, False, False, False, False, False, False, True, False, True, False)

        dgUsers.EditItemIndex = -1

    End Sub

++++++ HTML part ++++++

<asp:DataGrid id="dgUsers" runat="server" AutoGenerateColumns="False">
                                    <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" HeaderText="ed" CancelText="Cancel"
                                    <asp:BoundColumn DataField="username" ReadOnly="True" HeaderText="username"></asp:BoundColumn>
                                    <asp:TemplateColumn HeaderText="FullName">
                                                <asp:TextBox ID=txtFullname Runat=server Text='<%# Databinder.Eval(Container.DataItem, "fullname") %>' Width="150px" />
                                                <asp:checkbox id=chk1 runat="server" Enabled="False" Checked='<%# Container.DataItem("HOU") %>'>
                                                <asp:checkbox id="Checkbox1" runat="server" Enabled="True" Checked='<%# Container.DataItem("HOU") %>'>
                                    <asp:BoundColumn DataField="01"></asp:BoundColumn>
                                    <asp:BoundColumn DataField="02"></asp:BoundColumn>
                                    <asp:BoundColumn DataField="03"></asp:BoundColumn>
                                    <asp:BoundColumn DataField="04"></asp:BoundColumn>
                                    <asp:BoundColumn DataField="05"></asp:BoundColumn>
                                    <asp:BoundColumn DataField="06"></asp:BoundColumn>
                                    <asp:BoundColumn DataField="07"></asp:BoundColumn>
                                    <asp:BoundColumn DataField="08"></asp:BoundColumn>
                                    <asp:BoundColumn DataField="09"></asp:BoundColumn>
                                    <asp:BoundColumn DataField="INT"></asp:BoundColumn>

++++++ SQL part +++++

   Public Shared Function UpdateUsers(ByVal username As String, ByVal fullname As String, ByVal HOU As Boolean, ByVal T01 As Boolean, ByVal T02 As Boolean, ByVal T03 As Boolean, ByVal T04 As Boolean, ByVal T05 As Boolean, ByVal T06 As Boolean, ByVal T07 As Boolean, ByVal T08 As Boolean, ByVal T09 As Boolean, ByVal INT As Boolean) As Boolean

        Dim Connect As New SqlConnection(DB())
        Dim myCommand As SqlCommand = Connect.CreateCommand()

        myCommand.CommandText = "UPDATE users SET fullname = @f1, HOU = @h1, [01] = @p1, [02] = @p2, [03] = @p3, [04] = @p4, [05] = @p5, [06] = @p6, [07] = @p7, [08] = @p8, [09] = @p9, INT = @i1 " & _
                                "WHERE " & _
                                "(username = @u1)"

        myCommand.Parameters.Add("@f1", fullname)
        myCommand.Parameters.Add("@h1", HOU)
        myCommand.Parameters.Add("@p1", T01)
        myCommand.Parameters.Add("@p2", T02)
        myCommand.Parameters.Add("@p3", T03)
        myCommand.Parameters.Add("@p4", T04)
        myCommand.Parameters.Add("@p5", T05)
        myCommand.Parameters.Add("@p6", T06)
        myCommand.Parameters.Add("@p7", T07)
        myCommand.Parameters.Add("@p8", T08)
        myCommand.Parameters.Add("@p9", T09)
        myCommand.Parameters.Add("@i1", INT)
        myCommand.Parameters.Add("@u1", username)

        Dim count As Integer = myCommand.ExecuteNonQuery

        Return (count = 1)

    End Function
Question by:Jonsie
    LVL 5

    Accepted Solution

    Try it out:

    delete the line

    Dim username = e.Item.Cells(1).Text

    change the line to

    Dim username =_item.Cells(1).Text

    after you defined the _item.

    Good Luck

    LVL 13

    Expert Comment

    Well, for one thing, in your manual statement you have the ' marks around your string variables.  In the code you want the computer to execute, you do not have the ' marks around your string variables:


    Author Comment

    I get the folowing error when changing to your method

    Object reference not set to an instance of an object.

    for this line:
    Line 75:         Dim username = _item.Cells(1).Text

    Author Comment

    I've managed to sort out the problem myself but have awarded the points to Qingtong as i feel he contributed more.

    The problem was missing a vital line in my page load and edit command functions.

    I was not performing the post back checks in the page load and also not performing the datasource in the edit command.

    Together it meant i was editing nothing!

    Thanks for your help anyway


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    779 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

    9 Experts available now in Live!

    Get 1:1 Help Now