Click here to Skip to main content
15,914,013 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello :)

I would like to know on how to display to my textbox only 2 decimal places after loading the data from the database. Can anyone help me? here is my code on retrieving the data and put it inside the textbox :)



SqlCommand cmd = new SqlCommand("SELECT * from tblMiscellaneousFee WHERE GradeLevel = '" + grd_lvl.Text + "'", MySqlConnection);
               SqlDataReader rred = cmd.ExecuteReader();
               while (rred.Read())
               {
                   bookfee.Text = rred[2].ToString();
                   PEFee.Text = rred[3].ToString();
                   SchuniformFee.Text = rred[4].ToString();
                   MedicFee.Text = rred[5].ToString();
                   MiscFee.Text = rred[6].ToString();
                   tuitionFee.Text = rred[7].ToString();
                   totMisc.Text = MiscFee.Text;


               }
               rred.Close();
Posted
Updated 17-Jan-15 22:45pm
v2
Comments
Kornfeld Eliyahu Peter 18-Jan-15 5:00am    
Check this: http://msdn.microsoft.com/en-us/library/fzeeb5cd(v=vs.110).aspx
DarkDreamer08 18-Jan-15 5:05am    
sorry to say, i do not understand what the article is implying for.i am using a windows form application and not web forms. Sorry again :)
Kornfeld Eliyahu Peter 18-Jan-15 5:36am    
And how is my link connected to web?
DarkDreamer08 18-Jan-15 5:43am    
I am sorry. It is a windows form application codes. I am sorry to misunderstand your comment :) peace :)

1 solution

Assuming that it's your book fee that is the numeric value (if it isn't, then use the right values yourself).

First, convert it to an appropriate numeric type (appropriate in the case matching your DB column data type - so if it's FLOAT, use float or double; if it's DECIMAL use decimal. If it's NVARCHAR, change your database so it isn't - you should always store values in the most appropriate data type or it will give huge problems later on.

Then, format the data to a string.
C#
bookfee.Text = string.Format("{0:0.00}", (double) rred[2]);


BTW: You are doing things which combine to make one nasty source of bugs: you aren;t specifying the columns you want to retrieve (instead you get all of them), and you use numeric offsets to specify which column you use.
If you must use SELECT * FROM ... - and it's very inefficent, particularly if you aren't using all values, as you aren't - then you really have to use column names rather than numeric offsets. Otherwise you are relying on a column order that can be changed outside of your program - at which point things start to go very subtly and badly wrong: you can really muck up your database if you aren't very, very careful. List the columns you want, and only those, and use column names to access them from your reader.
Ans most seriously of all: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. The code you have there allows any user to delete your database just by typing in a text box...
 
Share this answer
 
Comments
DarkDreamer08 18-Jan-15 5:21am    
Thanks for your answer and explanations as well. But do I need to change the data type for my fields if they are declared as smallmoney? such Book fee. I used that data type since they accept currency :)
OriginalGriff 18-Jan-15 5:43am    
Yes - in the C# code. The Database itself will store them just fine.
But the DataReader always returns an Object - so if you want to use it as a specific type of value then you need to convert it to a "proper" C# datatype first - that way all future code will be restricted to just the operations you can perform on the data. Such as formatting it correctly for your display! :laugh:
Think about it: you can't add two Objects together (because there isn't an operator defined for that) so you have to cast them to numbers first.

For SMALLMONEY values, just cast them to Decimal:

bookfee.Text = string.Format("{0:0.00}", (decimal) rred[2]);
DarkDreamer08 18-Jan-15 5:48am    
hey, it works great! thanks for that answer! :laugh: Thank you so much!
OriginalGriff 18-Jan-15 5:52am    
You're welcome!
DarkDreamer08 18-Jan-15 5:45am    
I change the data type inside my database. I switched it to float. but when I tried your suggestion above, an error occurred: InvalidCastException Occurred: Specified cast is not valid.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900