Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have text box in win form
in which user give input
i am not sure in which formate user type value (either 10 or 10.0 or 10.00)
i am storing this value to sql server database
and want this value in (10.00 format either 10 or 10.0 or 10.00)
sql column data type is float

when i saw in table in sql i saw value as 10 or 10.0 or 10.00 which user type

but i want this value as 10.00 when user type 10 or 10.0

how can i manage this situation

can someone explain step by step ?

What I have tried:

using (SqlConnection conn = new SqlConnection(con))
{
using (SqlCommand cmd = new SqlCommand("usp_weightinsertdetails", conn))
{
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue("@Date", dateTimePicker1.Value);
cmd.Parameters.AddWithValue("@ProductName", textBox1.Text);
cmd.Parameters.AddWithValue("ProductPieces", int.Parse(textBox2.Text));

float weight, pc, avr;
weight = float.Parse(textBox3.Text.tostring("0.00"));
pc = float.Parse(textBox2.Text);
avr = weight / pc;

cmd.Parameters.AddWithValue("@ProductWeight", weight.tostring("0.00"));
cmd.Parameters.AddWithValue("@AveragePieceWeight", avr.tostring("0.00"));

conn.Open();
cmd.ExecuteNonQuery();
Posted
Updated 3-Jul-16 2:56am
Comments
Karthik_Mahalingam 3-Jul-16 8:52am    
what is the column type in sql table

syntax error
weight = float.Parse(textBox3.Text);

1 solution

First, convert the user input to a number:
C#
double userInput;
if (!double.TryParse(myTextBox.Text, out userInput))
   {
   // Report a problem with what he typed.
   ...
   return;
   }
Then pass the value as a parameter, just like you are, but using userInput instead of the text box content.
[edit]One line was outside the code block.[/edit]
 
Share this answer
 
v2
Comments
Member 12592153 3-Jul-16 9:06am    
can u explain it in some more details ?

how to store value in sql ?
OriginalGriff 3-Jul-16 9:15am    
What more detail do you need? You already have code to store values in SQL correctly...
Member 12592153 3-Jul-16 9:25am    
how to pass value ?
like "@ProductWeight", userInput.tostring("0.00")
or "@ProductWeight", userInput ?

i am totally confused
i am a newbe so forgive me for asking explanation :)
OriginalGriff 3-Jul-16 9:29am    
Just

cmd.Parameters.AddWithValue("@ProductWeight",userInput);

Always pass that actual value to SQL via a parameter, never convert it to a string first! If you convert it, something has to convert it back again and that's where problem start to creep back in. So remove the ToString from all your parameters.
And remember: C# is case sensitive, so ToString is not the same as tostring - the first is correct, the second is a compiler error!
Member 12592153 3-Jul-16 9:35am    
thanx for help
the error is solve pplus sql data is now display 10.00 thanx once again

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