none
how to get back to upper step of function if condition not matched.. RRS feed

  • Question

  • For Eg i have this Select Function.

     Try
                Query = "Select * From [ClientMaster] where [clientid] = '100001' "
                '  How to return here  with new query
                SqlCmd = New SqlCommand(Query, ConnC())
    
                '//Execute the query
                ' Dim comdr As SqlDataReader = 
                SDR = SqlCmd.ExecuteReader()
                If SDR.HasRows = True Then
                    While SDR.Read
                        ' idn = comdr.GetInt32(0)
                        t1.Text = SDR.GetInt32(0).ToString
                        d2.Value = SDR.GetDateTime(1)
    
                    End While
    
                Else
                    Query = "Select * From [ClientMaster] where [clientid] = '100002'"
                End If
    
    
            Catch ex As Exception
            End Try

    now in case when with first query data is not fount i want to change query and return to execute command with new Query..


    Pradeep Yadav (Social MSDN)

    Tuesday, May 29, 2018 7:21 AM

All replies

  •  

    Around your code

    SDR.HasRow = false Do Until SDR.Hasrow = true .......................... Loop

    And remove that 

    Catch ex As Exception
    End Try

    At the first professional inspection your code will be branded as dangerous to use. 



    Success
    Cor


    Tuesday, May 29, 2018 9:33 AM
  • now in case when with first query data is not fount i want to change query and return to execute command with new Query..
    Dim Querys(1) As String
    Querys(0) = "Select * From [ClientMaster] where [clientid] = '100001'"
    Querys(1) = "Select * From [ClientMaster] where [clientid] = '100002'"
    
    For Each Query As String in Querys
        SqlCmd = New SqlCommand(Query, ConnC())
         '//Execute the query
        SDR = SqlCmd.ExecuteReader()
        If SDR.HasRows = True Then
            While SDR.Read
                t1.Text = SDR.GetInt32(0).ToString
                d2.Value = SDR.GetDateTime(1)
            End While
            Exit For
        End If
    Next
    

    Tuesday, May 29, 2018 11:53 AM
  • Hi pk.yadav,

     Private Function loaddata(query As String) As Boolen
            Dim value As Boolean
            Try
                SqlCmd = New SqlCommand(query, ConnC())
    
                '//Execute the query
                ' Dim comdr As SqlDataReader = 
                SDR = SqlCmd.ExecuteReader()
                If SDR.HasRows = True Then
                    While SDR.Read
                        ' idn = comdr.GetInt32(0)
                        t1.Text = SDR.GetInt32(0).ToString
                        d2.Value = SDR.GetDateTime(1)
    
                    End While
                    value = True
                Else
                    value = False
                End If
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            Return value
        End Function
     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    
            Query1 = "Select * From [ClientMaster] where [clientid] = '100001' "
            '  How to return here  with new query
            query2 = "Select * From [ClientMaster] where [clientid] = '100002'"
            If loaddata(Query1) Then
    
            Else
                loaddata(query2)
            End If
    
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, May 30, 2018 5:43 AM
    Moderator
  • @Cherry,

    Again, same thing as Acamar posted, but with flaws... why return a string from a Boolean valued function?


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, May 30, 2018 12:14 PM
    Moderator
  • Maybe use a single query? For example:

    SELECT TOP(1) * FROM [ClientMaster] WHERE [ClientId] IN ('100001', '100002') ORDER BY [ClientId]


    Wednesday, May 30, 2018 7:13 PM
  • Maybe use a single query? For example:

    SELECT TOP(1) * FROM [ClientMaster] WHERE [ClientId] IN ('100001', '100002') ORDER BY [ClientId]


    That will return results for both IDs.  OP requirement was to return results for the second ID only if the first ID returned nothing.

    Wednesday, May 30, 2018 9:53 PM