[Last Call] Learn how to a build a cloud-first strategyRegister Now

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

Editable Datagrid only refreshes in debug mode asp.net

I have an editable datagrid. In debug mode, the page works just fine. Outside of debug mode, when I edit a row in the datagrid and click update, the grid refreshes but my change is not visible. If I reload the grid later, the change is there. It is like my grid is not getting re-bound right after I submit the change, but when I step through the code in debug mode, it is getting re-bound. I have inserted the code for my datagrid definition as well as my page_load and updatecommand routines in my code behind page.

Using asp.net, framework 1.1, visual studio 2003

Thanks!
<asp:DataGrid Runat="server" id="dgNotes" enableviewstate="true" OnCancelCommand="dgNotes_cancelcommand" OnUpdateCommand="dgNotes_Updatecommand" OnEditCommand="dgNotes_editcommand" OnDeleteCommand="dgNotes_deletecommand" AllowSorting="False" AutoGenerateColumns="False" Width="750px" CellPadding="5" AllowPaging="False" CssClass="dg" >
								<ItemStyle CssClass="dgrow" />
								<alternatingitemstyle CssClass="dgaltrow" />
								<HeaderStyle CssClass="dgheader" />
								<Columns>
									<asp:BoundColumn DataField="recordid" ReadOnly="True" Visible="False"  />
									<asp:BoundColumn DataField="datemodified" ReadOnly="True" Visible="True" HeaderText="Date" ItemStyle-Width=150px />
									<asp:TemplateColumn HeaderText="Note" ItemStyle-Width=450px>
										<ItemTemplate>
											<%# databinder.eval(container.dataitem,"consignornote")%>
										</ItemTemplate>
										<EditItemTemplate>
											<textarea runat=server rows=4 cols=60 id="tadgNote"><%# databinder.eval(container.dataitem,"consignornote") %></textarea>
										</EditItemTemplate>
									</asp:TemplateColumn>
									<asp:EditCommandColumn ButtonType="linkbutton" EditText="Edit" UpdateText="Update" CancelText="Cancel" HeaderText="Edit" ItemStyle-Width=75px />
									<asp:ButtonColumn ButtonType="LinkButton" CommandName="delete" Text="Delete" ItemStyle-HorizontalAlign="Center" headertext="Delete" itemstyle-width=75px/>
									
								</Columns>
							</asp:DataGrid>
 
 
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Session("partnerlogged") Then
            Response.Redirect("p_login.aspx")
        End If
 
        If Not Page.IsPostBack Then
            'first time page load
            viewstate("radiofilter") = "consignorid"
            bind_drpSearch("consignorid")
            BindData()
 
        Else
            'they have chosen a different order by selection, or they have submitted a new note, or they are editing a note
            If Request.Form("__EVENTTARGET") = "drpSearch" Then
                BindData()
            Else
                If Me.radiofilter.SelectedValue <> viewstate("radiofilter") Then
                    If Me.radiofilter.SelectedValue = "consignorid" Then
                        bind_drpSearch("consignorid")
                    Else
                        bind_drpSearch("lastname,firstname")
                    End If
                    viewstate("radiofilter") = Me.radiofilter.SelectedValue
                    BindData()
                End If
 
            End If
        End If
 
 
    End Sub
 
 
    Sub BindData()
        Dim objConn As New OleDbConnection(ConfigurationSettings.AppSettings("connstring_part1") & Server.MapPath("_database/" & Session("partnercode") & ".mdb"))
        Dim strsql As String
        Dim DA As OleDbDataAdapter
        Dim DS As DataSet
 
        Try
            strsql = "select * from consignornotes where consignorid = " & Me.drpSearch.SelectedValue & " order by datemodified desc"
            DA = New OleDbDataAdapter(strsql, objConn)
            DS = New DataSet
 
            If objConn.State <> ConnectionState.Open Then
                objConn.Open()
            End If
 
            DA.Fill(DS)
 
            'bind the datagrid
            With Me.dgNotes
                .DataSource = DS
                .DataKeyField = "recordid"
                .DataBind()
            End With
 
        Catch ex As Exception
            Throw New HLException(ex.Message, "default", "An unexpected error occurred while trying to bind the datagrid in p_consignornotes.", "", ex)
        Finally
            If objConn.State <> ConnectionState.Closed Then
                objConn.Close()
            End If
        End Try
    End Sub
 
    Sub dgNotes_editcommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs) Handles dgNotes.EditCommand
        dgNotes.EditItemIndex = e.Item.ItemIndex
        BindData()
    End Sub
 
    Sub dgNotes_updatecommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs) Handles dgNotes.UpdateCommand
        Dim objConn As New OleDbConnection(ConfigurationSettings.AppSettings("connstring_part1") & Server.MapPath("_database/" & Session("partnercode") & ".mdb"))
        Dim strsql As String
        Dim objCmd As New OleDbCommand(strsql, objConn)
        Dim lngEditID As Long
        Dim strConsignorNote As String
        Dim GKS As New GKS
 
        Try
            'Page.Validate()
            'get the data from the form
            lngEditID = CLng(e.Item.Cells(0).Text)
            strConsignorNote = GKS.CleanseForDB(CStr(CType(e.Item.FindControl("tadgnote"), System.Web.UI.HtmlControls.HtmlTextArea).Value))
 
 
            strsql = "update consignornotes set consignornote = '" & strConsignorNote & "', datemodified = #" & Now() & "# where recordid = " & lngEditID
            objCmd.CommandText = strsql
            If objConn.State <> ConnectionState.Open Then
                objConn.Open()
            End If
            objCmd.ExecuteNonQuery()
 
            'if no error, update was successful
            dgNotes.EditItemIndex = -1
 
            BindData()
 
 
        Catch ex As Exception
            Throw New HLException(ex.Message, "default", "An unexpected error occurred saving edited note on p_consignornotes.", "", ex)
        Finally
            If objConn.State <> ConnectionState.Closed Then
                objConn.Close()
            End If
            GKS = Nothing
 
        End Try
    End Sub

