您好我正在尝试执行一个简单的代码,它为上面提到了附加信息" Column' Service_Type_ID'被限制为独特的。价值' S2'已经存在"。 我是编程新手。请帮帮我。
当我尝试从组合框中选择项目并将其索引传递给文本框时,我得到此异常。代码的stared部分是为了那个
Dim cmd As SqlCommand = New SqlCommand
Dim con As New SqlConnection("Data Source=CHAMIKA\SQLEXPRESS; Initial Catalog=MyHotelManagementSystem; Integrated Security=True")
Private Function ValidData() As Boolean
If String.IsNullOrEmpty(Me.txtBoxID.Text) Then
MessageBox.Show("Service ID is empty.")
Me.txtBoxID.Focus()
Return False
End If
If String.IsNullOrEmpty(Me.cmbSerType.Text) Then
MessageBox.Show("Service Type is empty.")
Me.cmbSerType.Focus()
Return False
End If
If String.IsNullOrEmpty(Me.txtBoxName.Text) Then
MessageBox.Show("Service Name is empty.")
Me.txtBoxName.Focus()
Return False
End If
If String.IsNullOrEmpty(Me.txtBoxRate.Text) Then
MessageBox.Show("Service Rate is empty.")
Me.txtBoxRate.Focus()
Return False
End If
Return True
End Function
**Private Sub cmbSerType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbSerType.SelectedIndexChanged
Try
Dim selectedVal = cmbSerType.SelectedValue
txtBoxTypeID.Text = selectedVal
Catch ex As Exception
End Try
End Sub**
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim cmd As SqlCommand = New SqlCommand
If ValidData() Then
Try
cmd.Connection = con
con.Open()
cmd.CommandType = System.Data.CommandType.Text
cmd.CommandText = "Insert Into dbo.Service (Service_ID,Service_Type_ID,Name,Rate) Values (@ServID,@TypeID,@Name,@Rate) "
cmd.Parameters.AddWithValue("@ServID", txtBoxID.Text)
cmd.Parameters.AddWithValue("@TypeID", txtBoxTypeID.Text)
cmd.Parameters.AddWithValue("@Name", txtBoxName.Text)
cmd.Parameters.AddWithValue("@Rate", txtBoxRate.Text)
cmd.ExecuteNonQuery()
MsgBox("Succesfully Added", MsgBoxStyle.Information, "add")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
If Not ValidData() Then
Exit Sub
End If
con.Close()
txtBoxID.Clear()
txtBoxName.Clear()
txtBoxRate.Clear()
'txtBoxSerID.Clear()
End If
End Sub
Private Sub Service_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'MyHotelManagementSystemDataSet26.Service_Type' table. You can move, or remove it, as needed.
Me.Service_TypeTableAdapter.Fill(Me.MyHotelManagementSystemDataSet26.Service_Type)
'TODO: This line of code loads data into the 'MyHotelManagementSystemDataSet25.Service' table. You can move, or remove it, as needed.
End Sub
Private Sub btnSerach_Click(sender As Object, e As EventArgs) Handles btnSerach.Click
Dim cmd As SqlCommand = New SqlCommand
DataGridView2.Columns.Clear()
Dim stm As String
con.Open()
stm = "SELECT * FROM Service"
cmd.CommandType = CommandType.Text
cmd.CommandText = stm
cmd.Connection = con
da = New SqlDataAdapter(cmd)
dt = New DataTable
da.Fill(dt)
Dim cb As New SqlCommandBuilder(da)
With DataGridView2
.AutoGenerateColumns = True
.DataSource = dt
End With
cmd.Dispose()
cmd = Nothing
con.Close()
End Sub
Private Sub DataGridView_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView2.CellClick
Dim i As Integer
i = DataGridView2.CurrentRow.Index
If i >= 0 Then
Me.txtBoxID.Text = DataGridView2.Item(0, i).Value
Me.txtBoxTypeID.Text = DataGridView2.Item(1, i).Value
Me.txtBoxName.Text = DataGridView2.Item(2, i).Value
Me.txtBoxRate.Text = DataGridView2.Item(3, i).Value
Else
MessageBox.Show("Empty Row Clicked")
End If
End Sub
Private Sub butnUpdate_Click(sender As Object, e As EventArgs) Handles butnUpdate.Click
Dim cmd As SqlCommand = New SqlCommand
Try
cmd.Connection = con
con.Open()
cmd.CommandText = "UPDATE Service SET Service_Type_ID = @ID,Name = @Name" & _
"Rate = @Rate WHERE Service_ID = @SID"
cmd.Parameters.AddWithValue("@ID", txtBoxTypeID.Text)
cmd.Parameters.AddWithValue("@Name", txtBoxName.Text)
cmd.Parameters.AddWithValue("@Rate", txtBoxRate.Text)
cmd.Parameters.AddWithValue("@SID", txtBoxID.Text)
cmd.ExecuteNonQuery()
MessageBox.Show("Successfully Updated")
Catch ex As Exception
MessageBox.Show("Error while inserting record on table..." & ex.Message, "Update Records")
Finally
con.Close()
btnSave.Show()
txtBoxTypeID.Clear()
txtBoxName.Clear()
txtBoxRate.Clear()
txtBoxID.Clear()
End Try
End Sub
结束班