473,513 Members | 2,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQLDataReader

SQL
I'm new at this, so please forgive me if my question has a simple answer to
it.

I'm trying to get a value out of a SqlDataReader. Here is my code to do so:

Dim mySQLCommand As New SqlCommand("SELECT NextPrbKey FROM SystemAdmin", cn)
Dim mySQLReader As SqlDataReader = mySQLCommand.ExecuteReader()
intGetNextKey = mySQLReader.GetValue(0)

This does not return a value. I thought that GetValue(0) would return the
value of column 0 in my result set. What am I doing wrong?
Thanks.

Nov 20 '05 #1
2 1613
Hi,

Here is a simple datareader example.

Dim strConn As String

Dim conn As SqlConnection

Dim drCustomer As SqlDataReader

Dim cmd As SqlCommand

strConn = "Server = " + Environment.MachineName + "\VSdotNet;"

strConn += "Database = NorthWind;"

strConn += "Integrated Security = SSPI;"

conn = New SqlConnection(strConn)

cmd = New SqlCommand("Select Customers.*, (Customers.ContactTitle + ', ' +
Customers.ContactName) as FullName from Customers Order by ContactTitle,
ContactName", conn)

conn.Open()

drCustomer = cmd.ExecuteReader

Do While drCustomer.Read

ComboBox1.Items.Add(drCustomer("FullName"))

Loop

conn.Close()

Ken

---------------

"SQL" <no*****@adfadfd.com> wrote in message
news:OY**************@TK2MSFTNGP09.phx.gbl...
I'm new at this, so please forgive me if my question has a simple answer
to
it.

I'm trying to get a value out of a SqlDataReader. Here is my code to do
so:

Dim mySQLCommand As New SqlCommand("SELECT NextPrbKey FROM SystemAdmin",
cn)
Dim mySQLReader As SqlDataReader = mySQLCommand.ExecuteReader()
intGetNextKey = mySQLReader.GetValue(0)

This does not return a value. I thought that GetValue(0) would return the
value of column 0 in my result set. What am I doing wrong?
Thanks.

Nov 20 '05 #2
You haven't moved the position.... Wrap the intGetNextKey line in between

While mySqlReader.Read
intGetNextKey = mySqlReader.GetString(0)
End While

One bit of caution...you may want to check for IsDbNull using something like
an IIF(IsDbNull(mySqlReader(0),string.Empty, mySqlReader.GetString(0))

b/c the way it works now, you'll throw an exception if you pull in a Null
value from the db. Of course, this won't be a problem if you're sure there
are no nulls, but it's worth mentioning nonetheless.

HTH,

Bill
"SQL" <no*****@adfadfd.com> wrote in message
news:OY**************@TK2MSFTNGP09.phx.gbl...
I'm new at this, so please forgive me if my question has a simple answer to it.

I'm trying to get a value out of a SqlDataReader. Here is my code to do so:
Dim mySQLCommand As New SqlCommand("SELECT NextPrbKey FROM SystemAdmin", cn) Dim mySQLReader As SqlDataReader = mySQLCommand.ExecuteReader()
intGetNextKey = mySQLReader.GetValue(0)

This does not return a value. I thought that GetValue(0) would return the
value of column 0 in my result set. What am I doing wrong?
Thanks.

Nov 20 '05 #3

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

Similar topics

3
536
by: Ricola ! | last post by:
Why do I say: SqlDataReader dr; instead of SqlDataReader dr = new SqlDataReader();
7
2345
by: Franck Diastein | last post by:
Hi, when I call ExportData I have this error: Invalid attempt to Read when reader is closed. Telling me that there's a problem with this line: while(_dataR.Read()){ Code: ************************************************* public void Export2CSV(){
3
5869
by: Neil Guyette | last post by:
Hello, Everyone, I'm trying to find information on how to populate a combo box using a SqlDataReader. I want to be able to set the value of the combo's value property different then the combo's text property (what the user will see). Is this possible with a SqlDataReader? Thanks
1
3246
by: Arvind P Rangan | last post by:
Hi All, How do you get all the values of a sqldatareader if it contains multiple resultset. Using sqldatareader.nextresult and sqldatareader.read e.g. While sqldatareader.read ' If not sqldatareader then sqldatareader.nextresult
4
2878
by: Michael Carr | last post by:
I have a function that populates a class with values from a database. I'd like to pass into the function either a SqlDataReader or a DataRow, depending on which mechanism I'm using to retrieve data from the database. However, the two classes don't appear to have any common interfaces that would allow me to enumerate the fields. Yet, when you...
1
1420
by: me | last post by:
Why is SqlDataReader not present in visual studios Toolbox->Data where I find components like DataSet? Most examples in books and in vs.net are based on datasets but using disconnected tables seems a bit heavyweight for most purposes. I there some particular reason to stay away from sqldatareader?? I have managed to bind components using...
8
2059
by: bidllc | last post by:
I have a funtion that works fine and dandy when called from anywhere in my app. It will NOT work when called from inside the class in which it resides. This is the function I'm calling: "getProductByID(productID)" from inside another method in the same class. See below. This line throws a null ref exception: While dr.Read() Thanks for...
4
2356
by: mimi | last post by:
Hi Please help me out, I can't find a way to close a sqldatareader when error occur at statement cmd.ExecuteReader(). I can't close it in catch because it is local in try scope and I can't declare it outside try scope either since we have to call cmd.executeReader to create sqldatareader public string GetLogs(int logID) {
7
1698
by: Web learner | last post by:
I am trying to create a method GetDataFor(string column) becaues I have to repeat the same statements for several columns but I get an error as follows: The name 'dr' does not exist in the current context It seems the dr -the instance of sqlDataReader - is not becoming available to the method. How to make it available? This seems trivial and...
0
7270
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7397
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7128
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7543
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5103
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3242
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
817
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
473
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.