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("chris jones", "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(ByVa l source As Object, ByVal e As System.Web.UI.WebControls. DataGridCo mmandEvent Args) 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("txtFull name")
general.UpdateUsers(userna me, 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:Edit CommandCol umn>
<asp:BoundColumn DataField="username" ReadOnly="True" HeaderText="username"></as p:BoundCol umn>
<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:Bound Column>
<asp:BoundColumn DataField="02"></asp:Bound Column>
<asp:BoundColumn DataField="03"></asp:Bound Column>
<asp:BoundColumn DataField="04"></asp:Bound Column>
<asp:BoundColumn DataField="05"></asp:Bound Column>
<asp:BoundColumn DataField="06"></asp:Bound Column>
<asp:BoundColumn DataField="07"></asp:Bound Column>
<asp:BoundColumn DataField="08"></asp:Bound Column>
<asp:BoundColumn DataField="09"></asp:Bound Column>
<asp:BoundColumn DataField="INT"></asp:Boun dColumn>
</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
My update routine now works correctly if i set the function manually
e.g. general.UpdateUsers("chris
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(ByVa
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("txtFull
general.UpdateUsers(userna
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:Edit
<asp:BoundColumn DataField="username" ReadOnly="True" HeaderText="username"></as
<asp:TemplateColumn HeaderText="FullName">
<ItemTemplate>
<asp:TextBox ID=txtFullname Runat=server Text='<%# Databinder.Eval(Container.
</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:Bound
<asp:BoundColumn DataField="02"></asp:Bound
<asp:BoundColumn DataField="03"></asp:Bound
<asp:BoundColumn DataField="04"></asp:Bound
<asp:BoundColumn DataField="05"></asp:Bound
<asp:BoundColumn DataField="06"></asp:Bound
<asp:BoundColumn DataField="07"></asp:Bound
<asp:BoundColumn DataField="08"></asp:Bound
<asp:BoundColumn DataField="09"></asp:Bound
<asp:BoundColumn DataField="INT"></asp:Boun
</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("
myCommand.Parameters.Add("
myCommand.Parameters.Add("
myCommand.Parameters.Add("
myCommand.Parameters.Add("
myCommand.Parameters.Add("
myCommand.Parameters.Add("
myCommand.Parameters.Add("
myCommand.Parameters.Add("
myCommand.Parameters.Add("
myCommand.Parameters.Add("
myCommand.Parameters.Add("
myCommand.Parameters.Add("
Connect.Open()
Dim count As Integer = myCommand.ExecuteNonQuery
Connect.Close()
Return (count = 1)
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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
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
E.G. UPDATE MY_TABLE SET FULL_NAME = STEVE would be UPDATE MY_TABLE SET FULL_NAME = 'STEVE'