Open in new window

0
jkwasson
Asked:
jkwasson
1 Solution
 
CB_ThirumalaiCommented:
Use transaction and the binddata read will ensure that it is read after the commit.
Sub dgNotes_updatecommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs) Handles dgNotes.UpdateCommand
   Dim objConn As New OleDbConnection(ConfigurationSettings.AppSettings("connstring_part1") & Server.MapPath("_database/" & Session("partnercode") & ".mdb"))
   Dim strsql As String
   Dim objCmd As New OleDbCommand(strsql, objConn)
   Dim lngEditID As Long
   Dim strConsignorNote As String
   Dim GKS As New GKS
 
   Try
      'Page.Validate()
      'get the data from the form
      lngEditID = CLng(e.Item.Cells(0).Text)
      strConsignorNote = GKS.CleanseForDB(CStr(CType(e.Item.FindControl("tadgnote"), System.Web.UI.HtmlControls.HtmlTextArea).Value))
 
      strsql = "update consignornotes set consignornote = '" & strConsignorNote & "', datemodified = #" & Now() & "# where recordid = " & lngEditID
      objCmd.CommandText = strsql
      If objConn.State <> ConnectionState.Open Then
         objConn.Open()
      End If
 
      ' Start Transaction
      Dim Trans As SqlTransaction = Conn.BeginTransaction()
 
      objCmd.ExecuteNonQuery()
 
      'if no error, update was successful
      dgNotes.EditItemIndex = -1
 
      Trans.Commit()
      BindData()
 
 
   Catch ex As Exception
      ' Roll back
      Trans.Rollback()
      Throw New HLException(ex.Message, "default", "An unexpected error occurred saving edited note on p_consignornotes.", "", ex)
   Finally
      If objConn.State <> ConnectionState.Closed Then
        objConn.Close()
      End If
      GKS = Nothing
 
   End Try
End Sub

Open in new window

0
 
aibusinesssolutionsCommented:
Are you running it on the same machine out of debug mode?

VS 2003 doesn't have a built in web server if I recall, so you probably have IIS installed locally.  Have you checked the version of ASP.NET in IIS that is running on your web app directory?  It may be running 2.0 while your debugger is running 1.1.
0
 
jkwassonAuthor Commented:
That did the trick thanks. Funny that I do this exact same thing in lots of other areas of my web app but have never had to use the transaction in order to get it to work properly.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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