Valor de lectura de la fila de datos

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

Tengo este codigo:

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.

preguntado el 03 de mayo de 12 a las 12:05

4 Respuestas

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

Consulta este

contestado el 03 de mayo de 12 a las 13:05

Thats it? But what's with if function, I need it to set value in varibale CatchweightItem, the field that is beein red can be true or false and I need to reed that value - CrBruno

@Bruno dr_art_line["CatchweightItemt"].ToString() This line reads your string and Convert.ToBoolean will convert that string to bool value. whether your value is TRUE or true or TRue or TRUe it will assign CatchweightItem with true and if value is FALSE or False or false it will assign false to CatchweightItem. Test it. I have tested and used it n number of times. - Nikhil Agrawal

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")

contestado el 03 de mayo de 12 a las 13:05

But what if value is TrUe or FalSe or tRue or falsE or there are n number of ways this value is stored. In your case either use .ToLower() or ToUpper() o solo Convert.ToBoolean which just checks for all possible values of string - Nikhil Agrawal

You are wright, but the value is comming from xml document, and we aggred that it will write "false" - CrBruno

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.

contestado el 03 de mayo de 12 a las 13:05

 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.

contestado el 03 de mayo de 12 a las 13:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.