0

I am trying to insert some data into the database but getting the error mentioned above. The data type for this field in SQL Server is numeric(4,2). I have tried all kind of solution but cannot get it to work. here is my code:

 TextBox Hours = row.FindControl("Hrs") as TextBox; 

cmd.CommandText = "insert into myTable (ID, HOURS) values (@ID, @HOURS) ";
 cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = ID.Text;
 cmd.Parameters.Add("@HOURS", SqlDbType.VarChar).Value = Convert.ToInt32(Hours.ToString());

i have also tried to use decimal but not sure how to fix it. thanks

2
  • 4
    Calling ToString() on a TextBox will not give you a useful string Commented Feb 5, 2014 at 20:38
  • 1
    Why are you adding it as VarChar when it is Numeric? Commented Feb 5, 2014 at 20:38

4 Answers 4

2

You said the type is numeric, but you're trying to add varchar parameters.

Try changing how you're adding parameters:

cmd.Parameters.AddWithValue("@ID", Convert.ToInt32(ID.Text));
cmd.Parameters.AddWithValue("@HOURS", Convert.ToInt32(Hours.Text));
Sign up to request clarification or add additional context in comments.

Comments

2

If you want to pass an integer to your parameter, you should convert the Text property of the TextBox, not the instance variable. Calling ToString on the Hours TextBox return System.Windows.Forms.TextBox and of course this cannot be converted to an integer.

Of course the parameter type should be SqlDbType.Int not a VarChar

TextBox Hours = row.FindControl("Hrs") as TextBox; 
cmd.CommandText = "insert into myTable (ID, HOURS) values (@ID, @HOURS) ";
cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = ID.Text;
cmd.Parameters.Add("@HOURS", SqlDbType.Int).Value = Convert.ToInt32(Hours.Text);

If the field HOURS expects a decimal value (4,2) then you need to create a decimal type parameter. Also I suggest to add some check on the input box

decimal hourValue;
if(!decimal.TryParse(Hours.Text, out hourValue)    
   // Message to the user that he/she has typed an invalid number
else
{
    TextBox Hours = row.FindControl("Hrs") as TextBox; 
    cmd.CommandText = "insert into myTable (ID, HOURS) values (@ID, @HOURS) ";
    cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = ID.Text;
    cmd.Parameters.Add("@HOURS", SqlDbType.Decimal).Value = hourValue;
}

6 Comments

What it the value contained in the Hours TextBox?
okay the data type in the back end is numeric(4,2) and i am typing like 25 or 45
i am doing a for loop in the gridview so it does not like when the rest of the rows are null or blank. if i fill out all the rows in the gridview then it works. how can i overcome that?
It depends. Do you want null values or not? If not then inform the user, if yes (and the database table accepts null values) pass DBNull.Value as value for the parameter
how do i pass the dbnull value for the parameter?
|
1

Does this work for you?

object Hours = DBNull.Value;
Hours = row.FindControl("Hrs") as TextBox;     
cmd.CommandText = "insert into myTable (ID, HOURS) values (@ID, @HOURS) "; 
cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = ID.Text;
cmd.Parameters.Add("@HOURS", SqlDbType.Decimal).Value = Decimal.Parse((Hours.Text));

6 Comments

i am still getting this error: Input string was not in a correct format.
@moe On which line do you get that?
the last line where you say: Decimal.Parse((Hours.Text)
Debug that line and tell me what value is in Hours.Text
i am doing a for loop in the gridview so it does not like when the rest of the rows are null. if i fill out all the rows in the gridview then it works. how can i overcome that?
|
0

okay i found the solution.

  if (string.IsNullOrEmpty(Hours.Text))
                    cmd.Parameters.AddWithValue("@HOURS", DBNull.Value);
                else
                    cmd.Parameters.AddWithValue("@HOURS", decimal.Parse(Hours.Text));

1 Comment

decimal.Parse will crash your code if the user doesn't type a valid number

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.