Вопрос

I want to come up with this problem.... if there's only 1 false in loop then it will not insert into database and vise versa if all true then it will insert to database.

Here's is my code

Public Function Check_Foreign_Key(ByVal select_column_name As String, ByVal table_name As String, ByVal where_column_name As String, ByVal where_value As String, ByRef Foreign_Key As String) As Boolean
    Dim dt_service_provider_id As DataTable = ExecuteSQLQuery("select " & select_column_name & " from " & table_name & " where " & where_column_name & " = '" & where_value & "'")
    If dt_service_provider_id.Rows.Count = 0 Then
        Return False
    Else
        Foreign_Key = dt_service_provider_id.Rows(0).Item(0).ToString()
        Return True
    End If
End Function 


    For Each dr As DataRow In dt.Rows
     Dim dt_rows as Integer = 0
     Dim site_id As Boolean = Check_Foreign_Key("site_id", "sites", "site_code", dr(2).ToString, dt_fk)
        If dr(0).ToString.Length > 50 And dr(0).ToString = "" And dr(0).ToString Is Nothing Then
            get_error(dt.TableName, dt.Columns(2).ToString, dt_row.ToString, "Character is greater than 50")
            'in here i want to continue checking go to next, i think i got error in cheking if dr(0) is empty
         Else
             If dr(1).ToString.Length > 50  And dr(1).ToString = "" And dr(1).ToString Is Nothing Then
               get_error(dt.TableName, dt.Columns(2).ToString, dt_row.ToString, "Character is greater than 50")
               'in here i want to continue again to next
              Else
               Select Case site_id
                Case False
                  get_error(dt.TableName, dt.Columns(2).ToString, dt_row.ToString, dr(2).ToString)
                  Return
                End Select
              End If
            End If
          dt_row += 1
     Next

THis is the code i only i know but unfortunately i cannot get my logic it always inserting rather it have 1 false

Это было полезно?

Решение

Hope this solve the problem

Dim blnAllTrue As Boolean = True
For Each dr As DataRow In dt.Rows
    Dim site_id As Boolean = Check_Foreign_Key("site_id", "sites", "site_code", dr(2).ToString, dt_fk)
    blnAllTrue = blnAllTrue And site_id
    If site_id = False Then
          get_error(dt.TableName, dt.Columns(2).ToString, dt_row.ToString, dr(2).ToString)
    End If
Next
If blnAllTrue = True Then
    'insert into your database
End If

Другие советы

Try this

Private SaveToDB()
    For Each dr As DataRow In dt.Rows
     Dim site_id As Boolean = Check_Foreign_Key("site_id", "sites", "site_code", dr(2).ToString, dt_fk)
        Select Case site_id
           Case False
              get_error(dt.TableName, dt.Columns(2).ToString, dt_row.ToString, dr(2).ToString)
              Return

         End Select
     Next

    'if you got here means no error
    'NOTE: You should loop again and save the rows
     For Each dr As DataRow In dt.Rows
        'Your code to save into the database here
     Next
End Sub
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top