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.
What if there are no records to update in the second update (SR)sql command?
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