SQL in C# - Grabbing a single value

I'm quite new to C# (I've programmed in C++ until now) and also don't have a whole lot of experience working with databases and SQL.

I'm currently working on a windows application that uses a database and have been relying on Visual Studio 2005's built-in controls to handle it for me (i.e. binding data to textboxes etc.). Now I need to be able to grab a value out of one of the tables (I know exactly which value) to use it in some calculations, but I don't know how to put the value into an int variable. This is what I want to happen:

int ta500stock = this.rentalManagerDataSet.StockList.Select("February WHERE (Product = 'TA500')");

*Note: StockList is the table and February is the column that I want the value from.

Now, the compiler tells me that I "Cannot implicitly convert type 'System.Data.DataRow[]' to 'int'" which makes sense, but I don't know how to set it up so that I can grab the value and store it as an int.

Any suggestions, advice, or links would be much appreciated.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
int ta500stock = this.rentalManagerDataSet.StockList.Select("February WHERE (Product = 'TA500')")["February"].ToInt();
KyleGAuthor Commented:
Wow that was a fast reply!

That gave me 2 errors:

Cannot implicitly convert type 'string' to 'int' (this refers to the ["February"] part)
'System.Data.DataRow' does not contain a definition for 'ToInt'

Any thoughts?

- Kyle
Guy Hengel [angelIII / a3]Billing EngineerCommented:
This should work:
DataRow dr = this.rentalManagerDataSet.StockList.Select("February WHERE (Product = 'TA500')");
int ta500stock = dr["February"].ToInt();
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

KyleGAuthor Commented:
Sorry angelIII, that didn't work either. Now I get these:

Cannot implicitly convert type 'System.Data.DataRow[]' to 'System.Data.DataRow'
'object' does not contain a definition for 'ToInt'

I'm sure there's a very easy fix for both these problems, and I know when I get it working I won't be able to believe how simple the solution was :S
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Silly me: the SELECT method returns an array of DataRow, not a single DataRow, hence (not sure if you need to use [0] or [1]:

DataRow dr = this.rentalManagerDataSet.StockList.Select("February WHERE (Product = 'TA500')")[0];
int ta500stock = dr["February"].ToInt();

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KyleGAuthor Commented:
Perfect, that fixed the first problem, thanks. I'm still getting an error that says there's no definition for ToInt(). Do I need to add another "using ..."?

- Kyle
        dr["February"] is an object and has no ToInt method. Make it like this:

        int ta500stock =Convert.ToInt32(dr["February"]);
KyleGAuthor Commented:
A HAH! That did it, thanks a lot sumix and angelIII. Is there a way I can split up the points. since you both helped out?

 I guess it is, but it's ok to give them to angeIII, I've got some more ...
KyleGAuthor Commented:
If you say so. Thanks again to both of you.

- Kyle
Guy Hengel [angelIII / a3]Billing EngineerCommented:
yes, below the last comment there you find a "split points" link, I think sumix has pointed out an important part of the solution.
I am only a beginner in C#, still too used to VB6 :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.