VeeVan
asked on
ASP.NET DataGrid Update Changes Not Working
I have a simple datagrid that has info in it. I want to enable inline editing in the grid.
When I click Edit, I can edit my pages easily enough. When I click update, however, the changes go away. I have looked at my code over and over again, and can't see what's wrong. I am sure it's something really simple. Any help would be greatly appreciated.
Here is the update code:
========================== ========== ========== ========== ========== ====
Public Sub DataGrid_Update(ByVal source As Object, ByVal e As DataGridCommandEventArgs)
Dim strConn As String = System.Configuration.Confi gurationSe ttings.App Settings(" Connection String")
Dim connUACDC As New SqlConnection(strConn)
connUACDC.Open()
Dim strCommand As String = "UPDATE tblBoard SET Name = @Name, Title = @Title, DisplayOrder = @Display" & _
" WHERE BoardID = @BoardID"
Dim cmdUpdate As New SqlCommand(strCommand, connUACDC)
Dim objParam As SqlParameter
objParam = New SqlParameter
With objParam
.ParameterName = "@Name"
.Value = e.Item.Cells(3).Text
End With
cmdUpdate.Parameters.Add(o bjParam)
objParam = New SqlParameter
With objParam
.ParameterName = "@Title"
.Value = e.Item.Cells(4).Text
End With
cmdUpdate.Parameters.Add(o bjParam)
objParam = New SqlParameter
With objParam
.ParameterName = "@Display"
.Value = e.Item.Cells(2).Text
End With
cmdUpdate.Parameters.Add(o bjParam)
objParam = New SqlParameter
With objParam
.ParameterName = "@Picture"
.Value = e.Item.Cells(5).Text
End With
cmdUpdate.Parameters.Add(o bjParam)
objParam = New SqlParameter
With objParam
.ParameterName = "@BoardID"
.Value = e.Item.Cells(1).Text
End With
cmdUpdate.Parameters.Add(o bjParam)
System.Console.WriteLine(e .Item.Cell s(1).Text)
cmdUpdate.ExecuteNonQuery( )
dgrBoard.EditItemIndex = -1
connUACDC.Close()
BindGrid()
End Sub
When I click Edit, I can edit my pages easily enough. When I click update, however, the changes go away. I have looked at my code over and over again, and can't see what's wrong. I am sure it's something really simple. Any help would be greatly appreciated.
Here is the update code:
==========================
Public Sub DataGrid_Update(ByVal source As Object, ByVal e As DataGridCommandEventArgs)
Dim strConn As String = System.Configuration.Confi
Dim connUACDC As New SqlConnection(strConn)
connUACDC.Open()
Dim strCommand As String = "UPDATE tblBoard SET Name = @Name, Title = @Title, DisplayOrder = @Display" & _
" WHERE BoardID = @BoardID"
Dim cmdUpdate As New SqlCommand(strCommand, connUACDC)
Dim objParam As SqlParameter
objParam = New SqlParameter
With objParam
.ParameterName = "@Name"
.Value = e.Item.Cells(3).Text
End With
cmdUpdate.Parameters.Add(o
objParam = New SqlParameter
With objParam
.ParameterName = "@Title"
.Value = e.Item.Cells(4).Text
End With
cmdUpdate.Parameters.Add(o
objParam = New SqlParameter
With objParam
.ParameterName = "@Display"
.Value = e.Item.Cells(2).Text
End With
cmdUpdate.Parameters.Add(o
objParam = New SqlParameter
With objParam
.ParameterName = "@Picture"
.Value = e.Item.Cells(5).Text
End With
cmdUpdate.Parameters.Add(o
objParam = New SqlParameter
With objParam
.ParameterName = "@BoardID"
.Value = e.Item.Cells(1).Text
End With
cmdUpdate.Parameters.Add(o
System.Console.WriteLine(e
cmdUpdate.ExecuteNonQuery(
dgrBoard.EditItemIndex = -1
connUACDC.Close()
BindGrid()
End Sub
In your Page_Load event when you Bind DataGrid make sure you are binding it while IsPostBack is false:
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Not IsPostBack Then
BindGrid()
End If
End Sub
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
-tushar
--------------------------
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Not IsPostBack Then
BindGrid()
End If
End Sub
--------------------------
-tushar
ASKER
Yes, it is.
ASKER
The page is set to bind on Not IsPostBack.
And thanks for the Trim idea, but it still doesn't work.
And thanks for the Trim idea, but it still doesn't work.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I didn't think I could use the FindControl unless I had each column setup as a template column? Is that not correct?
Here is the code for my DataGrid: Maybe this will help...
========================== ========== ========== ========== ========== ========== =
<asp:datagrid id="dgrBoard" runat="server" datakeyfield="BoardID" OnDeleteCommand="DataGrid_ Delete"
OnSortCommand="DataGrid_So rt" AllowSorting="True" OnPageIndexChanged="DataGr id_Page" AllowPaging="False"
AutoGenerateColumns="False " OnCancelCommand="DataGrid_ CancelEdit " OnEditCommand="DataGrid_Ed it" OnUpdateCommand="DataGrid_ Update">
<AlternatingItemStyle Font-Size="Smaller" Font-Names="Verdana,Tahoma ,Sans-Seri f" BorderWidth="1px" BorderStyle="Solid"BorderC olor="This tle" BackColor="Thistle"></Alte rnatingIte mStyle><He aderStyle Font-Bold="True"></HeaderS tyle>
<Columns>
<asp:EditCommandColumn UpdateText="Update" CancelText="Cancel" EditText="edit">
<ItemStyle Width="50px"></ItemStyle>
</asp:EditCommandColumn>
<asp:BoundColumn DataField="BoardID" Visible="True"></asp:Bound Column>
<asp:BoundColumn DataField="DisplayOrder" HeaderText="Order">
<ItemStyle Width="40px"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="Name" HeaderText="Board Member Name">
<ItemStyle Width="200px"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="Title" HeaderText="Title">
<ItemStyle Width="75px"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="Picture" HeaderText="Picture Location" ReadOnly="True">
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundColumn>
<asp:HyperLinkColumn DataNavigateUrlField="Boar dID" DataNavigateUrlFormatStrin g="board_e dit.aspx?b oardid={0} "Text="Edi t Photo" HeaderText="Edit Photo">
<ItemStyle Width="100px"></ItemStyle>
</asp:HyperLinkColumn>
</Columns>
</asp:datagrid>
Here is the code for my DataGrid: Maybe this will help...
==========================
<asp:datagrid id="dgrBoard" runat="server" datakeyfield="BoardID" OnDeleteCommand="DataGrid_
OnSortCommand="DataGrid_So
AutoGenerateColumns="False
<AlternatingItemStyle Font-Size="Smaller" Font-Names="Verdana,Tahoma
<Columns>
<asp:EditCommandColumn UpdateText="Update" CancelText="Cancel" EditText="edit">
<ItemStyle Width="50px"></ItemStyle>
</asp:EditCommandColumn>
<asp:BoundColumn DataField="BoardID" Visible="True"></asp:Bound
<asp:BoundColumn DataField="DisplayOrder" HeaderText="Order">
<ItemStyle Width="40px"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="Name" HeaderText="Board Member Name">
<ItemStyle Width="200px"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="Title" HeaderText="Title">
<ItemStyle Width="75px"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="Picture" HeaderText="Picture Location" ReadOnly="True">
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundColumn>
<asp:HyperLinkColumn DataNavigateUrlField="Boar
<ItemStyle Width="100px"></ItemStyle>
</asp:HyperLinkColumn>
</Columns>
</asp:datagrid>
ASKER
mppeters was on the right track, except I was right. You can't use FindControl if you aren't using the Template Column, because ASP dynamically creates the name of the column at runtime, and the FIndControl throws that off:
In order to reference the item after the edit button is clicked, you can't just reference the cell. You have to reference the control within the cell. Additionally, you can't use the FindControl feature, because when you are using the boundcolumn funciton of the DataGrid, ASP dynamically creates the name of the control at run time.
Here is the way I had to reference my cells in order to get the value out of them:
CType(e.Item.Cells(2).Cont rols(0), TextBox).Text
Using that instead of just the e.item.Cells(2) made the whole thing work.
Thanks for your help.
V
In order to reference the item after the edit button is clicked, you can't just reference the cell. You have to reference the control within the cell. Additionally, you can't use the FindControl feature, because when you are using the boundcolumn funciton of the DataGrid, ASP dynamically creates the name of the control at run time.
Here is the way I had to reference my cells in order to get the value out of them:
CType(e.Item.Cells(2).Cont
Using that instead of just the e.item.Cells(2) made the whole thing work.
Thanks for your help.
V
how about try
.Value = e.Item.Cells(1).Text.Trim