H
Hexman
Hello All,
Well I'm stumped once more. Need some help. Writing a simple select and update program using VB.Net 2005 and an Access DB. I'm using parameters in
my update statement and when trying to update a record, I get a "No value given for one or more parameters." error message.
I use a Select with parameters and an Update with parameters. The select works fine. I thought I've tried everything (evidently not) to get this
working. Please show me the errors of my ways or a different way to solve. I purposely want to create the da, dt, cn, etc. in code so I will get
used to them.
Thanks,
Hexman
Here's the excerpt of the failing code. (dtRES contains the transactions to update dtCN. The index variables (I & Idx) are correct in their values.
Private cnCN As OleDbConnection
Private CNQrySel As String
Private CNQryAdd As String
Private CNQryUpd As String
Private CNQryDel As String
Private CNCount As Integer
Dim dtCN As New DataTable
Dim daCN As New OleDbDataAdapter
Dim cmbCN As New OleDbCommandBuilder(daCN)
CNQrySel = "Select CNDate,CNPart,CNLoc,CNDesc,CNAmt,CNValue " & _
"FROM CNMaster " & _
"WHERE (CNDate = ? and CNPart = ? and CNLoc = ?) ; "
CNQryAdd = " ; "
CNQryDel = " ; "
CNQryUpd = "UPDATE CNMaster " & _
"SET @CNDesc = ?, @CNAmt = ?, @CNValue = ?" & _
"WHERE (CNDate = ? and CNPart = ? and CNLoc = ?) ; "
' Create the SelectCommand and parameters.
daCN.SelectCommand = New OleDbCommand(CNQrySel, cnCN)
daCN.SelectCommand.Parameters.Add("CNDate", OleDbType.Date, 8)
daCN.SelectCommand.Parameters.Add("CNPart", OleDbType.VarChar, 15)
daCN.SelectCommand.Parameters.Add("CNLoc", OleDbType.VarChar, 6)
' Create the UpdateCommand and parameters.
daCN.UpdateCommand = New OleDbCommand(CNQryUpd, cnCN)
daCN.UpdateCommand.Parameters.Add("@CNDesc", OleDbType.VarChar, 25, "CNDesc")
daCN.UpdateCommand.Parameters.Add("@CNAmt", OleDbType.Single, 4, "CNAmt")
daCN.UpdateCommand.Parameters.Add("@CNValue", OleDbType.Single, 4, "CNValue")
daCN.UpdateCommand.Parameters.Add("@CNDate", OleDbType.Date, 8, "CNDate")
daCN.UpdateCommand.Parameters.Add("@CNPart", OleDbType.VarChar, 15, "CNPart")
daCN.UpdateCommand.Parameters.Add("@CNLoc", OleDbType.VarChar, 6, "CNLoc")
cnCN.Open()
daCN.SelectCommand.Parameters("CNDate").Value = dtRES.Rows(Idx).Item("STDate")
daCN.SelectCommand.Parameters("CNPart").Value = dtRES.Rows(Idx).Item("STPart")
daCN.SelectCommand.Parameters("CNLoc").Value = dtRES.Rows(Idx).Item("STLoc")
CNCount = daCN.Fill(dtCN)
daCN.UpdateCommand.Parameters("@CNDesc").Value = dtRES.Rows(Idx).Item("STDesc")
daCN.UpdateCommand.Parameters("@CNAmt").Value = dtRES.Rows(Idx).Item("STAmt")
daCN.UpdateCommand.Parameters("@CNValue").Value = dtRES.Rows(Idx).Item("STValue")
daCN.UpdateCommand.Parameters("@CNDate").Value = dtRES.Rows(Idx).Item("STDate")
daCN.UpdateCommand.Parameters("@CNPart").Value = dtRES.Rows(Idx).Item("STPart")
daCN.UpdateCommand.Parameters("@CNLoc").Value = dtRES.Rows(Idx).Item("STLoc")
dtCN.Rows(I).Item("CNDate") = dtRES.Rows(Idx).Item("STDate")
dtCN.Rows(I).Item("CNPart") = dtRES.Rows(Idx).Item("STPart")
dtCN.Rows(I).Item("CNLoc") = dtRES.Rows(Idx).Item("STLoc")
dtCN.Rows(I).Item("CNDesc") = dtRES.Rows(Idx).Item("STDesc")
dtCN.Rows(I).Item("CNAmt") = dtRES.Rows(Idx).Item("STAmt")
dtCN.Rows(I).Item("CNValue") = dtRES.Rows(Idx).Item("STValue")
Try
daCN.Update(dtCN)
Catch ex As Exception
'An exception occurred
MsgBox(ex.ToString)
End Try
dtCN.AcceptChanges()
cnCN.Close()
Well I'm stumped once more. Need some help. Writing a simple select and update program using VB.Net 2005 and an Access DB. I'm using parameters in
my update statement and when trying to update a record, I get a "No value given for one or more parameters." error message.
I use a Select with parameters and an Update with parameters. The select works fine. I thought I've tried everything (evidently not) to get this
working. Please show me the errors of my ways or a different way to solve. I purposely want to create the da, dt, cn, etc. in code so I will get
used to them.
Thanks,
Hexman
Here's the excerpt of the failing code. (dtRES contains the transactions to update dtCN. The index variables (I & Idx) are correct in their values.
Private cnCN As OleDbConnection
Private CNQrySel As String
Private CNQryAdd As String
Private CNQryUpd As String
Private CNQryDel As String
Private CNCount As Integer
Dim dtCN As New DataTable
Dim daCN As New OleDbDataAdapter
Dim cmbCN As New OleDbCommandBuilder(daCN)
CNQrySel = "Select CNDate,CNPart,CNLoc,CNDesc,CNAmt,CNValue " & _
"FROM CNMaster " & _
"WHERE (CNDate = ? and CNPart = ? and CNLoc = ?) ; "
CNQryAdd = " ; "
CNQryDel = " ; "
CNQryUpd = "UPDATE CNMaster " & _
"SET @CNDesc = ?, @CNAmt = ?, @CNValue = ?" & _
"WHERE (CNDate = ? and CNPart = ? and CNLoc = ?) ; "
' Create the SelectCommand and parameters.
daCN.SelectCommand = New OleDbCommand(CNQrySel, cnCN)
daCN.SelectCommand.Parameters.Add("CNDate", OleDbType.Date, 8)
daCN.SelectCommand.Parameters.Add("CNPart", OleDbType.VarChar, 15)
daCN.SelectCommand.Parameters.Add("CNLoc", OleDbType.VarChar, 6)
' Create the UpdateCommand and parameters.
daCN.UpdateCommand = New OleDbCommand(CNQryUpd, cnCN)
daCN.UpdateCommand.Parameters.Add("@CNDesc", OleDbType.VarChar, 25, "CNDesc")
daCN.UpdateCommand.Parameters.Add("@CNAmt", OleDbType.Single, 4, "CNAmt")
daCN.UpdateCommand.Parameters.Add("@CNValue", OleDbType.Single, 4, "CNValue")
daCN.UpdateCommand.Parameters.Add("@CNDate", OleDbType.Date, 8, "CNDate")
daCN.UpdateCommand.Parameters.Add("@CNPart", OleDbType.VarChar, 15, "CNPart")
daCN.UpdateCommand.Parameters.Add("@CNLoc", OleDbType.VarChar, 6, "CNLoc")
cnCN.Open()
daCN.SelectCommand.Parameters("CNDate").Value = dtRES.Rows(Idx).Item("STDate")
daCN.SelectCommand.Parameters("CNPart").Value = dtRES.Rows(Idx).Item("STPart")
daCN.SelectCommand.Parameters("CNLoc").Value = dtRES.Rows(Idx).Item("STLoc")
CNCount = daCN.Fill(dtCN)
daCN.UpdateCommand.Parameters("@CNDesc").Value = dtRES.Rows(Idx).Item("STDesc")
daCN.UpdateCommand.Parameters("@CNAmt").Value = dtRES.Rows(Idx).Item("STAmt")
daCN.UpdateCommand.Parameters("@CNValue").Value = dtRES.Rows(Idx).Item("STValue")
daCN.UpdateCommand.Parameters("@CNDate").Value = dtRES.Rows(Idx).Item("STDate")
daCN.UpdateCommand.Parameters("@CNPart").Value = dtRES.Rows(Idx).Item("STPart")
daCN.UpdateCommand.Parameters("@CNLoc").Value = dtRES.Rows(Idx).Item("STLoc")
dtCN.Rows(I).Item("CNDate") = dtRES.Rows(Idx).Item("STDate")
dtCN.Rows(I).Item("CNPart") = dtRES.Rows(Idx).Item("STPart")
dtCN.Rows(I).Item("CNLoc") = dtRES.Rows(Idx).Item("STLoc")
dtCN.Rows(I).Item("CNDesc") = dtRES.Rows(Idx).Item("STDesc")
dtCN.Rows(I).Item("CNAmt") = dtRES.Rows(Idx).Item("STAmt")
dtCN.Rows(I).Item("CNValue") = dtRES.Rows(Idx).Item("STValue")
Try
daCN.Update(dtCN)
Catch ex As Exception
'An exception occurred
MsgBox(ex.ToString)
End Try
dtCN.AcceptChanges()
cnCN.Close()