Creating a method to retun a vaule from an SQL query

Posted on 2004-09-24
Last Modified: 2010-04-17
I’m trying to create a method so that when it is called, it will return a value based on what it received.   Here is what I have come up with so far.  I guess you could say I'm still a C# dummy.

private void GetProductPrice(int productID)

                  SqlConnection cnSQL;

                  SqlCommand cmSQL;

                  SqlDataReader drSQL;
                  string strSQL;

                        strSQL = "SELECT product_price from t_products where product_id =productID";
                        cnSQL = new SqlConnection(Connectionstring);
                        cmSQL = new SqlCommand(strSQL, cnSQL);
                        drSQL = cmSQL.ExecuteReader();
                        return drSQL.?????;   //at this point I'm not sure how to return a value to the method that invoked it.

Question by:sean-keys
  • 3
  • 2

Expert Comment

ID: 12147462
Your method declaration has a type 'void' return identifier.

i.e. private void GetProductPrice(int productID)

If you want to return a value you have to change void to the type of value you are returning

i.e. private String GetProductPrice(int productID)

Expert Comment

ID: 12147635
you can return


//Gets all attribute columns in the collection for the current row.  

//values: An array of System.Object into which to copy the attribute columns.

//The number of instances of System.Object in the array.

Expert Comment

ID: 12147652
So you return the Object[] after you call GetSqlValues then just iterate through the columns
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.


Author Comment

ID: 12147815
I think I get it, since this is my command "SELECT product_price from t_products where product_id =productID", which will only return one value

I would want this?????


Thanks in advance


Accepted Solution

rajkumarrg earned 125 total points
ID: 12149513

You can use ExecuteScalar instead of ExecuteReader as you going to get only one value.

And in return type instead of void use string.

Hope it would be useful for you


Author Comment

ID: 12165036
This is what I ended up with.

public void GetProductPrice(int productID)
                  int ID = productID;
                  string strSQL = "SELECT product_price from t_products where product_id = productID";

                  SqlConnection cnSQL;

            cnSQL = new SqlConnection(Connectionstring);
                  SqlCommand getprice = new SqlCommand(strSQL, cnSQL);
                  price = getprice.ExecuteScalar().ToString();

Thanks for all your help,

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question