创建此代码以将项目作为emp_state插入到表名中,问题出在@ d3中,当我从组合框中选择项目时,它给出了参数@ d3没有默认值的错误?
这是代码,请给我一个解决方案
Dim sSQL As String = "insert into emp_state(Badge,Name,State,Date_of_Join,Date_of_Termination,Reasons,Information) values (@d1,@d2,@d3,@d4,@d5,@d6,@d7)"
Dim cmd As OleDbCommand = New OleDbCommand(sSQL, cn)
' Badge
Dim Badge As OleDbParameter = New OleDbParameter("@d1", OleDbType.VarWChar, 8)
Badge.Value = TextBox1.Text.ToString()
cmd.Parameters.Add(Badge)
' Name
Dim Name As OleDbParameter = New OleDbParameter("@d2", OleDbType.VarWChar, 50)
Name.Value = TextBox2.Text.ToString()
cmd.Parameters.Add(Name)
' State
ComboBox1.SelectedIndex = 0
Dim State As OleDbParameter = New OleDbParameter("@d3", OleDbType.VarWChar, 20)
ComboBox1.SelectedIndex = ComboBox1.Items.IndexOf("State")
cmd.Parameters.Add(State)
'Date_of_Join
Dim Date_of_Join As OleDbParameter = New OleDbParameter("@d4", OleDbType.Date, 15)
Date_of_Join.Value = dtpDOB.Text.ToString()
cmd.Parameters.Add(Date_of_Join)
'Date_of_Termination
Dim Date_of_Termination As OleDbParameter = New OleDbParameter("@d5", OleDbType.Date, 15)
Date_of_Termination.Value = DateTimePicker1.Text.ToString()
cmd.Parameters.Add(Date_of_Termination)
'Reasons
Dim Reasons As OleDbParameter = New OleDbParameter("@d6", OleDbType.VarWChar, 9999)
Reasons.Value = TextBox3.ToString()
cmd.Parameters.Add(Reasons)
'Information
Dim Information As OleDbParameter = New OleDbParameter("@d7", OleDbType.VarWChar, 9999)
Information.Value = TextBox4.ToString()
cmd.Parameters.Add(Information)
答案 0 :(得分:0)
' State
ComboBox1.SelectedIndex = 0
Dim State As OleDbParameter = New OleDbParameter("@d3", OleDbType.VarWChar, 20)
ComboBox1.SelectedIndex = ComboBox1.Items.IndexOf("State")
cmd.Parameters.Add(State)
再次阅读本部分。
我想你想做的是:
' State
' ComboBox1.SelectedIndex = 0
Dim State As OleDbParameter = New OleDbParameter("@d3", OleDbType.VarWChar, 20)
State.Value = ComboBox1.SelectedItem.Value
cmd.Parameters.Add(State)