Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 766
  • Last Modified:

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
0
VeeVan
Asked:
VeeVan
1 Solution
 
laotzi2000Commented:
is it possible that in your text box there is leading spaces?
how about try
 .Value = e.Item.Cells(1).Text.Trim
0
 
tusharashahCommented:
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
0
 
VeeVanAuthor Commented:
Yes, it is.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
VeeVanAuthor Commented:
The page is set to bind on Not IsPostBack.

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

0
 
mppetersCommented:
You're not actually getting anything from the edit mode of the row.

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


.Value = e.Item.Cells(3).Text refers to the prior value in that cell. So your SQL update statement is just updating the row with the same values.

You have to find the control in the item you're editing:

.Value = CType(e.Item.FindControl("NameTextBox"),TextBox).Text.Trim()

0
 
VeeVanAuthor Commented:
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>
0
 
VeeVanAuthor Commented:
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
0

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now