Link to home
Start Free TrialLog in
Avatar of Jonsie
Jonsie

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Qingtong
Qingtong

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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'
Avatar of Jonsie
Jonsie

ASKER

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
Avatar of Jonsie

ASKER

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