|
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.
|
|
|
|
|
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?
|
|
|
|
|
Hi,
I want to ask what's the best way to store images in MySQL size-wise and performance-wise? is it better to store it as blob or as base64 text?
Thanks,
Jassim[^]
Technology News @ www.JassimRahma.com
|
|
|
|
|
Don't store file data in the database, store it in the file system and have the file name in the database only. Just my 2c!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
If it is relational data, then it should be in the datastore
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Converting your blob yo base64 will cost time, and the result would be somewhat bigger.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Here's[^] a research document from Microsoft on the subject.
The tl;dr is that it's size dependent.
For smaller files it's better using a database, for larger files it's better using the file system. And the breakeven is somewhere around 0.1 - 1 MB, depending on your system. Possibly larger nowadays since there has been a bit of development since the paper was written.
|
|
|
|
|
Long story short, after reviewing server statistics after a large data conversion, I noticed that parameterized queries were being stored in plan cache thousands of times. Isn't parametrizing your queries supposed to prevent that?
Example offending query:
INSERT INTO AddressTable (ObjectId, IsMailing, Street1, Street2, City, State, PostalCode, County, Country)
VALUES (@ObjectId, @IsMailing, @Street1, @Street2, @City, @State, @PostalCode, @County, @Country);
The ObjectId is a Foreign Key (INT), IsMailing is a boolean (BIT), and the rest are NVARCHAR(X).
There were 38K+ records inserted into the table (one at a time) and there are ~3400 plans of this query cached in the plan cache. The conversion procedure is a custom application written in C# that generates the insert statements on the fly depending on what is being inserted but all the ad-hoc queries are identical for a given table. On a side note, the program was executing statements against the database across 70 to 80 separate threads concurrently (yay multi-threading).
Basically, my question is why would identical parameterized queries executed thousands of times create thousands of separate query plans? And is there any settings to change to prevent that?
if (Object.DividedByZero == true) { Universe.Implode(); }
|
|
|
|
|
Let me guess, you're using AddWithValue() for your parameters?
You should NEVER do that in production code.
There will be a real parameter created from it where the settings of type and length will be taken from the object supplied.
In other words, you'll get a new plan for every new length of an @address or @street and so on.
Create proper parameters where the types and lengths are specified according to the fields in the tables.
More info here[^].
|
|
|
|
|
Not at all. Here's the watered down, abreviated version of the code that generates the INSERT statement. There's a lot more going on but this is the meat of it.
List<string> columns = new List<string>();
List<string> variables = new List<string>();
string sqlStatement = null;
using (SqlCommand command = new SqlCommand() { CommandType = CommandType.Text, Connection = connection })
{
foreach (IProperty prop in IData.Properties)
{
columns.Add(prop.TableColumnName);
variables.Add("@" + prop.TableColumnName);
var currentParameter = new Parameter("@" + prop.TableColumnName, prop.SqlType);
currentParameter.Value = (objProp.Value == null) ? DBNull.Value : objProp.Value;
command.Parameters.Add(currentParameter);
}
sqlStatement = String.Join(" ", new string[]
{
"INSERT INTO dbo." + IData.TableName,
"(" + String.Join(", ", columns.ToArray()) + ")",
"VALUES (" + String.Join(", ", variables.ToArray()) + ");"
});
command.CommandText = sqlStatement;
command.ExecuteNonQuery();
}
if (Object.DividedByZero == true) { Universe.Implode(); }
|
|
|
|
|
|
Ah, so the reason for the multitude of cached query plans is that when dealing with variable length text a new plan is created when a parameter's length is different from a previous plan? So basically, if I want the server to not create new plans, I need to define the size of the variable length parameters?
if (Object.DividedByZero == true) { Universe.Implode(); }
|
|
|
|
|
|
That makes sense. Does the Size represent the size of the data or the size of the field in the database? I would assume that's in bytes also.
if (Object.DividedByZero == true) { Universe.Implode(); }
|
|
|
|
|
|
Got it. Now all I have to do is weigh the overhead of calculating sizes (when and where) vs additional query plans in the cache. Thanks for helping to clear this up.
if (Object.DividedByZero == true) { Universe.Implode(); }
|
|
|
|
|
It would be pretty hard to calculate, since the correct size would be what the size of the field in the database table is defined as.
So there are two ways to go if you want to keep generating your DML.
Either you ask the database what the field sizes are, Or you define them in your classes using an Attribute on your properties.
You get the SizeAttribute for a member using ((SizeAttribute)Member.GetCustomAttributes(typeof(SizeAttribute), true)[0]).FieldName;
|
|
|
|
|
Not so much in this instance. For the above example, the IProperty object also includes a definition class which already has a MaxLength field, along with other database specific information, which is the size of the database field. I used that size first on text fields but noticed that some fields were getting extra empty characters appended to the end of the text to fill the size of the field, so I stopped using it. If I understand correctly, I should be passing the length of the data to the SqlParameter. The extra overhead I was referring to was casting the IData object to it's appropriate type (boxing and unboxing overhead) then getting the length from string.Length or byte[].Length. Trying to weigh which overhead has greater impact, code conversion or database query plans.
if (Object.DividedByZero == true) { Universe.Implode(); }
|
|
|
|
|
Foothill wrote: I should be passing the length of the data to the SqlParameter.
Maxlength is what you want to pass[^], that you got empty character appended is something I've never heard of before. You didn't by any chance use Char instead of Varchar? Char gets padded to the size specified.
Foothill wrote: Trying to weigh which overhead has greater impact, code conversion or database query plans.
Database query plans, without any doubts.
|
|
|
|
|
MaxLength it is and all the text fields are NVarchar(N) by default. Since I should use the MaxLength field, it would be trivial to add a couple of lines in the code that executes the INSERT statement to handle the variable length data. Thanks again.
if (Object.DividedByZero == true) { Universe.Implode(); }
|
|
|
|
|
I want to create an ACCESS database of e-learning includes two class and a number of students and 4 tests and find out any test students liked by data mining
|
|
|
|
|
OK. Do you have a question?
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
That's not data-mining; you simply need to track how often a student logs in on a test, and select the one with the most visits.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi all.am really new to asp.net,c# as well as sql.i hope you will help me on this.
i want to call my stored procedure from my class file. and later create an instance of the class in the .aspx.cs file.to do this i have to create a connection object and pass in my configuration key from web.config. that is where am stuck because i had used this:
SqlConnection connection = new SqlConnection ( ConfigurationManager.ConnectionStrings [‘’B.F.M.S"].ConnectionString);
B.F.M.S is my sql database.
Now when i run my application its giving a [NullReference Exception: Object reference not set to an instance of an object].
the error is with that line.
Can you please help on how i can rectify that.
|
|
|
|
|
You have already posted this in the ASP.NET forum. Posting the same question in multiple forums duplicates work and annoys people.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|