CrBruno
CrBruno

Reputation: 1003

Reading value from data row

Is it possible to read a value from a data row?

I have this code:

bool CatchweightItem;
if (dr_art_line["CatchweightItemt"].ToString() -> READ VALUE)
{
    CatchweightItem = false;
}
else
{
    CatchweightItem = true;
}

So I am wondering if it is possible to read a value of that field. If the value is false, set variable to false and if it's true set variable to true.

Upvotes: 0

Views: 2081

Answers (4)

Vetrivel mp
Vetrivel mp

Reputation: 1224

 if(string.Compare(Convert.ToString(dr_art_line["CatchweightItemt"])+"","false",true)==0)
         { 
CatchweightItem = false; }
 else {  
CatchweightItem = true; } 

This will avoid null value coming from database as well as case insensitive check.

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460288

If the DataColumn's DataType is bool anyway, you should use this strong-type method:

bool isCatchweightItem = dr_art_line.Field<bool>("CatchweightItemt");

DataRowExtensions.Field<T> Method (DataRow, String)

It also supports nullable types.

Upvotes: 1

Maximilian Mayerl
Maximilian Mayerl

Reputation: 11367

You have to use the == operator to check the value like so:

bool CatchweightItem;
if (dr_art_line["CatchweightItemt"].ToString() == "FALSE")
{
    CatchweightItem = false;
}
else
{
    CatchweightItem = true;
}

Actually, you don't need all that code, you can also do this much shorter and cleaner:

bool CatchweightItem = (dr_art_line["CatchweightItemt"].ToString() == "TRUE")

Upvotes: 1

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48600

bool CatchweightItem = Convert.ToBoolean(dr_art_line["CatchweightItemt"].ToString());

Read this

Upvotes: 1

Related Questions