Solved

Creating a method to retun a vaule from an SQL query

Posted on 2004-09-24
6
184 Views
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;

                  try
                  {
                        
                        strSQL = "SELECT product_price from t_products where product_id =productID";
                        cnSQL = new SqlConnection(Connectionstring);
                        cnSQL.Open();
                        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.
}


Thanks,
Sean
0
Comment
Question by:sean-keys
  • 3
  • 2
6 Comments
 

Expert Comment

by:dubbs2003
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)
0
 

Expert Comment

by:dubbs2003
ID: 12147635
you can return

drSQL.GetSqlValues(Object[]);

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

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

//Returns:
//The number of instances of System.Object in the array.
0
 

Expert Comment

by:dubbs2003
ID: 12147652
So you return the Object[] after you call GetSqlValues then just iterate through the columns
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:sean-keys
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?????

drSQL.GetSqlValues(product_price[]);


Thanks in advance




0
 
LVL 2

Accepted Solution

by:
rajkumarrg earned 125 total points
ID: 12149513
Hi

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

Thanks
Raj
0
 

Author Comment

by:sean-keys
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);
                  getprice.Connection.Open();
                  price = getprice.ExecuteScalar().ToString();
                  cnSQL.Close();
                  
            }


Thanks for all your help,
Sean
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This is an explanation of a simple data model to help parse a JSON feed
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

929 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now