0

When I run my C# code, I get this error: Input string was not in a correct format. and it highlights this code:

       theTWAValue=Convert.ToInt32(theTWALabel.Text);

why is that happening?

Additional info:

The entire C# function:

    <script  runat="server">
                protected void YourListView_Load(object sender, EventArgs e)
{
    Label theTWALabel;
 int theTWAValue;
foreach (ListViewItem item in YourListView.Items)
{
        theTWALabel = (Label)item.FindControl("TWALabel");
    theTWAValue = Convert.ToInt32(theTWALabel.Text);
    if (theTWAValue >= 0)
    {
        if (theTWAValue < 90)
            theTWALabel.ForeColor = System.Drawing.Color.Yellow;
        else
            theTWALabel.ForeColor = System.Drawing.Color.Red;
    }

}   
 }

                </script>

Here is the rest of the code:

<asp:SqlDataSource id="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:2007 SoundAssist VER 1.0.5  05-12-2011 ( 2013-06-24)ConnectionString %>" ProviderName="<%$ ConnectionStrings:2007  SoundAssist VER 1.0.5  05-12-2011 ( 2013-06-24)ConnectionString.ProviderName %>" SelectCommand="SELECT [Plant] FROM [PLANT]">
  </asp:SqlDataSource>
  <asp:DropDownList id="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="Plant" DataValueField="Plant" Height="85px" Width="393px">
  </asp:DropDownList>
  <asp:SqlDataSource id="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:2007  SoundAssist VER 1.0.5  05-12-2011 ( 2013-06-24)ConnectionString %>" ProviderName="<%$ ConnectionStrings:2007  SoundAssist VER 1.0.5  05-12-2011 ( 2013-06-24)ConnectionString.ProviderName %>" SelectCommand="SELECT [Plant], [Group No#] AS column1, [Group] FROM [Temp Table that contains TWA values] WHERE ([Plant] = ?)">
      <SelectParameters>
          <asp:ControlParameter ControlID="DropDownList1" Name="Plant" PropertyName="SelectedValue" Type="String" />
      </SelectParameters>
  </asp:SqlDataSource>
  <asp:DropDownList id="DropDownList2" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource2" DataTextField="Group" DataValueField="column1" Height="30px" Width="394px">
  </asp:DropDownList>
  <asp:SqlDataSource id="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:2007  SoundAssist VER 1.0.5  05-12-2011 ( 2013-06-24)ConnectionString %>" ProviderName="<%$ ConnectionStrings:2007  SoundAssist VER 1.0.5  05-12-2011 (2013-06-24)ConnectionString.ProviderName %>" SelectCommand="SELECT [Plant], [Group No#] AS column1, [Group], [Job Code] AS Job_Code, [Job Function] AS Job_Function, [Job Classification] AS Job_Classification FROM [Temp Table that contains TWA values] WHERE (([Plant] = ?) AND ([Group No#] = ?))">
      <SelectParameters>
          <asp:ControlParameter ControlID="DropDownList1" Name="Plant" PropertyName="SelectedValue" Type="String" />
          <asp:ControlParameter ControlID="DropDownList2" Name="column1" PropertyName="SelectedValue" Type="String" />
      </SelectParameters>
  </asp:SqlDataSource>
  <asp:DropDownList id="DropDownList3" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource3" DataTextField="Job_Classification" DataValueField="Job_Classification" Height="17px" Width="384px">
  </asp:DropDownList>
  <asp:ListView id="YourListView" OnLoad="YourListView_Load" runat="server"  DataSourceID="SqlDataSource4">
      <ItemTemplate>
          <span style="">Plant:
          <asp:Label id="PlantLabel" runat="server" Text='<%# Eval("Plant") %>' />
          <br />
          column1:
          <asp:Label id="column1Label" runat="server" Text='<%# Eval("column1") %>' />
          <br />
          Group:
          <asp:Label id="GroupLabel" runat="server" Text='<%# Eval("Group") %>' />
          <br />
          Job_Code:
          <asp:Label id="Job_CodeLabel" runat="server" Text='<%# Eval("Job_Code") %>' />
          <br />
          Job_Classification:
          <asp:Label id="Job_ClassificationLabel" runat="server" Text='<%# Eval("Job_Classification") %>' />
          <br />
          Job_Function:
          <asp:Label id="Job_FunctionLabel" runat="server" Text='<%# Eval("Job_Function") %>' />
          <br />
          Job_Description:
          <asp:Label id="Job_DescriptionLabel" runat="server" Text='<%# Eval("Job_Description") %>' />
          <br />
          TWA:
          <asp:Label id="TWALabel" runat="server" Text='<%# Eval("TWA") %>' />
          <br />
          <br />
          </span>
      </ItemTemplate>

      </asp:ListView>
  <asp:SqlDataSource id="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:2007  SoundAssist VER 1.0.5  05-12-2011 ( 2013-06-24)ConnectionString %>" ProviderName="<%$ ConnectionStrings:2007  SoundAssist VER 1.0.5  05-12-2011 ( 2013-06-24)ConnectionString.ProviderName %>" SelectCommand="SELECT [Plant], [Group No#] AS column1, [Group], [Job Code] AS Job_Code, [Job Classification] AS Job_Classification, [Job Function] AS Job_Function, [Job Description] AS Job_Description, [TWA] FROM [Temp Table that contains TWA values] WHERE (([Plant] = ?) AND ([Group No#] = ?) AND ([Job Classification] = ?))">
      <SelectParameters>
          <asp:ControlParameter ControlID="DropDownList1" Name="Plant" PropertyName="SelectedValue" Type="String" />
          <asp:ControlParameter ControlID="DropDownList2" Name="column1" PropertyName="SelectedValue" Type="String" />
          <asp:ControlParameter ControlID="DropDownList3" Name="Job_Classification" PropertyName="SelectedValue" Type="String" />
      </SelectParameters>
  </asp:SqlDataSource>
