473,574 Members | 3,140 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Datareader(simp le)

SqlCommand findProducts = new SqlCommand("SEL ECT PRODUCT_TYPE_ID FROM
PRODUCT_TYPE WHERE [NAME] = 'Meals'", sqlConnection1) ;

This command returns a value of 17. How can i insert this value into
a string variable? Please help . I tried this but it wouldnt work

SqlDataReader myReader = findProducts.Ex ecuteReader();
while (myReader.Read( ))
{
ProdTypeID =
int.Parse(myRea der["PRODUCT_TYPE_I D"].ToString());
}
Apr 10 '08 #1
1 1293
If it only returns a single cell, then ExecuteScalar() might be an
easier option; since you state that you want a string, the easiest
option would then be:

string foo = Convert.ToStrin g(findProducts. ExecuteScalar() );

How to best use data-reader depends on whether PRODUCT_TYPE_ID is an int
or a [n][var]char;
if int:
string foo = reader.GetInt32 (0).ToString();
if [n][var]char:
string foo = reader.GetStrin g(0);

You could also use:
string foo = Convert.ToStrin g(reader[0]);
or
string foo = Convert.ToStrin g(reader["PRODUCT_TYPE_I D"]);

Marc
Apr 10 '08 #2

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

Similar topics

5
2625
by: Stephanie_Stowe | last post by:
Hi. I am trying to get used to AS.NET. I have been doing ASP classic for years, and am now in a position to do ASP.NET. I am in the stumbling around until I get my bearings phase. I hope you will bear with me. I am going through the QuickStart. After reading a little, I am trying to implement a simple page on a simple project I have made up....
10
1850
by: Scott Richards | last post by:
Hi I am getting a exception while using a datareader here is the code Me.SqlConnection1.Open() Me.SqlReservationCheck.Parameters("@Ref").Value = Ref Dim Reader As SqlClient.SqlDataReader = _ Me.SqlReservationCheck.ExecuteReader() Dim I As Integer() Dim Index As Integer = 0 If Reader.HasRows Then While Reader.Read()
14
2230
by: Bihn | last post by:
I was reading about datareader which is said to be slimmer & faster then dataset. Since the datareader have to go fetching the dat from the database every time it need it, the data it gets then should be up to date. However, both the IbuySpy and Duwamish samples and most, if not all, the shopping cart sample codes I've seen use dataset to...
2
989
by: Shapper | last post by:
Hello, I want to retrive N records from a database (I am using SQL and Access in 2 different projects): id class text 1 book book comments 2 paper paper comments ....
4
1166
by: Shapper | last post by:
Hello, I have this code to bind a DataGrid to a DataReader when page loads: Sub Page_Load(Sender As Object, e As EventArgs) dgContent.DataSource = drContent dgContent.DataBind() End Sub It's not working. What is wrong?
2
1579
by: DaveS | last post by:
Hi! I am trying to create a DataReader in .Net 2003, retrieving data from an Access backend db. In the code below, when I put a breakpoint within the While...Read() loop, the datareader returns five records (which is correct). However, when I simply run the program without the breakpoint, only one record is being returned. Is there...
11
1707
by: ^MisterJingo^ | last post by:
Hi all, I have a form with 4 dropdownlist controls which I populate with data from DB tables. I have a class with a method which constructs a dataset, putting each DB table into a dataset table. I then return the DS and bind the tables to the relevant controls. I've been reading that DataReaders are much more efficient than DS's for...
1
3064
by: Steve | last post by:
Hi, I currently display all the data on a website using tableadaptors and objectdatasources. Would it be significantly faster if I was to display the data by writing the code for a datareader and connecting to the database directly and populating my tables that way? Or is the tableadaptor using a datareader anyway for simple select...
20
7194
by: fniles | last post by:
I am using VB.NET 2003, SQL 2000, and SqlDataReader. As I read data from tblA, I want to populate tblB. I use SQLDataReader for both tables. I do not use thread. When I ExecuteReader on tblB, I get the error "There is already an open DataReader associated with this Connection which must be closed first." How can I fix this error ? For each...
0
7805
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
7726
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...
0
8234
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7815
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
8098
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...
0
3740
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...
0
3749
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2243
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
1339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.