HELP: Error converting data type varchar to bigint.

Ricardo Montoya | DYMSA 21 Reputation points
2020-11-02T18:43:04.573+00:00

hi,please help, when i try to insert data on the sql database have this error message appear. "Error converting data type varchar to bigint."

this is the current Code:

oDataadapter1 = New SqlDataAdapter("Insert into dbo.batch values ('" + Trim(txtBatch_ID.Text) + "','" + Trim("Doccas Food - Spain") + "','" + Trim("ReportName") + "','" + Trim("2020-10-26 13:00:00") + "','" + Trim("Ricardo") + "','" + Trim("2020-10-26 13:00:00") + "','" + Trim("2020-10-26 13:00:00") + "','" + Trim("2020-10-26 14:00:00") + "','" + "10" + "','" + "Test" + "','" + "1500.0" + "','" + "1510.0" + "','" + "1" + "','" + "4" + "','" + "200" + "','" + "210" + "','" + "2" + "','" + "3" + "','" + "0" + "," + "')", CC)
Dim oCmd1 As SqlCommandBuilder = New SqlCommandBuilder(oDataadapter1)
odataset1 = New DataSet
oDataadapter1.Fill(odataset1, "batch")

the data that generates the error is the number 19, (marked with bold, in the code there is a "0" to test)

Developer technologies | Visual Studio | Debugging
Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other
A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.
SQL Server | Other
0 comments No comments
{count} votes

Answer accepted by question author
  1. Viorel 125.6K Reputation points
    2020-11-02T18:53:32.203+00:00

    Try something like this: "2" + "','" + "3" + "'," + "0" + ")", CC), i.e. remove the incorrect apostrophes.

    Clarify if more values are required after this “0”. And consider Parameterised Queries, which are more reliable.

    If this is Visual Basic, then use ‘&’ instead of ‘+’.

    Show the new statement if still does not work.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Erland Sommarskog 128.7K Reputation points MVP Volunteer Moderator
    2020-11-02T23:11:47.01+00:00

    You should use a parameterised statement. This saves you from this mess of quotes. Here is a quite example:

    SqlCommand cmd = new SqlCommandI();
    cmd.CommandText = @"INSERT INTO dbo.batch_values VALUES(@par1, @par2, ...)
    cmd.Parameters.Add("@par1", SqlDbType.Int).Value = 35;
    cmd.Parameters.Add("@par2", SqlDbType.NVarChar, 40).Value = "Ricardo";
    ...
    

    And then you pass cmd to your DataAdapter.

    The above syntax is C#, but it is easily translated to VB .Net

    Concatenating strings to an SQL command like you do is an absolute no-no. It is very difficult to get right and it opens for SQL injection.

    0 comments No comments

  2. CathyJi-MSFT 22,401 Reputation points Microsoft External Staff
    2020-11-03T02:17:54.127+00:00

    Hi @Ricardo Montoya | DYMSA

    Your issue is related to Visual Basic, VB is currently not supported in the Q&A forums, the supported products are listed over here https://learn.microsoft.com/en-us/answers/products (more to be added later on).

    I suggest you ask the experts in the VB forum over here:
    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vbgeneral

    People there will help you more effectively.

    Best regards,
    Cathy


    If the response is helpful, please click "Accept Answer" and upvote it.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.