Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27235

Struggling With Updating Database Via DataGridView

$
0
0
I've previously posted about my difficulties here, here, here and here.

A year since I've started struggling with this problem, it still hasn't been fully resolved. 90% of the time, it works fine. 10% of the time, it doesn't, and I cannot figure out why.

I have a datagridview with several columns, all of which are read only by default. When the user clicks a toggle button, three columns (Start Date, Complete Date, and Load Date) are unlocked for editing. When clicking in one of those cells, a custom calendar control appears. When a date is selected in that control, it is inserted into the corresponding cell in the datagridview. This part works fine.

What is hit-or-miss is whether or not the update to the database actually goes through.

Here is my update procedure:

vb Code:
  1. Public Sub UpdateScheduleData()
  2.         Try
  3.             If IgnoreInput Then Exit Sub
  4.  
  5.             Using Conn = GetConnect()
  6.                 Conn.Open()
  7.  
  8.                 cmdSQL = New SqlCommand("Update Orders_Open Set JobStartDate = @JobStartDate, JobCompleteDate = @JobCompleteDate, LoadDate = @LoadDate, Status = @Status Where OrderID = @OrderID", daSchedule.SelectCommand.Connection)
  9.  
  10.                 cmdSQL.Parameters.Add(New SqlParameter("@OrderID", SqlDbType.VarChar))
  11.                 cmdSQL.Parameters("@OrderID").Value = dgSchedule.Rows(LastEditedRow).Cells("Order ID").Value.ToString
  12.  
  13.                 cmdSQL.Parameters.Add(New SqlParameter("@JobStartDate", SqlDbType.VarChar))
  14.                 If dgSchedule.Rows(LastEditedRow).Cells("Job Start").Value.ToString <> "" Then
  15.                     cmdSQL.Parameters("@JobStartDate").Value = dgSchedule.Rows(LastEditedRow).Cells("Job Start").Value.ToString
  16.                 Else
  17.                     cmdSQL.Parameters("@JobStartDate").Value = DBNull.Value
  18.                 End If
  19.  
  20.                 cmdSQL.Parameters.Add(New SqlParameter("@JobCompleteDate", SqlDbType.VarChar))
  21.                 If dgSchedule.Rows(LastEditedRow).Cells("Job Complete").Value.ToString <> "" Then
  22.                     cmdSQL.Parameters("@JobCompleteDate").Value = dgSchedule.Rows(LastEditedRow).Cells("Job Complete").Value.ToString
  23.                 Else
  24.                     cmdSQL.Parameters("@JobCompleteDate").Value = DBNull.Value
  25.                 End If
  26.  
  27.                 cmdSQL.Parameters.Add(New SqlParameter("@LoadDate", SqlDbType.VarChar))
  28.                 If dgSchedule.Rows(LastEditedRow).Cells("Load Date").Value.ToString <> "" Then
  29.                     cmdSQL.Parameters("@LoadDate").Value = dgSchedule.Rows(LastEditedRow).Cells("Load Date").Value.ToString
  30.                 Else
  31.                     cmdSQL.Parameters("@LoadDate").Value = DBNull.Value
  32.                 End If
  33.  
  34.                 cmdSQL.Parameters.Add(New SqlParameter("@Status", SqlDbType.Int))
  35.                 cmdSQL.Parameters("@Status").Value = dgSchedule.Rows(LastEditedRow).Cells("Status").Value
  36.  
  37.                 daSchedule.UpdateCommand = cmdSQL
  38.                 daSchedule.Update(dtSchedule)
  39.             End Using
  40.  
  41.         Catch ex As Exception
  42.             MessageBox.Show("Error: " & ex.Source & ": " & ex.Message, System.Reflection.MethodInfo.GetCurrentMethod.ToString, MessageBoxButtons.OK)
  43.             LogErrors(System.Reflection.MethodInfo.GetCurrentMethod.ToString & " -- " & ex.Message)
  44.  
  45.         End Try
  46.     End Sub

I call the UpdateScheduleData procedure on DataGridView_CellEndEdit, BindingSource_PositionChanged and DataGridView_RowValidated. Yet sometimes (and I can find no rhyme or reason as to why) the update doesn't make it to the database, even when the dataadapter.update call is made with a datatable that reflects the changed dates.

What am I doing wrong at this point?

Viewing all articles
Browse latest Browse all 27235

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>