textbox help
-
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?
-
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?
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. =========================================================
-
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?
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
ands
. 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 likeSales 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
-
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. =========================================================
sure you are right.thanx
-
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
ands
. 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 likeSales 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
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
-
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?
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; usexxx.TryParse
. As to theNullReferenceException
, you need to show exactly where it occurs in your code. -
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
My bad :/
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
-
My bad :/
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
No worries, you're talking to an expert in misreading specs.
Wrong is evil and must be defeated. - Jeff Ello
-
No worries, you're talking to an expert in misreading specs.
Wrong is evil and must be defeated. - Jeff Ello
:D
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson