开发者

Getting error on 2nd line in code below..What could be wrong?

开发者 https://www.devze.com 2023-01-26 05:20 出处:网络
for (I = 0; I < ds.Tables[0].Rows.Count; I++) { if (Convert.ToBoolean(ds.Tables[0].Rows[I][\"IsDefault\"].ToString()) == true && Convert.ToBoolean(ds.Tables[0].Rows[I][\"IsBill\"].ToString
for (I = 0; I < ds.Tables[0].Rows.Count; I++)
{
 if (Convert.ToBoolean(ds.Tables[0].Rows[I]["IsDefault"].ToString()) == true && Convert.ToBoolean(ds.Tables[0].Rows[I]["IsBill"].ToString()) == true)
.
.
.
.

Err开发者_如何转开发or: System.FormatException: String was not recognized as a valid Boolean.

Is default and IsBill are boolean values only


I think some values are null in your table.

if (ds.Tables[0].Rows[I]["IsDefault"] != null && ds.Tables[0].Rows[I]["IsBill"]) != null)
    {
        if (Convert.ToBoolean(ds.Tables[0].Rows[I]["IsDefault"].ToString()) == true && Convert.ToBoolean(ds.Tables[0].Rows[I]["IsBill"]..ToString()) == true)
        {


        }

    }

You might have to use Convert.IsDBNull function to check whether the value is NULL.


Obviously you aren't too comfortable with some of the debugging tools that are available in Visual Studio.

In future, if you wanted to figure this out yourself, I can suggest a couple of debugging methods:

1) Put a breakpoint in at that line and then mousing over the .ToString() method to see what it is returning 2) When you are at the breakpoint, click on the tab that says "immediate window", and then type the call that might be returning something odd, in this case ds.Tables[0].Rows[I]["IsDefault"].ToString() You can then see the result that is being returned by your call and you'll most likely solve your own bugs!

Edit: I was wrong about the cause, but I'd think a suggestion on good debugging practice is worthwhile! Good luck!


Check the values of IsDefault and IsBill columns in your DB, if you can. Do you have a bit column for them? If yes then it might be returning 1 or 0 and you may be trying to convert them into boolean which is not so easily done.

Also, check for null. The FormatException comes when the value you're trying to convert can't possibly be converted on the target type.

UPDATE: I think you've got the parenthesis wrong here. Your if is ending immediately after the first ToString()

if (Convert.ToBoolean(ds.Tables[0].Rows[I]["IsDefault"].ToString()) == true && Convert.ToBoolean(ds.Tables[0].Rows[I]["IsBill"].ToString()) == true)


Something can be improved in your code.

(1) Usually we use a lower case variable i in the loop.

(2) Don't use column names directly in your code. Suppose some day, the column name is changed from "isDefault" to "DefaultOrNot"(just an example), it's a nightmare because you need to modify the strings everywhere in your code. An enum is helpful here.

enum TableColumns //well, it's a bad name
{
   ID = 0,
   Name = 1,
   IsDefault = 2   
}

and visit your table cell with table.Rows[0][(int)TableColumns.IsDefault]. You need to modify the enum only if any columns are added or changed.

(3) I think the format exception is because of that the cell's value is T/F instead of True/False. So converting it to bool type will cause a format exception.

(4) You'd better to check if there is an value in this cell before using it.

EDIT: Are you storing the value in DB with 1/0 (1 for true and 0 for false)? So you should use:

object cell = table.Rows[0]["IsDefault"];
if (cell != null && !Convert.IsDBNull(cell))
{
    if (cell.ToString().Equals("1"))
    {
        //do something
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消