Tuesday, June 2, 2009

No value given for one or more required parameters - soultion : insert values into database using vb.net

How to Insert values into msaccess database using oledb and find the solution of error "No value given for one or more required parameters" :

Dim myOleDbConnection As OleDb.OleDbConnection
Dim insert As String
Dim value1, value2, value3, value4 As String

value1 = "'" & "ContactID" & "'" ' So to avoid the error("No value given for one or more required parameters") use ' single qoutes before and end of coulumn value
value2 = "'" & "FirstName" & "'"
value3 = "'" & "LastName" & "'"
value4 = "'" & "Sent" & "'"

Try


Dim myConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=C:\Documents and Settings\Administrator\Desktop\schoolapp\SchoolApp\school.mdb;"
'"Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source = C:\Users\Edward\Desktop\db1.mdb;"

insert = "INSERT INTO Cancelled (ContactID, FirstName, LastName, Sent) VALUES (" & value1 & " ," & value2 & " ," & value3 & " ," & value4 & ")"



myOleDbConnection = New OleDb.OleDbConnection(myConnectionString)
myOleDbConnection.Open()
Dim myOleDbCommand As New OleDb.OleDbCommand(insert, myOleDbConnection)

myOleDbCommand = myOleDbConnection.CreateCommand

myOleDbCommand.CommandType = CommandType.Text

myOleDbCommand.CommandText = insert

myOleDbCommand.ExecuteNonQuery()

myOleDbConnection.Close()

Catch ex As Exception
Trace.WriteLine(ex.ToString)
End Try


'-------Correct Query--------
' when you trace the value of insert will be : INSERT INTO Cancelled (ContactID, FirstName, LastName, Sent) VALUES ('ContactID' ,'FirstName' ,'LastName' ,'Sent')

'------Wrong Query-------
' basically the problem ("No value given for one or more required parameters") accurs when insert query will be :
' INSERT INTO Cancelled (ContactID, FirstName, LastName, Sent) VALUES (ContactID ,FirstName ,LastName ,Sent)
' because here inserted values doesn't have ' single quotes before and end of coulmn value which is mandatory while inserting data.

End Sub

No comments:

Post a Comment