1
  • 2
    what is the value of theTWALabel.Text when this fails? also, you should consider Int32.TryParse because you are reading from a textbox (which is not constrained otherwise?) Commented Jul 17, 2013 at 17:29

5 Answers 5

3

if your theTWALabel.Text value is empty then you would get this error. Check that there is a value inside before trying to convert.

Sign up to request clarification or add additional context in comments.

2 Comments

the list view is not going to be displayed until the user selects the items in each drop down list, so is that my issue?
I guess u got ur answer from discussing with McGarnagle, am I right?
2

You have to actually check that the input is valid -- I would use TryParse instead of the Convert function:

if (int.TryParse(theTWALabel.Text, out theTWAValue))
{
}

Update post-edit

I could be wrong, but I don't think the values of Label controls get posted back to the server -- this would explain why they're showing up as empty. In any case, setting colors from the "Loaded" event seems unnecessarily complicated. Why not create a helper method and bind it directly?

protected Color GetColorForLabel(string text)
{
    int theTWAValue;
    if (text != null && int.TryParse(text, out theTWAValue) && theTWAValue >= 0)
    {
        return (theTWAValue < 90) ? System.Drawing.Color.Yellow : System.Drawing.Color.Red;
    }
    return System.Drawing.Color.Green;
}

Then use it to bind the Foreground property:

<asp:Label id="TWALabel" runat="server" 
    Text = '<%# Eval("TWA") %>' 
    Foreground = '<%# GetColorForLabel( Eval("TWA") as string ) %>' />

9 Comments

Well it runs now but doesn't change the colors. Is it because I have the user select the parameters of the listview before it output? And by that I mean, I have them select the plant name, then the department, then the job classification, then it shows the list view.
@friendo9876 Yeah, seems to imply that the value isn't getting posted back to the server for some reason. Not really enough info to say for sure.
What additional information do you need that I can add to this page to make answer more precise?
@friendo9876 the relevant parts of the markup would be helpful.
when I run it i get this error: CS0246: The type or namespace name 'Color' could not be found (are you missing a using directive or an assembly reference?) and highlights this text : protected Color GetColorForLabel(string text)
|
1

You can add Trim function

 theTWAValue=Convert.ToInt32(theTWALabel.Text.Trim());

msdn link : http://msdn.microsoft.com/fr-fr/library/vstudio/t97s7bs3.aspx

I suggest you tu use Int32.TryParse function

link : http://msdn.microsoft.com/fr-fr/library/vstudio/f02979c7.aspx

4 Comments

What exactly does the Trim Function do?
Suppress blank, because "33" is not "33 " (with space)
trim will remove the white spaces in start and end of the string but I dont think that is your problem
it's possible solution Rab Nawaz, if you don't have surface validation
1

try this

theTWAValue = Convert.ToInt32(theTWALabel.Text.ToString().Trim());

Comments

1

Use int.TryParse instead.

int value;
if(!int.TryParse(theTWALabel.Text, out value))
{
  //didn't parse right
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.