cast String to In16

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • archuleta37

    cast String to In16

    I'm trying to cast a String from a web forms textbox into an Int16 (see code
    below), but it's not quite working. When I post the textbox with numbers (no
    alpha chars) I get the following error in my web page:
    Exception Details: System.FormatEx ception: Input string was not in a correct
    format.


    String qty;
    foreach (GridViewRow row in gridView1.Rows)
    {
    if (!String.IsNull OrEmpty(qty) || qty != "0")
    {
    qty = ((TextBox)row.F indControl("txt Quantity")).Tex t;
    order.quantity = Int16.Parse(qty ); // debugger stops on this line
    // order.quantity is defined as an Int16
    }
    }

    I don't understand why I'm getting a FormatException since I'm not using any
    formatting. Is there another approach I should be taking to cast my textbox
    value?

  • Larry Lard

    #2
    Re: cast String to In16

    archuleta37 wrote:
    I'm trying to cast a String from a web forms textbox into an Int16 (see code
    below), but it's not quite working. When I post the textbox with numbers (no
    alpha chars) I get the following error in my web page:
    Exception Details: System.FormatEx ception: Input string was not in a correct
    format.
    >
    >
    String qty;
    foreach (GridViewRow row in gridView1.Rows)
    {
    if (!String.IsNull OrEmpty(qty) || qty != "0")
    {
    qty = ((TextBox)row.F indControl("txt Quantity")).Tex t;
    order.quantity = Int16.Parse(qty ); // debugger stops on this line
    // order.quantity is defined as an Int16
    }
    }
    >
    I don't understand why I'm getting a FormatException since I'm not using any
    formatting. Is there another approach I should be taking to cast my textbox
    value?
    >
    What is the value of qty when you get the error?

    --
    Larry Lard
    larrylard@googl email.com
    The address is real, but unread - please reply to the group
    For VB and C# questions - tell us which version

    Comment

    • Marc Gravell

      #3
      Re: cast String to In16

      Well, what "numbers" are you giving it? What *exactly* is qty when it fails?
      Also - if 2.0 you may wish to look at TryParse, but that wouldn't help - it
      would just fail in a different way.

      Also - is your code "as is"? You don't assign to qty before the first test,
      so I assume it is paraphrased (since it won't compile)... if so, I wonder if
      perhaps the bug went with the snip?

      Marc


      Comment

      • Stoitcho Goutsev \(100\)

        #4
        Re: cast String to In16

        It looks like thes string that you pass to the Int16.Parse does not
        represent valid integer number. Have you tried to inpect the actual value of
        the qty string before you pass it ti the Parse method?



        --
        Stoitcho Goutsev (100)

        "archuleta3 7" <archuleta37@di scussions.micro soft.comwrote in message
        news:78EE792F-FD77-46D9-9D4F-A9C38B0C3B14@mi crosoft.com...
        I'm trying to cast a String from a web forms textbox into an Int16 (see
        code
        below), but it's not quite working. When I post the textbox with numbers
        (no
        alpha chars) I get the following error in my web page:
        Exception Details: System.FormatEx ception: Input string was not in a
        correct
        format.
        >
        >
        String qty;
        foreach (GridViewRow row in gridView1.Rows)
        {
        if (!String.IsNull OrEmpty(qty) || qty != "0")
        {
        qty = ((TextBox)row.F indControl("txt Quantity")).Tex t;
        order.quantity = Int16.Parse(qty ); // debugger stops on this line
        // order.quantity is defined as an Int16
        }
        }
        >
        I don't understand why I'm getting a FormatException since I'm not using
        any
        formatting. Is there another approach I should be taking to cast my
        textbox
        value?
        >

        Comment

        • archuleta37

          #5
          Re: cast String to In16

          I get this error if I type the number "1" in the first textbox (first row) in
          my gridview and submit it. If I comment out my code where I get the error and
          then take the value and pass it into another text box or label for debugging
          purposes it passes the correct string.

          "Larry Lard" wrote:
          archuleta37 wrote:
          I'm trying to cast a String from a web forms textbox into an Int16 (see code
          below), but it's not quite working. When I post the textbox with numbers (no
          alpha chars) I get the following error in my web page:
          Exception Details: System.FormatEx ception: Input string was not in a correct
          format.


          String qty;
          foreach (GridViewRow row in gridView1.Rows)
          {
          if (!String.IsNull OrEmpty(qty) || qty != "0")
          {
          qty = ((TextBox)row.F indControl("txt Quantity")).Tex t;
          order.quantity = Int16.Parse(qty ); // debugger stops on this line
          // order.quantity is defined as an Int16
          }
          }

          I don't understand why I'm getting a FormatException since I'm not using any
          formatting. Is there another approach I should be taking to cast my textbox
          value?
          >
          What is the value of qty when you get the error?
          >
          --
          Larry Lard
          larrylard@googl email.com
          The address is real, but unread - please reply to the group
          For VB and C# questions - tell us which version
          >

          Comment

          • archuleta37

            #6
            Re: cast String to In16

            Marc,
            Yes, I had posted a simplified version. I've pasted a more complete version
            below.
            In my tracing code, If I do this:
            TextBox1.Text += order.item_id + " - " + qty + " - ";
            and comment out the line where I'm getting the error, then I get the correct
            number showing up. I'm not yet trying to put anything but a number into my
            quantity textbox in my gridview.

            I hope this helps clarify. Let me know if you have anymore questions.

            protected void Page_Load(objec t sender, EventArgs e)
            {
            if (IsPostBack)
            {
            // create variables
            String qty;
            String price;
            String cmdText;
            ArrayList NewOrders = new ArrayList();
            OrderModel order = new OrderModel();
            Regex rx = new Regex(@"^\$"); // starts with "$"
            SqlConnection conn = new
            SqlConnection(C onfigurationMan ager.AppSetting s["myConnectionSt ring"]);

            // loop through the rows
            foreach (GridViewRow row in gvMultiOrder.Ro ws)
            {
            qty = ((TextBox)row.F indControl("txt Quantity")).Tex t;
            // look for a quantity
            if (!String.IsNull OrEmpty(qty) || qty != "0")
            {
            price = gvMultiOrder.Ro ws[row.RowIndex].Cells[1].Text;
            price = rx.Replace(pric e, ""); // remove the dollar sign
            // populate order object
            order.item_id =
            (Int32)gvMultiO rder.DataKeys[row.RowIndex].Value;
            order.internal_ price = Decimal.Parse(p rice);
            order.quantity = Int16.Parse(qty ); // error happens here
            order.departmen t_id = 5; // TODO: set this dynamically
            order.requested _by = "me"; // TODO: set this dynamically
            NewOrders.Add(o rder);

            // this is for tracing purposes
            //TextBox1.Text += order.item_id + " - " +
            order.quantity + " - ";
            //TextBox1.Text += order.internal_ price + " | ";
            }
            }
            // add the data to the database here ... bla bla bla
            }
            }

            "Marc Gravell" wrote:
            Well, what "numbers" are you giving it? What *exactly* is qty when it fails?
            Also - if 2.0 you may wish to look at TryParse, but that wouldn't help - it
            would just fail in a different way.
            >
            Also - is your code "as is"? You don't assign to qty before the first test,
            so I assume it is paraphrased (since it won't compile)... if so, I wonder if
            perhaps the bug went with the snip?
            >
            Marc
            >
            >
            >

            Comment

            • archuleta37

              #7
              Re: cast String to In16

              Yes, the qty variable is getting populated. See my response to Marc Gravell.

              "Stoitcho Goutsev (100)" wrote:
              It looks like thes string that you pass to the Int16.Parse does not
              represent valid integer number. Have you tried to inpect the actual value of
              the qty string before you pass it ti the Parse method?
              >
              >
              >
              --
              Stoitcho Goutsev (100)
              >
              "archuleta3 7" <archuleta37@di scussions.micro soft.comwrote in message
              news:78EE792F-FD77-46D9-9D4F-A9C38B0C3B14@mi crosoft.com...
              I'm trying to cast a String from a web forms textbox into an Int16 (see
              code
              below), but it's not quite working. When I post the textbox with numbers
              (no
              alpha chars) I get the following error in my web page:
              Exception Details: System.FormatEx ception: Input string was not in a
              correct
              format.


              String qty;
              foreach (GridViewRow row in gridView1.Rows)
              {
              if (!String.IsNull OrEmpty(qty) || qty != "0")
              {
              qty = ((TextBox)row.F indControl("txt Quantity")).Tex t;
              order.quantity = Int16.Parse(qty ); // debugger stops on this line
              // order.quantity is defined as an Int16
              }
              }

              I don't understand why I'm getting a FormatException since I'm not using
              any
              formatting. Is there another approach I should be taking to cast my
              textbox
              value?
              >
              >
              >

              Comment

              • Claes Bergefall

                #8
                Re: cast String to In16

                That code looks a bit incorrect. Your if-statement tests qty before you even
                assign it to something (well, actually it tests the value from the previous
                iteration of the loop, which is probably not what you want). Furthermore,
                your if-statement will be true for an empty string, which is also probably
                not what you want.

                As for the error you're getting, it's because qty is not in a format that
                can be converted to an int16. You didn't post what the string is though so
                it's rather hard to give any more info than that (my guess is that it's
                empty).

                /claes

                "archuleta3 7" <archuleta37@di scussions.micro soft.comwrote in message
                news:78EE792F-FD77-46D9-9D4F-A9C38B0C3B14@mi crosoft.com...
                I'm trying to cast a String from a web forms textbox into an Int16 (see
                code
                below), but it's not quite working. When I post the textbox with numbers
                (no
                alpha chars) I get the following error in my web page:
                Exception Details: System.FormatEx ception: Input string was not in a
                correct
                format.
                >
                >
                String qty;
                foreach (GridViewRow row in gridView1.Rows)
                {
                if (!String.IsNull OrEmpty(qty) || qty != "0")
                {
                qty = ((TextBox)row.F indControl("txt Quantity")).Tex t;
                order.quantity = Int16.Parse(qty ); // debugger stops on this line
                // order.quantity is defined as an Int16
                }
                }
                >
                I don't understand why I'm getting a FormatException since I'm not using
                any
                formatting. Is there another approach I should be taking to cast my
                textbox
                value?
                >

                Comment

                • WhiteWizard

                  #9
                  RE: cast String to In16

                  I know what I am about to ask you to check will sound weird, but trust me.
                  We went through about 2 1/2 WEEKS of chasing down a VERY similar problem.

                  First try another conversion (i.e. try converting to an int). If you still
                  get the error may be the same one we got. Believe it or not, on only 3 of
                  our 10 development machines, the PositiveSign for the en-US culture had
                  gotten corrupted and changed to a 0.

                  Here is the code for a simple Console application that lists all the
                  PositiveSigns for all of the cultures(in case you aren't using en-US).

                  If that is the problem, let me know. In the meantime I'll try and find the
                  Registry setting you need to reset to fix.

                  HTH
                  WhiteWizard
                  aka Gandalf
                  MCSD.NET, MCAD, MCT


                  "archuleta3 7" wrote:
                  I'm trying to cast a String from a web forms textbox into an Int16 (see code
                  below), but it's not quite working. When I post the textbox with numbers (no
                  alpha chars) I get the following error in my web page:
                  Exception Details: System.FormatEx ception: Input string was not in a correct
                  format.
                  >
                  >
                  String qty;
                  foreach (GridViewRow row in gridView1.Rows)
                  {
                  if (!String.IsNull OrEmpty(qty) || qty != "0")
                  {
                  qty = ((TextBox)row.F indControl("txt Quantity")).Tex t;
                  order.quantity = Int16.Parse(qty ); // debugger stops on this line
                  // order.quantity is defined as an Int16
                  }
                  }
                  >
                  I don't understand why I'm getting a FormatException since I'm not using any
                  formatting. Is there another approach I should be taking to cast my textbox
                  value?
                  >

                  Comment

                  • WhiteWizard

                    #10
                    RE: cast String to In16

                    Sorry, I just realized the code didn't get copied. Here you go.

                    using System;
                    using System.Globaliz ation;
                    using System.Text;

                    public sealed class App
                    {
                    static void Main()
                    {
                    Console.WriteLi ne("Current Culture: " +
                    CultureInfo.Cur rentCulture.Nam e);

                    foreach (CultureInfo cinfo in CultureInfo.Get Cultures
                    CultureTypes.Al lCultures))
                    {
                    try
                    {
                    Console.WriteLi ne("Name: " + cinfo.Name + " Positive sign: "
                    + cinfo.NumberFor mat.PositiveSig n);
                    }
                    catch (NotSupportedEx ception e)
                    {
                    }
                    }
                    Console.ReadLin e();
                    }
                    }

                    --
                    MCSD.NET, MCAD, MCT


                    "archuleta3 7" wrote:
                    I'm trying to cast a String from a web forms textbox into an Int16 (see code
                    below), but it's not quite working. When I post the textbox with numbers (no
                    alpha chars) I get the following error in my web page:
                    Exception Details: System.FormatEx ception: Input string was not in a correct
                    format.
                    >
                    >
                    String qty;
                    foreach (GridViewRow row in gridView1.Rows)
                    {
                    if (!String.IsNull OrEmpty(qty) || qty != "0")
                    {
                    qty = ((TextBox)row.F indControl("txt Quantity")).Tex t;
                    order.quantity = Int16.Parse(qty ); // debugger stops on this line
                    // order.quantity is defined as an Int16
                    }
                    }
                    >
                    I don't understand why I'm getting a FormatException since I'm not using any
                    formatting. Is there another approach I should be taking to cast my textbox
                    value?
                    >

                    Comment

                    • archuleta37

                      #11
                      Re: cast String to In16

                      lClaes,
                      Thanks for pointing out the error in my logic in the if statement. I changed
                      the "||" to "&&" and it worked. In terms of the other errors you noted, I had
                      actually shortened my code to the relevant parts and misplaced the line where
                      I set the qty value. In fact I had orginally placed it before the if
                      statement (see my response to Marc Gravell).

                      Thanks again for your good eye. :-)


                      "Claes Bergefall" wrote:
                      That code looks a bit incorrect. Your if-statement tests qty before you even
                      assign it to something (well, actually it tests the value from the previous
                      iteration of the loop, which is probably not what you want). Furthermore,
                      your if-statement will be true for an empty string, which is also probably
                      not what you want.
                      >
                      As for the error you're getting, it's because qty is not in a format that
                      can be converted to an int16. You didn't post what the string is though so
                      it's rather hard to give any more info than that (my guess is that it's
                      empty).
                      >
                      /claes
                      >
                      "archuleta3 7" <archuleta37@di scussions.micro soft.comwrote in message
                      news:78EE792F-FD77-46D9-9D4F-A9C38B0C3B14@mi crosoft.com...
                      I'm trying to cast a String from a web forms textbox into an Int16 (see
                      code
                      below), but it's not quite working. When I post the textbox with numbers
                      (no
                      alpha chars) I get the following error in my web page:
                      Exception Details: System.FormatEx ception: Input string was not in a
                      correct
                      format.


                      String qty;
                      foreach (GridViewRow row in gridView1.Rows)
                      {
                      if (!String.IsNull OrEmpty(qty) || qty != "0")
                      {
                      qty = ((TextBox)row.F indControl("txt Quantity")).Tex t;
                      order.quantity = Int16.Parse(qty ); // debugger stops on this line
                      // order.quantity is defined as an Int16
                      }
                      }

                      I don't understand why I'm getting a FormatException since I'm not using
                      any
                      formatting. Is there another approach I should be taking to cast my
                      textbox
                      value?
                      >
                      >
                      >

                      Comment

                      • archuleta37

                        #12
                        RE: cast String to In16

                        WhiteWizard,
                        Thanks for sharing the bug you've found in the past. This could be valuable
                        to keep in mind should in crop up in the future. Also, thanks for the console
                        app, this could come in handy from time to time. As a note to anyone who uses
                        it, I noticed that there was a missing opening perenthesis in the foreach
                        statement - just add it back in and it works great.

                        Luckily I found I don't have that bug. It turns out that I was using "||" in
                        my if statement instead of "&&". Incidentally, using your console app, I
                        found that my culture info is indeed en-US.

                        "WhiteWizar d" wrote:
                        Sorry, I just realized the code didn't get copied. Here you go.
                        >
                        using System;
                        using System.Globaliz ation;
                        using System.Text;
                        >
                        public sealed class App
                        {
                        static void Main()
                        {
                        Console.WriteLi ne("Current Culture: " +
                        CultureInfo.Cur rentCulture.Nam e);
                        >
                        foreach (CultureInfo cinfo in CultureInfo.Get Cultures
                        CultureTypes.Al lCultures))
                        {
                        try
                        {
                        Console.WriteLi ne("Name: " + cinfo.Name + " Positive sign: "
                        + cinfo.NumberFor mat.PositiveSig n);
                        }
                        catch (NotSupportedEx ception e)
                        {
                        }
                        }
                        Console.ReadLin e();
                        }
                        }
                        >
                        --
                        MCSD.NET, MCAD, MCT
                        >
                        >
                        "archuleta3 7" wrote:
                        >
                        I'm trying to cast a String from a web forms textbox into an Int16 (see code
                        below), but it's not quite working. When I post the textbox with numbers (no
                        alpha chars) I get the following error in my web page:
                        Exception Details: System.FormatEx ception: Input string was not in a correct
                        format.


                        String qty;
                        foreach (GridViewRow row in gridView1.Rows)
                        {
                        if (!String.IsNull OrEmpty(qty) || qty != "0")
                        {
                        qty = ((TextBox)row.F indControl("txt Quantity")).Tex t;
                        order.quantity = Int16.Parse(qty ); // debugger stops on this line
                        // order.quantity is defined as an Int16
                        }
                        }

                        I don't understand why I'm getting a FormatException since I'm not using any
                        formatting. Is there another approach I should be taking to cast my textbox
                        value?

                        Comment

                        • Jon Skeet [C# MVP]

                          #13
                          Re: cast String to In16

                          archuleta37 <archuleta37@di scussions.micro soft.comwrote:
                          Marc,
                          Yes, I had posted a simplified version. I've pasted a more complete version
                          below.
                          In my tracing code, If I do this:
                          TextBox1.Text += order.item_id + " - " + qty + " - ";
                          and comment out the line where I'm getting the error, then I get the correct
                          number showing up. I'm not yet trying to put anything but a number into my
                          quantity textbox in my gridview.
                          Do you have any rows with empty strings? This line isn't doing what you
                          want it to:

                          if (!String.IsNull OrEmpty(qty) || qty != "0")

                          There is no string which *won't* satisfy that condition - if it's
                          anything other than "0", the right hand side will be true, and if it's
                          "0" then the left hand side will be true.

                          My guess is that you're parsing an empty string which is causing the
                          problem.

                          --
                          Jon Skeet - <skeet@pobox.co m>
                          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                          If replying to the group, please do not mail me too

                          Comment

                          • Marc Gravell

                            #14
                            Re: cast String to In16

                            Good eyes, Jon... perhaps the bug wasn't snipped after all ;-p

                            Marc

                            Comment

                            Working...