Problem with editing a datagrid

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.

Thanks

--------------------------------------------

++++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
        dgUsers.DataBind()

    End Sub



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

<asp:DataGrid id="dgUsers" runat="server" AutoGenerateColumns="False">
                              <Columns>
                                    <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" HeaderText="ed" CancelText="Cancel"
                                          EditText="Edit"></asp:EditCommandColumn>
                                    <asp:BoundColumn DataField="username" ReadOnly="True" HeaderText="username"></asp:BoundColumn>
                                    <asp:TemplateColumn HeaderText="FullName">
                                          <ItemTemplate>
                                                <asp:TextBox ID=txtFullname Runat=server Text='<%# Databinder.Eval(Container.DataItem, "fullname") %>' Width="150px" />
                                          </ItemTemplate>
                                    </asp:TemplateColumn>
                                    <asp:TemplateColumn>
                                          <HeaderTemplate>
                                                HOU
                                          </HeaderTemplate>
                                          <ItemTemplate>
                                                <asp:checkbox id=chk1 runat="server" Enabled="False" Checked='<%# Container.DataItem("HOU") %>'>
                                                </asp:checkbox>
                                          </ItemTemplate>
                                          <EditItemTemplate>
                                                <asp:checkbox id="Checkbox1" runat="server" Enabled="True" Checked='<%# Container.DataItem("HOU") %>'>
                                                </asp:checkbox>
                                          </EditItemTemplate>
                                    </asp:TemplateColumn>
                                    <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>
                              </Columns>
                        </asp:DataGrid>



++++++ 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)

        Connect.Open()
        Dim count As Integer = myCommand.ExecuteNonQuery
        Connect.Close()

        Return (count = 1)

    End Function
JonsieAsked:
Who is Participating?
 
QingtongConnect With a Mentor Commented:
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
Qingtong


0
 
TorrwinCommented:
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:

E.G.  UPDATE MY_TABLE SET FULL_NAME = STEVE would be UPDATE MY_TABLE SET FULL_NAME = 'STEVE'
0
 
JonsieAuthor Commented:
Qingtong:
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
0
 
JonsieAuthor Commented:
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

Chris
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.