?
Solved

ASP.NET DataGrid Update Changes Not Working

Posted on 2005-03-22
7
Medium Priority
?
756 Views
Last Modified: 2012-06-27
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
Comment
Question by:VeeVan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13603515
is it possible that in your text box there is leading spaces?
how about try
 .Value = e.Item.Cells(1).Text.Trim
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 13603708
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
 
LVL 1

Author Comment

by:VeeVan
ID: 13603756
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!

 
LVL 1

Author Comment

by:VeeVan
ID: 13603787
The page is set to bind on Not IsPostBack.

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

0
 
LVL 8

Accepted Solution

by:
mppeters earned 1500 total points
ID: 13604840
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
 
LVL 1

Author Comment

by:VeeVan
ID: 13610114
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
 
LVL 1

Author Comment

by:VeeVan
ID: 13610839
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

770 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