newbie: reusing command object in same procedure

V

vbDavidC

I have a procedure (private sub) that loads comboboxes on my form with
data from my access Db:

Dim SQL As String = "SELECT * FROM TableBlahBlah
Dim cmd As New OleDbCommand(SQL, con)
Dim reader As OleDb.OleDbDataReader = cmd.ExecuteReader()

Do While reader.Read
comboblahblah.Items.Add(reader(0))
Loop

I then want to populate a second combo box right after the first one.
I am new to ado.net and using objects so I am learning.

I cut and pasted the same code so I could use the same objects (I
guess), I did not want to recreate more objects. I guess it does not
matter if I do or not.

I am also wondering if I use my connection string to the DB can I
define it in the General area and just reference that DB once instead
of having to create it in all my procedures.
 
R

RobinS

Comments within...

vbDavidC said:
I have a procedure (private sub) that loads comboboxes on my form with
data from my access Db:

Dim SQL As String = "SELECT * FROM TableBlahBlah
Dim cmd As New OleDbCommand(SQL, con)
Dim reader As OleDb.OleDbDataReader = cmd.ExecuteReader()

Do While reader.Read
comboblahblah.Items.Add(reader(0))
Loop

I then want to populate a second combo box right after the first one.
I am new to ado.net and using objects so I am learning.

You can try this and reuse your command object; I think it will work.

cmd = OleDBCommand(newSQL, con)

I think you really need to be sure to close your datareader before using it
again; I don't use those, but seem to remember that being a requirement.
I cut and pasted the same code so I could use the same objects (I
guess), I did not want to recreate more objects. I guess it does not
matter if I do or not.

It's probably 6 of one, half dozen of the other. If you want another
command object in the same procedure, you have to name it something
different, or uninstantiate it before reinstantiating it. I don't think you
can set it to New() twice.
I am also wondering if I use my connection string to the DB can I
define it in the General area and just reference that DB once instead
of having to create it in all my procedures.

Most people put it in My.Settings. You can actually store it as a
connection string, then pull it using whatever name you give it, like
My.ProdDBCOnnectionString in your code.

Hope this helps.
Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
 
P

punjab_tom

ADO has better functionality than ADO.net

dipshits in redmond tried to sell us on 'Multiple Active Result Sets'
but I guess that they didn't test it before they sold it; because it
sure as **** doesn't work

-Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top