Link to home
Start Free TrialLog in
Avatar of KyleG
KyleG

asked on

SQL in C# - Grabbing a single value

Experts,
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.

Thanks,
Kyle
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

int ta500stock = this.rentalManagerDataSet.StockList.Select("February WHERE (Product = 'TA500')")["February"].ToInt();
Avatar of KyleG
KyleG

ASKER

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
This should work:
DataRow dr = this.rentalManagerDataSet.StockList.Select("February WHERE (Product = 'TA500')");
int ta500stock = dr["February"].ToInt();
Avatar of KyleG

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KyleG

ASKER

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"]);
Avatar of KyleG

ASKER

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 ...
Avatar of KyleG

ASKER

If you say so. Thanks again to both of you.

- Kyle
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 :-)