473,790 Members | 2,528 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("SEL ECT NextPrbKey FROM SystemAdmin", cn)
Dim mySQLReader As SqlDataReader = mySQLCommand.Ex ecuteReader()
intGetNextKey = mySQLReader.Get Value(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 1626
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.Mac hineName + "\VSdotNet; "

strConn += "Database = NorthWind;"

strConn += "Integrated Security = SSPI;"

conn = New SqlConnection(s trConn)

cmd = New SqlCommand("Sel ect Customers.*, (Customers.Cont actTitle + ', ' +
Customers.Conta ctName) as FullName from Customers Order by ContactTitle,
ContactName", conn)

conn.Open()

drCustomer = cmd.ExecuteRead er

Do While drCustomer.Read

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

Loop

conn.Close()

Ken

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

"SQL" <no*****@adfadf d.com> wrote in message
news:OY******** ******@TK2MSFTN GP09.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("SEL ECT NextPrbKey FROM SystemAdmin",
cn)
Dim mySQLReader As SqlDataReader = mySQLCommand.Ex ecuteReader()
intGetNextKey = mySQLReader.Get Value(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.Rea d
intGetNextKey = mySqlReader.Get String(0)
End While

One bit of caution...you may want to check for IsDbNull using something like
an IIF(IsDbNull(my SqlReader(0),st ring.Empty, mySqlReader.Get String(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*****@adfadf d.com> wrote in message
news:OY******** ******@TK2MSFTN GP09.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("SEL ECT NextPrbKey FROM SystemAdmin", cn) Dim mySQLReader As SqlDataReader = mySQLCommand.Ex ecuteReader()
intGetNextKey = mySQLReader.Get Value(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
2363
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
5889
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
3265
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
2915
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 databind you can pass either of these classes (as well as many others) and .NET somehow knows how...
1
1431
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 sqldatareader but digging up the information was quite hard.
8
2084
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 any insight!
4
2366
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
1715
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 newbie problem related to OOP, but I am confused. Could you pl. look at the code or point me to...
0
9666
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9512
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9986
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7530
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5422
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3707
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.