Link to home
Start Free TrialLog in
Avatar of VeeVan
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.ConfigurationSettings.AppSettings("ConnectionString")
        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(objParam)

        objParam = New SqlParameter
        With objParam
            .ParameterName = "@Title"
            .Value = e.Item.Cells(4).Text
        End With
        cmdUpdate.Parameters.Add(objParam)

        objParam = New SqlParameter
        With objParam
            .ParameterName = "@Display"
            .Value = e.Item.Cells(2).Text
        End With
        cmdUpdate.Parameters.Add(objParam)

        objParam = New SqlParameter
        With objParam
            .ParameterName = "@Picture"
            .Value = e.Item.Cells(5).Text
        End With
        cmdUpdate.Parameters.Add(objParam)

        objParam = New SqlParameter
        With objParam
            .ParameterName = "@BoardID"
            .Value = e.Item.Cells(1).Text
        End With
        cmdUpdate.Parameters.Add(objParam)

        System.Console.WriteLine(e.Item.Cells(1).Text)

        cmdUpdate.ExecuteNonQuery()
        dgrBoard.EditItemIndex = -1

        connUACDC.Close()
        BindGrid()

    End Sub
Avatar of laotzi2000
laotzi2000

is it possible that in your text box there is leading spaces?
how about try
 .Value = e.Item.Cells(1).Text.Trim
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
Avatar of VeeVan

ASKER

Yes, it is.
Avatar of VeeVan

ASKER

The page is set to bind on Not IsPostBack.

And thanks for the Trim idea, but it still doesn't work.

ASKER CERTIFIED SOLUTION
Avatar of mppeters
mppeters

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

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_Sort" AllowSorting="True" OnPageIndexChanged="DataGrid_Page" AllowPaging="False"
AutoGenerateColumns="False" OnCancelCommand="DataGrid_CancelEdit" OnEditCommand="DataGrid_Edit" OnUpdateCommand="DataGrid_Update">
            <AlternatingItemStyle Font-Size="Smaller" Font-Names="Verdana,Tahoma,Sans-Serif" BorderWidth="1px" BorderStyle="Solid"BorderColor="Thistle" BackColor="Thistle"></AlternatingItemStyle><HeaderStyle Font-Bold="True"></HeaderStyle>
            <Columns>
                            <asp:EditCommandColumn UpdateText="Update" CancelText="Cancel" EditText="edit">
                                  <ItemStyle Width="50px"></ItemStyle>
                  </asp:EditCommandColumn>
                  <asp:BoundColumn DataField="BoardID" Visible="True"></asp:BoundColumn>
                  <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="BoardID" DataNavigateUrlFormatString="board_edit.aspx?boardid={0}"Text="Edit Photo" HeaderText="Edit Photo">
                        <ItemStyle Width="100px"></ItemStyle>
                  </asp:HyperLinkColumn>
            </Columns>
               </asp:datagrid>
Avatar of VeeVan

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).Controls(0), TextBox).Text

Using that instead of just the e.item.Cells(2) made the whole thing work.

Thanks for your help.

V