Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display records in Textboxs from the database, by searching.. I given 6 Textboxs for the output display, but I'm getting an error while searching" There is no row at position 3", If there is no records in position 3, then it should display Zero value in that following textbox.
Please find the below code and help me

VB
Dim con As New OleDb.OleDbConnection
       con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\dbexport.accdb;Persist Security Info=False;"
       con.Open()
       Dim dt As New DataTable


       Dim rs As New OleDb.OleDbDataAdapter("Select * from QA_db where eNB_Name Like '%" & search_box.Text & "%' ", con)
       rs.Fill(dt)


       '/// Display Search data feilds in textbox //

      EUtran1_txt.Text = dt.Rows(0).Item("EUtranCellFDD_Name")
      EUtran2_txt.Text = dt.Rows(1).Item("EUtranCellFDD_Name")
      EUtran3_txt.Text = dt.Rows(2).Item("EUtranCellFDD_Name")
      EUtran4_txt.Text = dt.Rows(3).Item("EUtranCellFDD_Name")
      EUtran5_txt.Text = dt.Rows(4).Item("EUtranCellFDD_Name")
      EUtran6_txt.Text = dt.Rows(5).Item("EUtranCellFDD_Name")

       rs.Dispose()

       con.Close()
Posted
Updated 28-Dec-14 4:26am
v2

So check the Rows.Count property...
VB
If DT.Rows.Count >0 Then
   EUtran1_txt.Text = dt.Rows(0).Item("EUtranCellFDD_Name")
End If
...
repeat for each row, with the appropriate count and row number.
 
Share this answer
 
Comments
Member 11338695 28-Dec-14 2:59am    
I repeated the code for each row, If the records have 6 then it showing result, if it has 3 records at that time for "EUtran4_txt.Text" showing error "There is no row at position 3."

If dt.Rows.Count > 0 Then
Me.EUtran1_txt.Text = dt.Rows(0).Item("EUtranCellFDD_Name")
End If
If dt.Rows.Count > 0 Then
Me.EUtran2_txt.Text = dt.Rows(1).Item("EUtranCellFDD_Name")
End If
If dt.Rows.Count > 0 Then
Me.EUtran3_txt.Text = dt.Rows(2).Item("EUtranCellFDD_Name")
End If
If dt.Rows.Count > 0 Then
Me.EUtran4_txt.Text = dt.Rows(3).Item("EUtranCellFDD_Name")
End If
If dt.Rows.Count > 0 Then
Me.EUtran5_txt.Text = dt.Rows(4).Item("EUtranCellFDD_Name")
End If
If dt.Rows.Count > 0 Then
Me.EUtran6_txt.Text = dt.Rows(4).Item("EUtranCellFDD_Name")
End If
DamithSL 28-Dec-14 3:09am    
If dt.Rows.Count > 0 Then
Me.EUtran1_txt.Text = dt.Rows(0).Item("EUtranCellFDD_Name")
End If
If dt.Rows.Count > 1 Then
Me.EUtran2_txt.Text = dt.Rows(1).Item("EUtranCellFDD_Name")
End If

you need to increase the value in if condition
Member 11338695 28-Dec-14 3:16am    
Thankyou Verymuch its working... :)
OriginalGriff 28-Dec-14 3:28am    
You're welcome!
You can also try something like:

VB
For row as Integer = 0 to dt.Rows.Count - 1
   GetTextBoxForRow(row).text = dt.Rows(row).Item("EUtranCellFDD_Name")
Next

Function GetTextBoxForRow(ByVal rowNumber as Integer) as TextBox
     Select Case rowNumber

        Case 0 : return EUtran1_txt
        Case 1 : return EUtran2_txt
        Case 2 : return EUtran3_txt
        Case 3 : return EUtran4_txt
        Case 4 : return EUtran5_txt
        Case 5 : return EUtran6_txt

     End Select
End Function


In this solution the loop will run for the only available number of rows.
 
Share this answer
 
v3

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