Local variable assAdapter already declared in block?

B

bill

Does anyone see why I'm getting this message? It errors out on line 5 at
"assAdapter"?
Dim Con = New OleDb.OleDbConnection("provider=microsoft.jet.oledb.4.0;" &
"data source=c:\_Archive\Documentation - Projects\Hardware Tracking -
2008\IT_Assets.mdb")

Dim assAdapter As OleDb.OleDbDataAdapter

Dim assCmd As New OleDb.OleDbCommand("SELECT * from tblAssets where
asset_tag=?")

assCmd.Parameters.Add("asset_tag", OleDbType.VarChar).Value = cboAsset.Text

Dim assAdapter As New OleDb.OleDbDataAdapter(assCmd, Con)

Dim assDT As New DataTable

'Release the instances of the above objects

assAdapter.Fill(assDT)

assAdapter.Dispose()
 
J

Jack Jackson

This is exactly the same problem you had in the previous example.

Either remove the first Dim (the best solution), or change the second
one to just an assignment statement:

assCmd = New OleDb.OleDbCommand( _
"SELECT * from tblAssets where asset_tag=?")
 
B

bill

It's still the assAdapter that errors out in this. It's not declared
anywhere else so I just don't see why this is happening. If i take out the
first declaration there are even more errors that come up.
 
B

bill

Sorry I see where you were going with that and you are right. It works like
this:
assAdapter = New OleDb.OleDbDataAdapter '(assCmd, Con)

What I don't see in the properties now is how to add the command and the
connection to the dataAdapter. Can you help me there since you've almost
done it all now? :)

Thank you!
 
J

Jack Jackson

You specify the connection in the command object.

assCmd = New OleDb.OleDbCommand( _
"SELECT * from tblAssets where asset_tag=?", Con)
assAdapter = New OleDbAdapter(assCmd)

or
assCmd = ...
Dim assAdapter as New OleDbAdapter
assAdapter.SelectCommand = assCmd
 
B

bill

Thank you again! That got it:) Though there is more now.... I get an error
at "assAdapter.fill(assDT)" that says "fill: SelectCommand.Connection
property has not been initialized? Do you see why?
Dim Con = New OleDb.OleDbConnection("provider=microsoft.jet.oledb.4.0;" &
"data source=c:\_Archive\Documentation - Projects\Hardware Tracking -
2008\IT_Assets.mdb")

Dim assCmd As New OleDb.OleDbCommand("SELECT * from tblAssets where
asset_tag=?")

Dim assAdapter As OleDb.OleDbDataAdapter

assAdapter = New OleDb.OleDbDataAdapter(assCmd)

'Dim assCmd As OleDb.OleDbCommand

assCmd = New OleDb.OleDbCommand("SELECT * from tblAssets where asset_tag=?",
Con)

assCmd.Parameters.Add("asset_tag", OleDbType.VarChar).Value = cboAsset.Text

Dim assDT As New DataTable

'Need to initialize the dataAdapter here

'Release the instances of the above objects

assAdapter.Fill(assDT)

'assAdapter.Dispose()

Me.DataGridView1.DataSource = assDT

End If
 
J

Jack Jackson

Sure. The message says it all. The DataAdapter's Select command,
which is built from the text you pass to the DataAdapter's
constructor, doesn't have any connection info.

See my comments below.

Thank you again! That got it:) Though there is more now.... I get an error
at "assAdapter.fill(assDT)" that says "fill: SelectCommand.Connection
property has not been initialized? Do you see why?
Dim Con = New OleDb.OleDbConnection("provider=microsoft.jet.oledb.4.0;" &
"data source=c:\_Archive\Documentation - Projects\Hardware Tracking -
2008\IT_Assets.mdb")

The following line creates an OleDbCommand without a connection
object.
Dim assCmd As New OleDb.OleDbCommand("SELECT * from tblAssets where
asset_tag=?")

Dim assAdapter As OleDb.OleDbDataAdapter

The following line creates a DataAdapter from the OleDbCommand object
that doesn't have a connection object.
assAdapter = New OleDb.OleDbDataAdapter(assCmd)

'Dim assCmd As OleDb.OleDbCommand

The following line creates a second command object with the same
select as the previous one, this time with a connection object. This
command object is never used. You should get rid of this line, and
add Con as a second parameter to the previous OleDbCommand.
 
B

bill

Oh man, I should have seen that. I comment out that lin, add the con to the
second and it works perfectly. Thank you so much! You have been incredibly
helpful and patient and I really appreciate it. I have learned allot!
Thank you!
Bill Bray
 
S

sloan

Now you can work on your naming conventions.

The @$$Adaptor sounds like something you use to get a colonoscopy and you
have an oversized or undersized butt. (ha ha).

...
 
B

bill

LOL! I thought someone would have said something sooner:)

It just made sense to me...it was a data adapter for the assets....but it
still made me chuckle.
 

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