Hello all,

I want to get items into the ListBox from Database on FormLoad.

My code is:

database Code:
  1. Private Sub Models_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.  
  3.         Dim qry As String
  4.         qry = "SELECT model FROM models"
  5.  
  6.         Dim sqlcon As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\My Personal\VB Projects\Mobile Repairing\Mobile Repairing\repairs.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
  7.        
  8.         Dim sqlcmd As New SqlCommand(qry, sqlcon)
  9.         Dim dr As SqlDataReader
  10.  
  11.         sqlcon.Open()
  12.         dr = sqlcmd.ExecuteReader
  13.         dr.Read()
  14.  
  15.         If dr.HasRows Then
  16.             While dr.Read()
  17.                 ModelListBox.Items.Add(ToString)
  18.             End While
  19.         End If
  20.  
  21.         dr.Close()
  22.         sqlcon.Close()
  23.  
  24.     End Sub

I am already having three models in database that has to be loaded in ListBox on formload.

Any help will be appreciated.

Thanks in advance.

Trusted