Link to home
Create AccountLog in
Avatar of eayvaz
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.SqlCommand(strSQL, Con)
Dim rdr As System.Data.SqlClient.SqlDataReader = Com.ExecuteReader()
While rdr.Read()
            cmb_firma_ismi.Items.Add(rdr.GetString(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 ?
ASKER CERTIFIED SOLUTION
Avatar of cheddar73
cheddar73

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of eayvaz
eayvaz

ASKER

There is already an open DataReader associated with this Command which must be closed first.
This error was given for   rdr = Com.ExecuteReader()  this line
Look for this block of code the first time you use the reader:

While rdr.Read()
            cmb_firma_ismi.Items.Add(rdr.GetString(0))   'or this can be any other control'
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.....
Avatar of eayvaz

ASKER

error :  Incorrect syntax near ')'.
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?