Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. textbox help

textbox help

Scheduled Pinned Locked Moved Database
helpcsharpquestionasp-netdatabase
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 12287337
    wrote on last edited by
    #1

    how are you all. i am new to asp.net and c#. i need help. well my question is, i have a textbox in which a user have to enter a float value in the webform. and i want to manipulate that value in a calculation. so have created a class for the calculation which is Sales class. in that class i have declared Price as a float value,the value which should be obtained from the textbox.ie public float Price {get;set;} Now my problem is with binding/linking the Price variable with the textbox in the button_click method. initially i had done it this way: in my .aspx.cs file i have created an object of the class in this case : Sales s = new Sales (); protected void button_Click (object sender EventArgs e) { s.Price =Convert.ToInt32(Textbox1.Text); s.Save(); } i want the Price value to be saved in my database after a user clicks the SAVE button from the webform. Now the problem is when i run the application there is an error (

    An exception of type 'System.NullReferenceException' occurred in Version1.dll but was not handled in user code

    ) on the line : s.Price = Convert.ToInt32(Textbox1.Text); could i have done something wrong in the code?

    C S L 3 Replies Last reply
    0
    • M Member 12287337

      how are you all. i am new to asp.net and c#. i need help. well my question is, i have a textbox in which a user have to enter a float value in the webform. and i want to manipulate that value in a calculation. so have created a class for the calculation which is Sales class. in that class i have declared Price as a float value,the value which should be obtained from the textbox.ie public float Price {get;set;} Now my problem is with binding/linking the Price variable with the textbox in the button_click method. initially i had done it this way: in my .aspx.cs file i have created an object of the class in this case : Sales s = new Sales (); protected void button_Click (object sender EventArgs e) { s.Price =Convert.ToInt32(Textbox1.Text); s.Save(); } i want the Price value to be saved in my database after a user clicks the SAVE button from the webform. Now the problem is when i run the application there is an error (

      An exception of type 'System.NullReferenceException' occurred in Version1.dll but was not handled in user code

      ) on the line : s.Price = Convert.ToInt32(Textbox1.Text); could i have done something wrong in the code?

      C Offline
      C Offline
      Chris Quinn
      wrote on last edited by
      #2

      Member 12287337 wrote:

      could i have done something wrong in the code?

      Possibly, but you have definitely done something wrong by pasting this in the Database forum - It probably belongs in the C# forum[^]

      ========================================================= I'm an optoholic - my glass is always half full of vodka. =========================================================

      M 1 Reply Last reply
      0
      • M Member 12287337

        how are you all. i am new to asp.net and c#. i need help. well my question is, i have a textbox in which a user have to enter a float value in the webform. and i want to manipulate that value in a calculation. so have created a class for the calculation which is Sales class. in that class i have declared Price as a float value,the value which should be obtained from the textbox.ie public float Price {get;set;} Now my problem is with binding/linking the Price variable with the textbox in the button_click method. initially i had done it this way: in my .aspx.cs file i have created an object of the class in this case : Sales s = new Sales (); protected void button_Click (object sender EventArgs e) { s.Price =Convert.ToInt32(Textbox1.Text); s.Save(); } i want the Price value to be saved in my database after a user clicks the SAVE button from the webform. Now the problem is when i run the application there is an error (

        An exception of type 'System.NullReferenceException' occurred in Version1.dll but was not handled in user code

        ) on the line : s.Price = Convert.ToInt32(Textbox1.Text); could i have done something wrong in the code?

        S Offline
        S Offline
        Sascha Lefevre
        wrote on last edited by
        #3

        When you get a NullReferenceException you have to look at the line where it occurs and ask yourself: What could be null here that shouldn't be null? There are two variables involved: TextBox1 and s. TextBox1 could be null but you probably have let it "set up" for you by the designer so it's unlikely. That leaves s. Have you initialized s with an instance of its class Sales, something like Sales s = new Sales(); ? Edit: Yes, you have. Sorry, I completely missed it. So then run your App in debug mode and when you hit that line where the exception occurs inspect all variables on that line to find out which one is null.

        If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

        J 1 Reply Last reply
        0
        • C Chris Quinn

          Member 12287337 wrote:

          could i have done something wrong in the code?

          Possibly, but you have definitely done something wrong by pasting this in the Database forum - It probably belongs in the C# forum[^]

          ========================================================= I'm an optoholic - my glass is always half full of vodka. =========================================================

          M Offline
          M Offline
          Member 12287337
          wrote on last edited by
          #4

          sure you are right.thanx

          1 Reply Last reply
          0
          • S Sascha Lefevre

            When you get a NullReferenceException you have to look at the line where it occurs and ask yourself: What could be null here that shouldn't be null? There are two variables involved: TextBox1 and s. TextBox1 could be null but you probably have let it "set up" for you by the designer so it's unlikely. That leaves s. Have you initialized s with an instance of its class Sales, something like Sales s = new Sales(); ? Edit: Yes, you have. Sorry, I completely missed it. So then run your App in debug mode and when you hit that line where the exception occurs inspect all variables on that line to find out which one is null.

            If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

            J Offline
            J Offline
            Jorgen Andersson
            wrote on last edited by
            #5

            Sascha Lefèvre wrote:

            Have you initialized s with an instance of its class Sales, something like Sales s = new Sales(); ?

            He states so in the OP.

            Member 12287337 wrote:

            in my .aspx.cs file i have created an object of the class in this case : Sales s = new Sales ();

            Wrong is evil and must be defeated. - Jeff Ello

            S 1 Reply Last reply
            0
            • M Member 12287337

              how are you all. i am new to asp.net and c#. i need help. well my question is, i have a textbox in which a user have to enter a float value in the webform. and i want to manipulate that value in a calculation. so have created a class for the calculation which is Sales class. in that class i have declared Price as a float value,the value which should be obtained from the textbox.ie public float Price {get;set;} Now my problem is with binding/linking the Price variable with the textbox in the button_click method. initially i had done it this way: in my .aspx.cs file i have created an object of the class in this case : Sales s = new Sales (); protected void button_Click (object sender EventArgs e) { s.Price =Convert.ToInt32(Textbox1.Text); s.Save(); } i want the Price value to be saved in my database after a user clicks the SAVE button from the webform. Now the problem is when i run the application there is an error (

              An exception of type 'System.NullReferenceException' occurred in Version1.dll but was not handled in user code

              ) on the line : s.Price = Convert.ToInt32(Textbox1.Text); could i have done something wrong in the code?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Member 12287337 wrote:

              could i have done something wrong in the code?

              Yes quite a few in fact. Firstly you should not use float values in financial calculations, use Int or Decimal types. Secondly do not try to convert a float/decimal value into an integer, it will fail. Thirdly, do not use Convert.Toxxx as you have no idea what the user has typed into the textbox; use xxx.TryParse. As to the NullReferenceException, you need to show exactly where it occurs in your code.

              1 Reply Last reply
              0
              • J Jorgen Andersson

                Sascha Lefèvre wrote:

                Have you initialized s with an instance of its class Sales, something like Sales s = new Sales(); ?

                He states so in the OP.

                Member 12287337 wrote:

                in my .aspx.cs file i have created an object of the class in this case : Sales s = new Sales ();

                Wrong is evil and must be defeated. - Jeff Ello

                S Offline
                S Offline
                Sascha Lefevre
                wrote on last edited by
                #7

                My bad :/

                If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                J 1 Reply Last reply
                0
                • S Sascha Lefevre

                  My bad :/

                  If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                  J Offline
                  J Offline
                  Jorgen Andersson
                  wrote on last edited by
                  #8

                  No worries, you're talking to an expert in misreading specs.

                  Wrong is evil and must be defeated. - Jeff Ello

                  S 1 Reply Last reply
                  0
                  • J Jorgen Andersson

                    No worries, you're talking to an expert in misreading specs.

                    Wrong is evil and must be defeated. - Jeff Ello

                    S Offline
                    S Offline
                    Sascha Lefevre
                    wrote on last edited by
                    #9

                    :D

                    If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups