1 Replies - 5153 Views - Last Post: 03 December 2013 - 01:39 PM Rate Topic: -----

#1 kin21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-November 13

c# using int.parse and string.substring

Posted 23 November 2013 - 03:44 AM

I have a check list box in webform and I want to write a code that will automatically check the remaining list when the user check the "All" option. heres the webform code ..
<asp:CheckBoxList ID="chkItem" runat="server" Height="18px" 
    RepeatColumns="3" RepeatDirection="Horizontal" Width="651px" 
    AutoPostBack="True" onselectedindexchanged="chkPosition_SelectedIndexChanged">

heres the server side code ..
            string strchkClick = Request.Form["__EVENTTARGET"];

            int index1 = int.Parse(strchkClick.Substring(strchkClick.IndexOf("$")) + 1);
            if (index1 == 0)
                bool tf = chkItem.Items[index1].Selected ? true : false;


        void CheckUncheckAll(bool tf)
            foreach (ListItem item in chkItem.Items)
                item.Selected = tf;

when I tried to run it I have this error ...

Line 19: string strchkClick = Request.Form["__EVENTTARGET"];
Line 20:
Line 21: int index1 = int.Parse(strchkClick.Substring(strchkClick.IndexOf("$")) + 1);
Line 22: if (index1 == 0)
Line 23: {

Input string was not in a correct format.

[FormatException: Input string was not in a correct format.]
System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +10689507
System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +145
System.Int32.Parse(String s) +23


I can solve this by simply not using the listcheckbox and do the old passion check box but I want to try this control, any help will be appreciated.. thanks mate... :bigsmile:/>

Is This A Good Question/Topic? 0
  • +

Replies To: c# using int.parse and string.substring

#2 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1267
  • View blog
  • Posts: 1,794
  • Joined: 24-August 11

Re: c# using int.parse and string.substring

Posted 03 December 2013 - 01:39 PM

You should be able to solve this by stepping through your code and watching the values that you are working with. My guess is that the int.Parse() call is failing because the string you're passing in is not a valid integer. Using the int.TryParse() method instead will prevent an exception from being thrown, and allow you to decide what to do if the conversion fails.

int index1;
bool result = int.TryParse(someStringValue, out index1);

if (!result)
    // the value you passed in was not a valid integer; figure out what you did wrong, or handle the bad input value (whichever applies)

if (index == 0)
    // do stuff

This would give you greater clarity into what's going wrong, but again, you'd need to step through the code to see what's going on. I can't find the link to the C# Learning series here on DIC, but here's a link to a good tutorial on debugging, and there are links to other useful tutorials in the post and in the comments: Debugging Express. The debugger is your best friend: it will make you a better programmer if you learn how to use it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1