473,382 Members | 1,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How do I complete this?


I'm still new to vb.net and even newer to using stored procedures in mysql.
The called procedure will return a single row from the DB.
I'm not sure how to get that value into a DataTable. I think I need to use
some type of adapter. Could someone help me complete this?

Thanks in advance
Jeff

Dim connection2 As New OdbcConnection(MyConnString)
Dim command2 As New OdbcCommand("call SelectFromParticipants(?)",
connection2)
Dim param As New OdbcParameter("User", OdbcType.VarChar)
param.Value = Session("User")
command2.Parameters.Add(param)
connection2.Open()
command2.ExecuteNonQuery()
DataTable = <- not sure how to complete this.
connection2.Close()

--
Posted via a free Usenet account from http://www.teranews.com

Apr 5 '07 #1
10 1398
Dim connection2 As New OdbcConnection(MyConnString)
Dim command2 As New OdbcCommand("call SelectFromParticipants(?)",
connection2)
Dim param As New OdbcParameter("User", OdbcType.VarChar)
param.Value = Session("User")
command2.Parameters.Add(param)
connection2.Open()
command2.ExecuteNonQuery()
DataTable = <- not sure how to complete this.
connection2.Close()
Change this to something like this:

Dim connection2 As New OdbcConnection(MyConnString)
Dim command2 As New OdbcCommand("call
SelectFromParticipants(?)", connection2)
command2.Parameters.Add("User", OdbcType.VarChar).Value =
Session("User")
Dim da As New OdbcDataAdapter(command2)
Dim dt As New DataTable("My Table")
da.Fill(dt)

Please note I left out the disposing statements for simplicity's sake
(though you should dispose of these objects to ensure fast cleanup) I
usually wrap the usage of each object in using statements.

Also, is there a reason you want to dump a single row into a
datatable? If you just want to retrieve that value you could also use
a DataReader instead of the DataAdapter/DataTable pair.

Thanks,

Seth Rowe
On Apr 5, 2:24 am, "Jeff" <n...@nothingX.comwrote:
I'm still new to vb.net and even newer to using stored procedures in mysql.
The called procedure will return a single row from the DB.
I'm not sure how to get that value into a DataTable. I think I need to use
some type of adapter. Could someone help me complete this?

Thanks in advance
Jeff

Dim connection2 As New OdbcConnection(MyConnString)
Dim command2 As New OdbcCommand("call SelectFromParticipants(?)",
connection2)
Dim param As New OdbcParameter("User", OdbcType.VarChar)
param.Value = Session("User")
command2.Parameters.Add(param)
connection2.Open()
command2.ExecuteNonQuery()
DataTable = <- not sure how to complete this.
connection2.Close()

--
Posted via a free Usenet account fromhttp://www.teranews.com

Apr 5 '07 #2

"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...
...I left out the disposing statements for simplicity's sake
(though you should dispose of these objects to ensure fast cleanup) I
usually wrap the usage of each object in using statements.

Also, is there a reason you want to dump a single row into a
datatable? If you just want to retrieve that value you could also use
a DataReader instead of the DataAdapter/DataTable pair.

Seth Rowe
It worked. Thanks.
By "using statements," I assume that you mean something like below?
This works, but I am not yet certain that I understand the cleanup issue and
when I do and don't need to consider cleanup.
I am using dataAdapter/DataTable for a single row only because the first
code that I learned required an entire table and I don't yet have an example
for a single row (i.e., the WWWTable below contains multiple rows).
Everytime I write something like this, Dim sss As New DataRow , I get an
error about sss being "protected" and can't go further. Would you be so kind
as to provide an example or give me a hint about what might be wrong?

Jeff

WWWTable = New DataTable
UUUTable = New DataTable
Using conn As New OdbcConnection(MyConnString)
Dim adapter As OdbcDataAdapter = New OdbcDataAdapter("call
XXX", conn)
adapter.Fill(WWWTable)

Dim command As New OdbcCommand("call YYY(?)", conn)
command.Parameters.Add("User", OdbcType.VarChar).Value =
Session("User")
Dim da As New OdbcDataAdapter(command)
da.Fill(UUUTable)
End Using

--
Posted via a free Usenet account from http://www.teranews.com

Apr 5 '07 #3
By "using statements," I assume that you mean something like below?

Yes, only I would also use them on the command and adapter objects
(IMO you should use them on any object the implements the IDisposable
interface (has a dispose method)).
This works, but I am not yet certain that I understand the cleanup issue and
when I do and don't need to consider cleanup.
**Note - I'm not trying to start another Dispose/Don't Dispose war
here**

Consider the following:

Public Sub Foo()
Dim connection as new OleDbConnection(connString)
Dim com as OleDbCommand = conn.CreateCommand()
com.commandtext = "Select * From SomeWhere"
com.ExecuteNonQuery()
End Sub

As is, that procedure will create two objects(a connection and command
object) and use them. When the procedure exits, both of these objects
will go out of scope and be marked for collection. Eventually, the
garbage collector (the GC) will execute it's collect method and "spot"
these two objects and will call their Finalize methods. The Finalize
methods will call the objects Dispose method which will do the actual
clean up of resources it uses. After having the Finalize method called
by the GC, the object will be collected during the next garbage
collection cycle. During the time between the objects going out of
scope and being collected by the GC, the objects are needlessly using
resources.

So we can change the code to this and use Using statements to manually
dispose of the objects:

Public Sub Foo()
Dim conn as new OleDbConnection(connString)
Using (conn)
Dim com as OleDbCommand = conn.CreateCommand()
Using (com)
com.commandtext = "Select * From SomeWhere"
com.ExecuteNonQuery()
End Using
End Using
End Sub

This way, as soon as the using statements finish the objects will
dispose of themselves and clean up any resources they uses, as well as
suppressing their finalize methods. The first benefit of this is that
the object is no longer needlessly holding on to other resources while
it waits to go through the garbage collection process. The second
benefit is that when the GC executes, it will see that the objects
don't need to have there Finalize methods called (because of the
suppressfinialize call in the dispose method), and will immediately
collect the object, instead of having to wait until the next
collection cycle to do so.

I hope I made sense there, I was typing awfully fast. If you have any
more questions please ask and I'll try to answer them!

Thanks,

Seth Rowe

On Apr 5, 9:35 am, "Jeff" <n...@nothingX.comwrote:
"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message

news:11**********************@o5g2000hsb.googlegro ups.com...
...I left out the disposing statements for simplicity's sake
(though you should dispose of these objects to ensure fast cleanup) I
usually wrap the usage of each object in using statements.
Also, is there a reason you want to dump a single row into a
datatable? If you just want to retrieve that value you could also use
a DataReader instead of the DataAdapter/DataTable pair.
Seth Rowe

It worked. Thanks.
By "using statements," I assume that you mean something like below?
This works, but I am not yet certain that I understand the cleanup issue and
when I do and don't need to consider cleanup.
I am using dataAdapter/DataTable for a single row only because the first
code that I learned required an entire table and I don't yet have an example
for a single row (i.e., the WWWTable below contains multiple rows).
Everytime I write something like this, Dim sss As New DataRow , I get an
error about sss being "protected" and can't go further. Would you be so kind
as to provide an example or give me a hint about what might be wrong?

Jeff

WWWTable = New DataTable
UUUTable = New DataTable
Using conn As New OdbcConnection(MyConnString)
Dim adapter As OdbcDataAdapter = New OdbcDataAdapter("call
XXX", conn)
adapter.Fill(WWWTable)

Dim command As New OdbcCommand("call YYY(?)", conn)
command.Parameters.Add("User", OdbcType.VarChar).Value =
Session("User")
Dim da As New OdbcDataAdapter(command)
da.Fill(UUUTable)
End Using

--
Posted via a free Usenet account fromhttp://www.teranews.com

Apr 5 '07 #4

"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...
I hope I made sense there, I was typing awfully fast. If you have any
more questions please ask and I'll try to answer them!
Seth Rowe
....made sense. I'll try the code. ...but could you give me a brief example
of using the datarow rather than the data table for the second part of my
code below where UUUTable is really a single row. When I try, I get close,
but something is still missing.

Thanks

Jeff
> WWWTable = New DataTable
UUUTable = New DataTable
Using conn As New OdbcConnection(MyConnString)
Dim adapter As OdbcDataAdapter = New
OdbcDataAdapter("call
XXX", conn)
adapter.Fill(WWWTable)

Dim command As New OdbcCommand("call YYY(?)", conn)
command.Parameters.Add("User", OdbcType.VarChar).Value =
Session("User")
Dim da As New OdbcDataAdapter(command)
da.Fill(UUUTable)
End Using


--
Posted via a free Usenet account from http://www.teranews.com

Apr 5 '07 #5
...but could you give me a brief example
of using the datarow rather than the data table for the second part of my
code below where UUUTable is really a single row. When I try, I get close,
but something is still missing.
Oops, I meant to do that in the last post, but I went to lunch in the
middle of my reply and forgot about it when I got back :-)

Here's two methods for retrieving the single row, the first uses a
DataRow, while the second uses a DataReader instead. Both will show a
messagebox for each value in the datarow/datareader. Also, note I used
the System.Data.SqlClient class instead of the System.Data.Obdc class,
so you'll need to change that.

' Using a DataRow
Private Sub Foo()
Dim conn As New SqlConnection(connString)
Using (conn)
conn.Open()
Dim com As SqlCommand = conn.CreateCommand()
Using (com)
com.CommandType = CommandType.Text
com.CommandText = "Select Top 1 * From MyTable"
Dim da As New SqlDataAdapter(com)
Using (da)
Dim dt As New DataTable("My Table")
Using (dt)
da.Fill(dt)
Dim dr As DataRow = dt.Rows(0)
For i As Int32 = 0 To dr.ItemArray.Length - 1
MessageBox.Show(dr(i).ToString())
Next
End Using
End Using
End Using
End Using
End Sub

' Using the DataReader
Private Sub Foo2()
Dim conn As New SqlConnection(connString)
Using (conn)
conn.Open()
Dim com As SqlCommand = conn.CreateCommand()
Using (com)
com.CommandType = CommandType.Text
com.CommandText = "Select Top 1 * From MyTable"
Dim dr As SqlDataReader =
com.ExecuteReader(CommandBehavior.SingleRow)
Using (dr)
If (dr.Read()) Then
For i As Int32 = 0 To dr.FieldCount - 1
MessageBox.Show(dr.GetValue(i).ToString())
Next
End If
End Using
End Using
End Using
End Sub
I hope that helps!

Thanks,

Seth Rowe
On Apr 5, 12:34 pm, "Jeff" <no_...@george.comwrote:
"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message

news:11**********************@e65g2000hsc.googlegr oups.com...
I hope I made sense there, I was typing awfully fast. If you have any
more questions please ask and I'll try to answer them!
Seth Rowe

...made sense. I'll try the code. ...but could you give me a brief example
of using the datarow rather than the data table for the second part of my
code below where UUUTable is really a single row. When I try, I get close,
but something is still missing.

Thanks

Jeff
WWWTable = New DataTable
UUUTable = New DataTable
Using conn As New OdbcConnection(MyConnString)
Dim adapter As OdbcDataAdapter = New
OdbcDataAdapter("call
XXX", conn)
adapter.Fill(WWWTable)
Dim command As New OdbcCommand("call YYY(?)", conn)
command.Parameters.Add("User", OdbcType.VarChar).Value =
Session("User")
Dim da As New OdbcDataAdapter(command)
da.Fill(UUUTable)
End Using

--
Posted via a free Usenet account fromhttp://www.teranews.com

Apr 5 '07 #6

"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11**********************@n59g2000hsh.googlegr oups.com...
>
I hope that helps!

Thanks,

Seth Rowe

Yes. Thanks. ...but now that I see how this is accomplished, I am wondering
why exactly it is better to use a DataRow instead of a DataTable when
returning a single row. ...i.e. you actually return the entire table first
and then subsequently strip off all but the first row, so you have an extra
computational step. If you just left the query result in the form of a
table, you would save that step. ...but perhaps this is offset by how vb
manages memory? Does it matter?

Jeff


--
Posted via a free Usenet account from http://www.teranews.com

Apr 5 '07 #7
On Apr 5, 7:47 pm, "Jeff" <no_...@george.comwrote:
"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message

news:11**********************@n59g2000hsh.googlegr oups.com...
I hope that helps!
Thanks,
Seth Rowe

Yes. Thanks. ...but now that I see how this is accomplished, I am wondering
why exactly it is better to use a DataRow instead of a DataTable when
returning a single row. ...i.e. you actually return the entire table first
and then subsequently strip off all but the first row, so you have an extra
computational step. If you just left the query result in the form of a
table, you would save that step. ...but perhaps this is offset by how vb
manages memory? Does it matter?

Jeff

--
Posted via a free Usenet account fromhttp://www.teranews.com
What do you plan on doing with the single row returned from the
database? If you just want to retrieve the value(s) then using a
datareader is going to be your best bet. DataRows and DataTables are
too much overhead to just get some values from a database.

Thanks,

Seth Rowe

Apr 6 '07 #8

"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...
What do you plan on doing with the single row returned from the
database? If you just want to retrieve the value(s) then using a
datareader is going to be your best bet. DataRows and DataTables are
too much overhead to just get some values from a database.
Seth Rowe
The application requires me to use a number of values from the DB, but only
one row at a time. I'm still new to this and haven't yet learned to use the
datareader. ...don't suppose that you would want to provide an example
where a few columns are placed into variables?

Jeff

--
Posted via a free Usenet account from http://www.teranews.com

Apr 6 '07 #9

"Jeff" <no**@nothingX.comwrote in message
news:46***********************@free.teranews.com.. .
>
....don't suppose that you would want to provide an example
where a few columns are placed into variables?

Oops, I skimmed your earlier post and just looked at it more carefully and
realized that you already did this.

Thanks.

....but it would seems that the use of the datarow might be better in the
case where the values are a mixture of integer, character, etc.?

Jeff

--
Posted via a free Usenet account from http://www.teranews.com

Apr 6 '07 #10
On Apr 5, 9:30 pm, "Jeff" <n...@nothingX.comwrote:
"Jeff" <n...@nothingX.comwrote in message

news:46***********************@free.teranews.com.. .

...don't suppose that you would want to provide an example
where a few columns are placed into variables?

Oops, I skimmed your earlier post and just looked at it more carefully and
realized that you already did this.

Thanks.

...but it would seems that the use of the datarow might be better in the
case where the values are a mixture of integer, character, etc.?

Jeff

--
Posted via a free Usenet account fromhttp://www.teranews.com
...but it would seems that the use of the datarow might be better in the
case where the values are a mixture of integer, character, etc.?
In my opinion the datareader is better for getting db values into
variables. I really only use DataTables when I display data to the
user and have him/her change things, and then I update the database
from that. For simple data retrieval I always rely on a datareader to
do the job.

The datareader is forward reading only, moving forward one "row"
whenever you call it's .Read() method. You pull the information out of
the reader by it's column index in it's GetType() method.

So you could do something like this:

' This is all typed in the message so it may have some errors

Dim dr as DataReader = command.ExecuteReader()
while (dr.Read())
Dim int as Int32 = dr.GetInt32(0)
Dim str as String = dr.GetString(1)
Dim dbl as Double = dr.GetDouble(2)
' Then do something with the variables
end while

Thanks,

Seth Rowe

Apr 6 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: post400 | last post by:
Hi, apparently there is a very famous book that every developer should read: Code complete by Steve McConnell ! Is there an electronic version freely downloadable ? After all, the book was...
0
by: Dustin Lee | last post by:
Seems like py-complete.el offers some cool features, if I could only get it to work. >From the file ;; 1. hippie-expand support via try-complete-py-complete-symbol, ;; just like the...
3
by: mvdevnull | last post by:
static void Main(string args) { DoSomething(); } static void DoSomething() { for (int i=0; i<=10; i++) { CallAsyncMethod(); } } my problem is when i run the app console exists without really...
4
by: Max | last post by:
Hi guys, did some searching on the topic, but can't find much more then just basic info on binary trees. I need to write a binary tree implementation so that it is always complete. In other words...
4
by: Faw | last post by:
What is NP-complete? How do we prove that a problem is NP-complete? How do you rank NP, NP-complete, NP-hard?
0
by: george d lake | last post by:
Hi, Is there a way to have a "Auto Complete" textbox or a "editable" dropdown? Here is my problem. I have a screen that need to have a list of 800+ employees. To be a dropdown, that could be a...
1
by: Deepan Chakravarthy | last post by:
Hi, I am using fedora core 5. I would like to have auto complete in python prompt and history. How do i get it enabled ? i have .pystart file and .pyhist file setup. thanks Deepan...
8
by: dd | last post by:
Has anyone found a way around the problem IE has if you create elements (script or div, doesn't seem to matter) BEFORE the document.readyState is "complete" ? I know you can sometimes get away...
4
by: Weston | last post by:
I've been playing around with some means of testing image loading, and noticing that the "complete" property seems to default to true. Trying the following in the squarefree javascript shell: I...
1
by: glasssd | last post by:
Hi, I was hoping someone might have some ideas on how I can hack around a problem I'm having with the auto-complete feature of the ComboBox. I have a data entry application that uses a pair of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.