[Webinar] Learn how to a build a cloud-first strategyRegister Now


Problem with editing a datagrid

Posted on 2005-04-18
Medium Priority
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
  • 2

Accepted Solution

Qingtong earned 375 total points
ID: 13807131
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

ID: 13808938
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

ID: 13813597
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

ID: 13814084
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


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month20 days, 13 hours left to enroll

864 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