471,337 Members | 1,165 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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


SqlCommand findProducts = new SqlCommand("SELECT 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.ExecuteReader();
while (myReader.Read())
ProdTypeID =
Apr 10 '08 #1
1 1213
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.ToString(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.GetString(0);

You could also use:
string foo = Convert.ToString(reader[0]);
string foo = Convert.ToString(reader["PRODUCT_TYPE_ID"]);

Apr 10 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Stephanie_Stowe | last post: by
10 posts views Thread by Scott Richards | last post: by
14 posts views Thread by Bihn | last post: by
4 posts views Thread by Shapper | last post: by
2 posts views Thread by DaveS | last post: by
11 posts views Thread by ^MisterJingo^ | last post: by
1 post views Thread by Steve | last post: by

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.