Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When i click the Search button with out any input ,Am getting a message box as "There is no row at position 0." and then when i click on Ok then am getting my message as "Please enter an ID to search"

Private Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click
       Try
           FillDataGridView1("select * from [Sheet1$] where ID='" & TxtId.Text & "'")
           TxtFamilyname.Text = dt.Rows(0).Item(1)
           TxtGivenname.Text = dt.Rows(0).Item(2)
           TxtGender.Text = dt.Rows(0).Item(3)
           TxtDob.Text = dt.Rows(0).Item(4)
           TxtStreet.Text = dt.Rows(0).Item(5)
           TxtHouse.Text = dt.Rows(0).Item(6)
           TxtPostcode.Text = dt.Rows(0).Item(7)
           TxtCity.Text = dt.Rows(0).Item(8)
       Catch ex As Exception
           MsgBox(ex.Message, MsgBoxStyle.Critical, Text)
       End Try

       If TxtId.Text = "" Then
           MsgBox("Please enter a ID to search")
       End If

   End Sub
Posted

1 solution

Because you are doing the search before checking the input. Try changing things around a bit

Private Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click

        If TxtId.Text = "" Then
            MsgBox("Please enter a ID to search")
            Exit Sub
        End If

        Try
            FillDataGridView1("select * from [Sheet1$] where ID='" & TxtId.Text & "'")
            TxtFamilyname.Text = dt.Rows(0).Item(1)
            TxtGivenname.Text = dt.Rows(0).Item(2)
            TxtGender.Text = dt.Rows(0).Item(3)
            TxtDob.Text = dt.Rows(0).Item(4)
            TxtStreet.Text = dt.Rows(0).Item(5)
            TxtHouse.Text = dt.Rows(0).Item(6)
            TxtPostcode.Text = dt.Rows(0).Item(7)
            TxtCity.Text = dt.Rows(0).Item(8)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Text)
        End Try
 

End Sub
 
Share this answer
 
Comments
Shaik Izaz 11-Jun-15 6:37am    
Its works like charm!, how to validate if the given Input is not in the database?
CHill60 11-Jun-15 7:33am    
Try checking dt.Rows.Count = 0

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900