Ideally, when you are fetching values from db, you should convert null values to some known value like empty space. You can check the value before converting it. If it is empty space set a 0 value may be.
Please Mark As Answer if it helped.
MCPD ASP.NET 4.0 and 3.5, MCTS WSS, MOSS, SharePoint 2010, MCT
Microsoft Community Contributor Award 2011
You can use Extension Methods to simplify the task
Add the following class to your project..
public static class MyExtensions
{
public static double ToDouble(this string value)
{
double d = 0;
double.TryParse(value, out d);
return d;
}
public static int ToInt32(this string value)
{
int i = 0;
int.TryParse(value, out i);
return i;
}
}
patricktlc
Member
2 Points
14 Posts
Input string was not in a correct format.
Jun 07, 2012 10:38 AM|LINK
Hi there.
Ive got a DataGridView that pulls data from a SQL Database
There are a few cells in the grid that has null values.
Now ive got some code that is suposed to change the backcolor of a certain row based on certain paramaters.
The program runs through the code fine until it encounters a null value and then it throws me a "Input string was not in a correct format." error.
How can i bypass this?
Here is a code snipet:
(ps. i know its messy and there are probibly a few other errors in there, but they can be fixed once i fix this problem first)
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { foreach (GridViewRow row in GridView1.Rows) { if (e.Row.RowType == DataControlRowType.DataRow) { int StationStatus1 = Int32.Parse(GridView1.DataKeys[e.Row.RowIndex].Values[0].ToString()); int Status24 = 0; Double MinRFL = 0.0; Double MaxRFL = 0.0; Double MinRFR = 0.0; Double MaxRFR = 0.0; MinRFL = Double.Parse(e.Row.Cells[13].Text); MaxRFL = Double.Parse(e.Row.Cells[14].Text); MinRFR = Double.Parse(e.Row.Cells[15].Text); MaxRFR = Double.Parse(e.Row.Cells[16].Text); switch (StationStatus1) { case 0: case 1: case 2: case 3: if (MinRFL <= -900.0 || MaxRFL >= 900.0 || MinRFR <= -900.0 || MaxRFR >= 900.0) { Status24 = 3; } if (((MinRFL > -900.0) && (MinRFL <= -800.0) || (MaxRFL >= 800.0) && (MaxRFL < 900.0)) || ((MinRFR > -900.0) && (MinRFR <= -800.0) || (MaxRFR >= 800.0) && (MaxRFR < 900.0))) { Status24 = 2; } if (((MinRFL > -800.0) && (MinRFL <= -600.0) || (MaxRFL >= 600.0) && (MaxRFL < 800.0)) || ((MinRFR > -800.0) && (MinRFR <= -600.0) || (MaxRFR >= 600.0) && (MaxRFR < 800.0))) { Status24 = 1; } switch (Status24) { case 0: e.Row.BackColor = System.Drawing.Color.White; break; case 1: e.Row.BackColor = System.Drawing.Color.Yellow; break; case 2: e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml("#0166FE"); break; case 3: e.Row.BackColor = System.Drawing.Color.Red; break; default: e.Row.BackColor = System.Drawing.Color.White; break; } //e.Row.BackColor = System.Drawing.Color.Red; break; case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 12: e.Row.BackColor = System.Drawing.Color.Blue; break; default: e.Row.BackColor = System.Drawing.Color.White; break; } } } } }urenjoy
Star
11997 Points
1797 Posts
Re: Input string was not in a correct format.
Jun 07, 2012 10:43 AM|LINK
Try following:
OR
Use String.IsNullOrEmpty method
patricktlc
Member
2 Points
14 Posts
Re: Input string was not in a correct format.
Jun 07, 2012 10:47 AM|LINK
Thanks.
That worked for StationStatus1, but not for the MinRFL,ect.
adeelehsan
All-Star
18221 Points
2724 Posts
Re: Input string was not in a correct format.
Jun 07, 2012 10:48 AM|LINK
Ideally, when you are fetching values from db, you should convert null values to some known value like empty space. You can check the value before converting it. If it is empty space set a 0 value may be.
MCPD ASP.NET 4.0 and 3.5, MCTS WSS, MOSS, SharePoint 2010, MCT
Microsoft Community Contributor Award 2011
patricktlc
Member
2 Points
14 Posts
Re: Input string was not in a correct format.
Jun 07, 2012 10:58 AM|LINK
Unfortunately i cant convert them to either of that because i need them to be a double value.
the String.IsNullOrEmpty worked for the StationStatus part but not the MinRFL,MaxRFL,MinRFR,MaxRFR double values.
Why is this?
jobinjjp
Member
164 Points
22 Posts
Re: Input string was not in a correct format.
Jun 07, 2012 11:07 AM|LINK
You can use Extension Methods to simplify the task
Add the following class to your project..
public static class MyExtensions { public static double ToDouble(this string value) { double d = 0; double.TryParse(value, out d); return d; } public static int ToInt32(this string value) { int i = 0; int.TryParse(value, out i); return i; } }Now you should be able to do this
int StationStatus1 = GridView1.DataKeys[e.Row.RowIndex].Values[0].ToString().ToInt32(); Double MinRFL = 0.0; MinRFL = e.Row.Cells[13].Text.ToDouble();I hope this will help you... You can reuse this code in your entire project as well.....Jobin John
My Technical Jottings
urenjoy
Star
11997 Points
1797 Posts
Re: Input string was not in a correct format.
Jun 07, 2012 11:08 AM|LINK
Have you tried following:
patricktlc
Member
2 Points
14 Posts
Re: Input string was not in a correct format.
Jun 07, 2012 11:18 AM|LINK
Hi Urenjoy
I did try that yes, but it still throws me the error on
but if i do this : MinRFL = String.IsNullOrEmpty(e.Row.Cells[13].Text) ? 0 : 0;it accepts it. (but thats not the number i want.patricktlc
Member
2 Points
14 Posts
Re: Input string was not in a correct format.
Jun 07, 2012 11:21 AM|LINK
I LOVE YOU!!! THANKS SO MUCH!