[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1092
  • Last Modified:

vb.net parameterized query fails looking for existing parameter

I get the following error on the following code at (cmd.executenonquery) and can't see the problem:

Parameterized Query '(@TS datetime,@M varchar(300),@H varchar(300),@A varchar(300),@M' expects parameter @TS, which was not supplied.

The code is in the code snippet bit.

I have checked the cmd object at the point of failure and it has 10 parameters, one of which is indeed the one shown in the error.

Any help appreciated - even if its just telling me what I have obviously missed!!

Andy.
' send data to database
 
        Dim conString As String = "Server=xxxxx\yyyyyyyy;Database=zzzzzzz;Trusted_Connection=True;"
        Dim con As New SqlConnection(conString)
        Dim cmdCommand As String
 
        con.Open()
        With dsSpreads.Tables("ssssss")
            cmdCommand = "INSERT INTO sssTable (ts, m, ht, at," & _
                        " mk, s, b, n, c, t)" & _
                        " VALUES (@TS, @M, @H, @A, @MK, @S, @B, @N, @C, @T )"
            For i As Integer = 0 To .Rows.Count - 1
 
                Dim cmd As New SqlCommand(cmdCommand, con)
                cmd.Parameters.Add("@TS", SqlDbType.DateTime, 50, .Rows(i).Item("ts"))
                cmd.Parameters.Add("@M", SqlDbType.VarChar, 300, .Rows(i).Item("m"))
                cmd.Parameters.Add("@H", SqlDbType.VarChar, 300, .Rows(i).Item("h"))
                cmd.Parameters.Add("@A", SqlDbType.VarChar, 300, .Rows(i).Item("a"))
                cmd.Parameters.Add("@MK", SqlDbType.VarChar, 300, .Rows(i).Item("mk"))
                cmd.Parameters.Add("@S", SqlDbType.Decimal, 18, .Rows(i).Item("s"))
                cmd.Parameters.Add("@B", SqlDbType.Decimal, 18, .Rows(i).Item("b"))
                cmd.Parameters.Add("@N", SqlDbType.VarChar, 1000, .Rows(i).Item("n"))
                cmd.Parameters.Add("@C", SqlDbType.VarChar, 100, .Rows(i).Item("c"))
                cmd.Parameters.Add("@T", SqlDbType.VarChar, 100, .Rows(i).Item("t"))
 
                cmd.ExecuteNonQuery()
 
                cmd = Nothing
            Next
        End With
        con.Close()

Open in new window

0
andycrellin
Asked:
andycrellin
1 Solution
 
CarlVerretCommented:
Hi,

I suggest you to use the : cmd.Parametes.AddWithValue (@name, _value_) function instead.  It worked really fine for me!

Hope this helps!
0
 
andycrellinAuthor Commented:
Well whadyaknow! Worked perfectly, thanks.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now