NCSO
asked on
Populating a ListBox from a database
Hi Experts,
I have a need to fill a listbox with information from a database. I have been somewhat successful thus far but I am only getting the first record that matches the sql command. What am I missing. I need the listbox to be populated with every record that matches the sql command. Your assistance is greatly appreciated.
Private Sub SuspectArrestorEscapeeADD_ Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim conn2 As New System.Data.SqlClient.SqlC onnection( "Persist Security Info=True;Server=xxx.xxx.x xx.xxx;Dat abase=Repo rting;uid= uid;networ k=dbmssocn ;password= password")
Dim da2 As New System.Data.SqlClient.SqlD ataAdapter ("Select * from tblOffense", conn2)
Dim ds2 As New System.Data.DataSet
Dim cmd As New System.Data.SqlClient.SqlC ommand("Se lect (offense_number) from tblOffense where incident_ID='" & IncidentPeopleAdd.Incident _ID.Text & "' and ARN='" & IncidentPeopleAdd.ARN.Text & "'", conn2)
Dim cmd3 As New System.Data.SqlClient.SqlC ommand("Se lect (offensedesc) from tblOffense where incident_ID='" & IncidentPeopleAdd.Incident _ID.Text & "' and ARN='" & IncidentPeopleAdd.ARN.Text & "'", conn2)
Try
conn2.Open()
ListBox1.Items.Add(cmd.Exe cuteScalar () & " - " & cmd3.ExecuteScalar()(1).To String)
Catch ex As Exception
MessageBox.Show(ex.Message )
Finally
conn2.Close()
End Try
End Sub
I have a need to fill a listbox with information from a database. I have been somewhat successful thus far but I am only getting the first record that matches the sql command. What am I missing. I need the listbox to be populated with every record that matches the sql command. Your assistance is greatly appreciated.
Private Sub SuspectArrestorEscapeeADD_
Dim conn2 As New System.Data.SqlClient.SqlC
Dim da2 As New System.Data.SqlClient.SqlD
Dim ds2 As New System.Data.DataSet
Dim cmd As New System.Data.SqlClient.SqlC
Dim cmd3 As New System.Data.SqlClient.SqlC
Try
conn2.Open()
ListBox1.Items.Add(cmd.Exe
Catch ex As Exception
MessageBox.Show(ex.Message
Finally
conn2.Close()
End Try
End Sub
ASKER
Hai,
I am fairly new to vb, could you give me an example of using a datareader rather than the execute scalar? Thanx
I am fairly new to vb, could you give me an example of using a datareader rather than the execute scalar? Thanx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
hai
r u can fill dataset and bind it to list box
Dim d As SqlDataReader
Dim ds As New DataSet
Dim cmd As New SqlCommand
Dim sqldatap As New SqlDataAdapter
dim SqlConnection1 as new sqlconnection
cmd.CommandText = "select <col1>, <col2> from <tablename> where <condition>"
SqlConnection1.connections trin = "<yr connection string>"
cmd.Connection= sqlconnection1
sqldatap.SelectCommand = cmd
SqlConnection1.Open()
sqldatap.Fill(ds)
ListBox1.DataSource = ds.Tables(0)
ListBox1.DisplayMember = "<display name>"
ListBox1.ValueMember = "<primary key>"
SqlConnection1.Close()
regards
jp
r u can fill dataset and bind it to list box
Dim d As SqlDataReader
Dim ds As New DataSet
Dim cmd As New SqlCommand
Dim sqldatap As New SqlDataAdapter
dim SqlConnection1 as new sqlconnection
cmd.CommandText = "select <col1>, <col2> from <tablename> where <condition>"
SqlConnection1.connections
cmd.Connection= sqlconnection1
sqldatap.SelectCommand = cmd
SqlConnection1.Open()
sqldatap.Fill(ds)
ListBox1.DataSource = ds.Tables(0)
ListBox1.DisplayMember = "<display name>"
ListBox1.ValueMember = "<primary key>"
SqlConnection1.Close()
regards
jp
Hey i tried the above code and got this error,
System.InvalidOperationExc eption was unhandled
Message="An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object."
Source="ListBoxTest"
StackTrace:
at ListBoxTest.My.MyProject.M yForms.Cre ate__Insta nce__[T](T Instance) in 17d14f5c-a337-4978-8281-53 493378c107 1.vb:line 190
at ListBoxTest.My.MyProject.M yForms.get _Form1()
at ListBoxTest.My.MyApplicati on.OnCreat eMainForm( ) in C:\Documents and Settings\Deepesh Admin\Local Settings\Application Data\Temporary Projects\ListBoxTest\My Project\Application.Design er.vb:line 35
at Microsoft.VisualBasic.Appl icationSer vices.Wind owsFormsAp plicationB ase.OnRun( )
at Microsoft.VisualBasic.Appl icationSer vices.Wind owsFormsAp plicationB ase.DoAppl icationMod el()
at Microsoft.VisualBasic.Appl icationSer vices.Wind owsFormsAp plicationB ase.Run(St ring[] commandLine)
at ListBoxTest.My.MyApplicati on.Main(St ring[] Args) in 17d14f5c-a337-4978-8281-53 493378c107 1.vb:line 81
at System.AppDomain._nExecute Assembly(A ssembly assembly, String[] args)
at Microsoft.VisualStudio.Hos tingProces s.HostProc .RunUsersA ssembly()
at System.Threading.Execution Context.Ru n(Executio nContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHel per.Thread Start()
InnerException: System.NullReferenceExcept ion
Message="Object reference not set to an instance of an object."
Source="ListBoxTest"
StackTrace:
at ListBoxTest.Form1..ctor() in C:\Documents and Settings\Deepesh Admin\Local Settings\Application Data\Temporary Projects\ListBoxTest\Form1 .vb:line 3
InnerException:
System.InvalidOperationExc
Message="An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object."
Source="ListBoxTest"
StackTrace:
at ListBoxTest.My.MyProject.M
at ListBoxTest.My.MyProject.M
at ListBoxTest.My.MyApplicati
at Microsoft.VisualBasic.Appl
at Microsoft.VisualBasic.Appl
at Microsoft.VisualBasic.Appl
at ListBoxTest.My.MyApplicati
at System.AppDomain._nExecute
at Microsoft.VisualStudio.Hos
at System.Threading.Execution
at System.Threading.ThreadHel
InnerException: System.NullReferenceExcept
Message="Object reference not set to an instance of an object."
Source="ListBoxTest"
StackTrace:
at ListBoxTest.Form1..ctor() in C:\Documents and Settings\Deepesh Admin\Local Settings\Application Data\Temporary Projects\ListBoxTest\Form1
InnerException:
instead of using execute scalar, use Datareader. and loop thru in it and add values from reader to ur list box.. execute scalar Executes the query, and returns the first column of the first row in the result set returned by the query. Extra columns or rows are ignored..
jp