eayvaz
asked on
read multiple datas from sql 2005 and use in visual basic 2005
To get data from sql 2005 and put it in visual basic 2005 I do as
Dim strSQL As String
strSQL = "SELECT..."
Dim Com As New System.Data.SqlClient.SqlC ommand(str SQL, Con)
Dim rdr As System.Data.SqlClient.SqlD ataReader = Com.ExecuteReader()
While rdr.Read()
cmb_firma_ismi.Items.Add(r dr.GetStri ng(0)) 'or this can be any other control'
End While
When I again want to get a value when I use the same code it gives an error saying the con and rdr is already declared.
Is there an easy way to run these statements without declaring other variable ?
Dim strSQL As String
strSQL = "SELECT..."
Dim Com As New System.Data.SqlClient.SqlC
Dim rdr As System.Data.SqlClient.SqlD
While rdr.Read()
cmb_firma_ismi.Items.Add(r
End While
When I again want to get a value when I use the same code it gives an error saying the con and rdr is already declared.
Is there an easy way to run these statements without declaring other variable ?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Look for this block of code the first time you use the reader:
While rdr.Read()
cmb_firma_ismi.Items.Add(r dr.GetStri ng(0)) 'or this can be any other control'
End While
...and add this after it.
rdr.Close()
While rdr.Read()
cmb_firma_ismi.Items.Add(r
End While
...and add this after it.
rdr.Close()
Instead of opening a connection each time you want to populate the control...i would suggest you use a DataSet and store the values in different DataTable... the loop through each DataRow in the DataTable and do a Add Item... or alternatively you can also bind the control for eg. a Combobox to the DataTable and hence this will save you the hassle of looping through the DataRow of the DataTable.....
Instead of opening a connection each time you want to populate the control...i would suggest you use a DataSet and store the values in different DataTable... the loop through each DataRow in the DataTable and do a Add Item... or alternatively you can also bind the control for eg. a Combobox to the DataTable and hence this will save you the hassle of looping through the DataRow of the DataTable.....
ASKER
error : Incorrect syntax near ')'.
on line : rdr = Com.ExecuteReader()
on line : rdr = Com.ExecuteReader()
Now that's strange. There is absolutely nothing wrong with that statement. Are you sure you have it right in your code? No rogue trailing characters or anything?
ASKER
This error was given for rdr = Com.ExecuteReader() this line