Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
i have kept rate_km,ac_rate_km as decimal(6,2) and allowed null but when i submit the textbox as null it shows error of

" Error converting data type nvarchar to decimal. "

my code is as above:
C#
cmd.CommandText = "usp_vehicleinsert";
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.Parameters.AddWithValue("@vehicle_name",txtvname.Text);
          cmd.Parameters.AddWithValue("@vehicle_type",ddlvtype.SelectedValue);
          cmd.Parameters.AddWithValue("@rate_km",txtrate_km.Text);
          cmd.Parameters.AddWithValue("@ac_rate_km", txtacrate_km.Text);
          cmd.Parameters.AddWithValue("@rate_day", txtrate_day.Text);
          cmd.Parameters.AddWithValue("@ac_rate_day",txtacrate_day.Text);
          cmd.Parameters.AddWithValue("@quantity",txtquantity.Text);
          int check = 0;
          cmd.Connection = con;
          con.Open();
          check = cmd.ExecuteNonQuery();
          con.Close();
          if (check == 1)
          {
              lblmsg.Visible = true;
              lblmsg.Text = "Vehicle registered";
          }
          else
          {
              lblmsg.Visible = true;
              lblmsg.Text = "error";
          }
Posted
Updated 12-Mar-13 5:29am
v2

You need to wrap it up in a parser.

If you change to code to be

C#
Decimal.Parse(txtrate_km.Text)


Then you should achieve what you are after.
The problem is caused because the Parameter object does not know how to convert from one type to another.

In order to handle the null value you could put something like this in place:

C#
txtrate_km.Text.IsNullOrEmpty() ? DBnull.value : Decimal.Parse(txtrate_km.Text)


What this will do is check if the text box is empty/null and send the correct dbnull value, else it will try and parse the entered text and send that decimal value.

I would also add validation code to ensure only numbers have been entered into the text box.
 
Share this answer
 
v2
Comments
Member 9671810 12-Mar-13 10:50am    
if i do the above

cmd.Parameters.AddWithValue("@rate_km",Decimal.Parse(txtrate_km.Text));
cmd.Parameters.AddWithValue("@ac_rate_km", Decimal.Parse(txtacrate_km.Text));
cmd.Parameters.AddWithValue("@rate_day", Decimal.Parse(txtrate_day.Text));
cmd.Parameters.AddWithValue("@ac_rate_day", Decimal.Parse(txtacrate_day.Text));


i get error as

Input string was not in a correct format.
Pheonyx 12-Mar-13 10:53am    
Try adding in the test for the "null" as the Decimal.Parse might be erroring when it receives a null value.
Sharp Robin 12-Mar-13 11:49am    
You should try using Decimal.TryParse instead and assign value according to success of your parsing.

Happy coding.
Member 9671810 12-Mar-13 11:13am    
i didn't get you.

i am a stater so.... :P
Pheonyx 12-Mar-13 11:19am    
Try putting


cmd.Parameters.AddWithValue("@rate_km",
String.IsNullOrEmpty(txtrate_km.Text) ? DBNull.Value : decimal.Parse(txtrate_km.Text));
I think you should pass DBNull.Value instead of null.
 
Share this answer
 
Comments
Member 9671810 12-Mar-13 10:51am    
i have not passed null i have checked allow null box in sql

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