hi,
i would like to assign the value of a combobox to a variable and use this variable as a parameter on the insert statement.see code below,code 1 works ok,but code 2 brings an error relating to wrong datatype,i cant seem to find where the problem because i think i am doing everything right.
1.
CmdinsertSite = New SqlCommand("insert into SiteStock(serialno,partno,technicianID,dispatcherID,ATMID,DateDispatched) values (@serialno,@partno,@technicianID,@dispatcherID,@ATMID,@DateDispatched)", AddConn)
CmdinsertSite.Parameters.AddWithValue("@serialno", cbSARserialused1.SelectedValue)
CmdinsertSite.Parameters.AddWithValue("@partno", cbSARpartused1.SelectedValue)
CmdinsertSite.Parameters.Add("@technicianID", SqlDbType.VarChar).Value = cbSARtech.SelectedValue
CmdinsertSite.Parameters.Add("@dispatcherID", SqlDbType.VarChar).Value = cbSARdispatcher.SelectedValue
CmdinsertSite.Parameters.Add("@ATMID", SqlDbType.VarChar).Value = cbSARatm.SelectedValue
CmdinsertSite.Parameters.Add("@DateDispatched", SqlDbType.SmallDateTime).Value = dtpSARdispatchdate.Text
CmdinsertSite.ExecuteNonQuery()
2.
CmdinsertSite = New SqlCommand("insert into SiteStock(serialno,partno,technicianID,dispatcherID,ATMID,DateDispatched) values (@serialno,@partno,@technicianID,@dispatcherID,@ATMID,@DateDispatched)", AddConn)
CmdinsertSite.Parameters.AddWithValue("@serialno", serialno)
CmdinsertSite.Parameters.AddWithValue("@partno", partno)
CmdinsertSite.Parameters.Add("@technicianID", SqlDbType.VarChar).Value = cbSARtech.SelectedValue
CmdinsertSite.Parameters.Add("@dispatcherID", SqlDbType.VarChar).Value = cbSARdispatcher.SelectedValue
CmdinsertSite.Parameters.Add("@ATMID", SqlDbType.VarChar).Value = cbSARatm.SelectedValue
CmdinsertSite.Parameters.Add("@DateDispatched", SqlDbType.SmallDateTime).Value = dtpSARdispatchdate.Text
dim partno as integer = cbSARpartused1.SelectedValue
dim serialno as integer = cbSARserialused1.SelectedValue
CmdinsertSite.ExecuteNonQuery()
please note that columns 'Serialno' and 'Partno' in table 'SiteStock' are both integer datatype.
code 2 brings error:
"The INSERT statement conflicted with the FOREIGN KEY constraint "FK__SiteStock__Seria__1367E606". The conflict occurred in database "ISIS", table "dbo.Stock", column 'SerialNo'."
i would like code 2 to work so that i can assign the variables different values at different scenarios.
column serialno is a foreign key from table Stock.
Please assist.