user1570048
user1570048

Reputation: 880

Exception: no value given for one or more parameter VB.NET due to the use of TEXT type in CommandText

i am using the following code to create to save my DataTable content to and empty mdb database, so first i create the datata table and when i try to insert data i get the following error: no value given for one or more parameter, now i think this error is caused by the use of Text as mentioned in this link

but i am not sure how to fix it, here is the code and i get the exception in the commented line

Dim cmd = cnn.CreateCommand
                cmd.CommandText = "CREATE TABLE productstable(supplierid Integer ,catalogid Integer ,ccode Text ,cname Text,pother2 Text, pother3 Double, cprice Double, rank Integer, rankchange Integer , PD Double, PDP Double , TPD Double, TPDP Double, sprice Double, msprice Double, minprice Double, sopriceDB Double, pother4 Long, usedsuppliermargin Double, cimageurl Text, ccategory Integer, pricetaggroup Byte , spricemargingroup Byte)"
                cmd.ExecuteNonQuery()

                Dim ocmd As New OleDbCommand("INSERT INTO productstable(supplierid , catalogid, ccode,cname,pother2,pother3,cprice,rank,rankchange,PD,PDP,TPD,TPDP,sprice,msprice,minprice,sopriceDB,pother4,usedsuppliermargin,cimageurl,ccategory,pricetaggroup,spricemargingroup) VALUES (@supplierid , @catalogid, @ccode, @cname, @pother2, @pother3, @cprice, @rank, @rankchange, @PD, @PDP, @TPD, @TPDP, @sprice, @msprice, @minprice, @sopriceDB, @pother4, @usedsuppliermargin,@cimageurl,@ccategory,@pricetaggroup,@spricemargingroup)", cnn)

                For i = 0 To mainDatatable.Rows.Count - 1
                    ocmd.Parameters.Add(New OleDbParameter("@supplierid", mainDatatable.Rows(i)("supplierid")))
                    ocmd.Parameters.Add(New OleDbParameter("@catalogid", mainDatatable.Rows(i)("catalogid")))
                    ocmd.Parameters.Add(New OleDbParameter("@ccode", mainDatatable.Rows(i)("ccode")))
                    ocmd.Parameters.Add(New OleDbParameter("@cname", mainDatatable.Rows(i)("cname")))
                    ocmd.Parameters.Add(New OleDbParameter("@pother2", mainDatatable.Rows(i)("pother2")))
                    ocmd.Parameters.Add(New OleDbParameter("@pother3", mainDatatable.Rows(i)("pother3")))
                    ocmd.Parameters.Add(New OleDbParameter("@cprice", mainDatatable.Rows(i)("cprice")))
                    ocmd.Parameters.Add(New OleDbParameter("@rank", mainDatatable.Rows(i)("rank")))
                    ocmd.Parameters.Add(New OleDbParameter("@rankchange", mainDatatable.Rows(i)("rankchange")))
                    ocmd.Parameters.Add(New OleDbParameter("@PD", mainDatatable.Rows(i)("PD")))
                    ocmd.Parameters.Add(New OleDbParameter("@PDP", mainDatatable.Rows(i)("PDP")))
                    ocmd.Parameters.Add(New OleDbParameter("@TPD", mainDatatable.Rows(i)("TPD")))
                    ocmd.Parameters.Add(New OleDbParameter("@TPDP", mainDatatable.Rows(i)("TPDP")))
                    ocmd.Parameters.Add(New OleDbParameter("@sprice", mainDatatable.Rows(i)("sprice")))
                    ocmd.Parameters.Add(New OleDbParameter("@msprice", mainDatatable.Rows(i)("msprice")))
                    ocmd.Parameters.Add(New OleDbParameter("@minprice", mainDatatable.Rows(i)("minprice")))
                    ocmd.Parameters.Add(New OleDbParameter("@sopriceDP", mainDatatable.Rows(i)("sopriceDP")))
                    ocmd.Parameters.Add(New OleDbParameter("@pother4", mainDatatable.Rows(i)("pother4")))
                    ocmd.Parameters.Add(New OleDbParameter("@usedsuppliermargin", mainDatatable.Rows(i)("usedsuppliermargin")))
                    ocmd.Parameters.Add(New OleDbParameter("@cimageurl", mainDatatable.Rows(i)("cimageurl")))
                    ocmd.Parameters.Add(New OleDbParameter("@ccategory", mainDatatable.Rows(i)("ccategory")))
                    ocmd.Parameters.Add(New OleDbParameter("@pricetaggroup", mainDatatable.Rows(i)("pricetaggroup")))
                    ocmd.Parameters.Add(New OleDbParameter("@spricemargingroup", mainDatatable.Rows(i)("spricemargingroup")))

                    ocmd.ExecuteNonQuery()
                    ocmd.Parameters.Clear()

                Next

Upvotes: 0

Views: 415

Answers (2)

Scott Selby
Scott Selby

Reputation: 9570

I don't know how that is working if you left it like in the question , Look at this line

  cmd.Parameters.Add(New OleDbParameter("@sopriceDP", mainDatatable.Rows(i)("sopriceDP")))

and compare to Command

..@msprice, @minprice, @sopriceDB, @pother4,..

@sopriceDP on one and @sopriceDB on the other

Upvotes: 1

John Woo
John Woo

Reputation: 263723

It should be the ocmd that you have to add parameters and not the cmd.

ocmd.Parameters.Add(New OleDbParameter("@s....

and one more thing, you have to clear the parameters after the ExecuteNonQuery

For i = 0 To mainDatatable.Rows.Count - 1
    ocmd.Parameters.Add(New OleDbParameter("@s....
    ....

    ocmd.ExecuteNonQuery()
    ocmd.Parameters.Clear()
Next

Upvotes: 1

Related Questions