0

I have a fun project that involves VB.net winform app. This applications uses datasets for the forms. Three tables will be effected Shift_Log (SL), Problem_Log (PL) and Service_Request (SR). If the user changes the log dates on the (SL) so that the (PL) no longer agrees with the error time. I need to remove the foreign key from the (PL) and (SR) tables. I have not tested this yet. I have a couple of questions before I do.

  1. What if there are no records to update in the second update (SR)sql command?

  2. do I need to refresh the Dataset after this is executed.

     Public Function UPD_PL_Check(ByVal Shift_Key As String, ByVal SL_Begin As DateTime, SL_End As DateTime) As Integer
    
     ' Get rows from Laser Status Info
     Dim query As String = "SELECT Status_Key FROM [dbo].[Laser_Status_Info]  " &
                 " WHERE [Shift_FKey] = " & Shift_Key &
                 " AND [Err_time] NOT Between " & SL_Begin & " AND " & SL_End & ";"
    
     Dim dt As DataTable = New DataTable()
     Using conn As SqlConnection = New SqlConnection(My.Settings.LaserMaintLogConnectionString)
         conn.Open()
         Try
             Dim da As SqlDataAdapter = New SqlDataAdapter(query, conn)
             da.Fill(dt)
         Catch ex As Exception
             MessageBox.Show("Error occured! : " & ex.Message)
         Finally
             conn.Close()
         End Try
     End Using
    
     Dim rowsAffected As Integer = dt?.Rows.Count
    
     If rowsAffected = 0 Then Return rowsAffected
    
     ' Update the Problem log
     For Each i In dt?.Rows
         Using conn As SqlConnection = New SqlConnection(My.Settings.LaserMaintLogConnectionString)
             conn.Open()
             Dim Status_Key As String = dt.Rows(i)("Status_Key")
             Try
                 Dim PL_query As String =
                  "UPDATE [dbo].[Laser_Status_Info] " &
                  "   SET [Shift_FKey] = '' " &
                  " WHERE [Status_Key] = '" & Status_Key & "';"
                 Using Cmd As New SqlCommand(query, conn)
                     rowsAffected = Cmd.ExecuteNonQuery()
                 End Using
    
             Catch ex As Exception
                 MessageBox.Show("Error occured! : " & ex.Message)
             End Try
    
             ' Update Service request
             Try
                 Dim PL_query As String =
                  "UPDATE [dbo].[Laser_Maint_Log] " &
                  "   SET [SR_Shift_Key] = '' " &
                  " WHERE [SR_Status_Key] = '" & Status_Key & "';"
                 Using Cmd As New SqlCommand(query, conn)
                     rowsAffected = Cmd.ExecuteNonQuery()
                 End Using
             Catch ex As Exception
                 MessageBox.Show("Error occured! : " & ex.Message)
             End Try
             conn.Close()
         End Using
     Next
    
     Return rowsAffected
    

    End Function

  • 1
    You really need to pass those values as SQL parameters: using string concatenation is a recipe for failure. [Here's an example](https://stackoverflow.com/a/24023670/1115360), but don't use AddWithValue—the correct way is shown in these articles: [AddWithValue is Evil](http://www.dbdelta.com/addwithvalue-is-evil/), [AddWithValue is evil!](http://chrisrickard.blogspot.com/2007/06/addwithvalue-is-evil.html), and [Can we stop using AddWithValue() already?](https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/). – Andrew Morton Apr 02 '21 at 17:33
  • The best approach to your problem is to create a function in the sql database itself. To asnwer ur question, since u r using non-query, the command will still run without any error even there's no data. – Yat Fei Leong Apr 05 '21 at 06:00

0 Answers0