2

I have a csv from where I import data to a database.

myCommand.Parameters.Add("@cn", SqlDbType.Text);
myCommand.Parameters["@cn"].Value = comp.Rows[i][1].ToString();

In case the value of comp.Rows[i][1] is X'496e626randomcharacters' I receive this error: The floating point value '496e626' is out of the range of computer representation (8 bytes). Incorrect syntax near '496e626'.

It should be handled as string/text so I don't understand the floating point error. What is wrong here?

8
  • 1
    How does the query look like? Commented Jan 14, 2014 at 12:41
  • 1
    Are you using a DataTable? What's the type of column [1]? Commented Jan 14, 2014 at 12:41
  • 1
    Have you tried using AddWithValue("@cn", comp.Rows[i][1])? Commented Jan 14, 2014 at 12:41
  • 1
    AddWithValue is more likely to suffer from implicit conversion problems than explicitly defining a parameter type. Commented Jan 14, 2014 at 12:43
  • 2
    When do you get the exception? Commented Jan 14, 2014 at 12:48

3 Answers 3

3

Use

 SqlDbType.VarChar

rather than

 SqlDbType.Text

SqlDbType.Text: String. A variable-length stream of non-Unicode data with a maximum length of 2 31 -1 (or 2,147,483,647) characters.

Ref.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this :

myCommand.Parameters.Add("@cn", SqlDbType.VarChar).Value=comp.Rows[i][1].ToString();

Comments

0

The error you are getting is coming from the SQL Server, and happens whenever SQL tries to convert a string value into a numeric value.

You haven't shown any of the relevant SQL code, but I assume you are passing this value into a stored procedure, which is then inserting it into a data table. Both the parameter type and column type in the table needs to be VARCHAR or TEXT as well, or SQL will try to do the conversion.

Also, be careful about doing comparisons with your parameter against numeric values; for example, if you try to do a comparison like:

WHERE @CN = 1.234

SQL will try to convert your VARCHAR to a floating point value. You can use methods like ISNUMERIC to avoid the comparisons that are going to fail.

Comments

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.