Private Sub Btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnsave.Click
If StaffIDTextBox.Text = "" Or NameTextBox.Text = "" Or LnameTextBox.Text = "" Or AddressTextBox.Text = "" Or UsernameTextBox.Text = "" Or PasswordTextBox.Text = "" Or TelTextBox.Text = "" Or EmailTextBox.Text = "" Then
MsgBox("กรุณากรอกข้อมูลให้ครบทุกช่อง", MsgBoxStyle.Exclamation, "แจ้งเตือน")
Return
End If
Dim con As New SqlConnection(strcon)
If con.State = ConnectionState.Closed Then
Try
con.Open()
Catch ex As Exception
MsgBox("ไม่สามารถเชื่อมถานข้อมูลได้", MsgBoxStyle.Critical, "เตือน")
Return
End Try
End If
Try
Dim strselect As String = "insert into staff (StaffID,Name,Lname,Address,Username,Password,Tel,Email) values ('" + StaffIDTextBox.Text + "','" + NameTextBox.Text + "','" _
+ LnameTextBox.Text + "','" + AddressTextBox.Text + "','" + UsernameTextBox.Text + "','" + PasswordTextBox.Text + "','" + TelTextBox.Text + "','" + EmailTextBox.Text + "')"
Dim com As New SqlCommand
com.CommandType = CommandType.Text
com.CommandText = strselect 'เลือกคำสั่ง
com.Connection = con 'เปืดถานข้อมูล
com.ExecuteNonQuery() 'ประมวลผล
Dim da As New SqlDataAdapter("Select * from staff", con)
Dim ds As DataSet = New DataSet
da.Fill(ds, "staff")
StaffDataGridView.DataSource = ds.Tables("staff")
StaffDataGridView.Sort(StaffDataGridView.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
MsgBox("บันทึกข้อมูลเรียบร้อย", MsgBoxStyle.Information, "เตือน")
Catch ex As Exception
End Try
End Sub
Tag : .NET, VS 2008 (.NET 3.x), VS 2010 (.NET 4.x)