EDN Admin
Well-known member
HI im in a bit of a dilemma.. Im trying create a small payroll system using VB.Net windows form applications. The problem im currently facing is when im updating the datagridview. But my update doesnt work. IM using dataset and databinding source. I want
to udpate the datagrid view and automatically update the database.
Public Class Emp<br/>
Private Sub Btn_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_save.Click<br/>
SQL Connection<br/>
<br/>
Dim myconnection As SqlConnection<br/>
Dim mycommand As SqlCommand<br/>
myconnection = New SqlConnection("server=;uid=sa;pwd=sa;database=payroll")<br/>
myconnection.Open()<br/>
mycommand = New SqlCommand("Insert into Employee([Salutation],[FirstName],[LastName],[Address],[IDNo],[PinNo],[Status],[NSSF],[NHIF],[BankName],[AccountNo],[BranchCode]) values (" & cmbsalut.SelectedItem &
"," & txtfname.Text & "," & txtlname.Text & "," & txtaddr.Text & ", " & txtid.Text & ", " & txtpin.Text & ", " & cmbstatus.SelectedItem & ", " & txtNssf.Text & ", " & txtNhif.Text
& ", " & txtbank.Text & ", " & txtaccount.Text & ", " & txtbranch.Text & ")", myconnection)<br/>
mycommand.ExecuteNonQuery()<br/>
<br/>
MessageBox.Show("Employee Information Added", " Payroll System Message")<br/>
<br/>
End Sub
Private Sub Emp_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<br/>
<br/>
Me.EmployeeTableAdapter.Fill(Me.SpayrollEmpDataSet1.Employee)<br/>
<br/>
Me.EmployeeTableAdapter.Fill(Me.SpayrollEmpDataSet1.Employee)<br/>
<br/>
Dim sqlcn As New SqlConnection("server=;uid=sa;pwd=sa;database=payroll;")<br/>
Dim sadapt As New SqlDataAdapter("select * from Employee", sqlcn)<br/>
Dim ds As DataSet = New DataSet<br/>
sadapt.Fill(ds, "Employee")<br/>
DataGridView1.DataSource = ds.Tables("Employee")<br/>
<br/>
End Sub<br/>
Private Sub EmployeeBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)<br/>
Me.Validate()<br/>
Me.EmployeeBindingSource.EndEdit()<br/>
End Sub
Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click<br/>
dim da as data
<br/>
Dim dt As DataTable = SpayrollEmpDataSet1.Tables("Employee")<br/>
Me.DataGridView1.BindingContext(dt).EndCurrentEdit()<br/>
Me.EmployeeTableAdapter.Update(dt)<br/>
<br/>
End Sub
View the full article
to udpate the datagrid view and automatically update the database.
Public Class Emp<br/>
Private Sub Btn_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_save.Click<br/>
SQL Connection<br/>
<br/>
Dim myconnection As SqlConnection<br/>
Dim mycommand As SqlCommand<br/>
myconnection = New SqlConnection("server=;uid=sa;pwd=sa;database=payroll")<br/>
myconnection.Open()<br/>
mycommand = New SqlCommand("Insert into Employee([Salutation],[FirstName],[LastName],[Address],[IDNo],[PinNo],[Status],[NSSF],[NHIF],[BankName],[AccountNo],[BranchCode]) values (" & cmbsalut.SelectedItem &
"," & txtfname.Text & "," & txtlname.Text & "," & txtaddr.Text & ", " & txtid.Text & ", " & txtpin.Text & ", " & cmbstatus.SelectedItem & ", " & txtNssf.Text & ", " & txtNhif.Text
& ", " & txtbank.Text & ", " & txtaccount.Text & ", " & txtbranch.Text & ")", myconnection)<br/>
mycommand.ExecuteNonQuery()<br/>
<br/>
MessageBox.Show("Employee Information Added", " Payroll System Message")<br/>
<br/>
End Sub
Private Sub Emp_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<br/>
<br/>
Me.EmployeeTableAdapter.Fill(Me.SpayrollEmpDataSet1.Employee)<br/>
<br/>
Me.EmployeeTableAdapter.Fill(Me.SpayrollEmpDataSet1.Employee)<br/>
<br/>
Dim sqlcn As New SqlConnection("server=;uid=sa;pwd=sa;database=payroll;")<br/>
Dim sadapt As New SqlDataAdapter("select * from Employee", sqlcn)<br/>
Dim ds As DataSet = New DataSet<br/>
sadapt.Fill(ds, "Employee")<br/>
DataGridView1.DataSource = ds.Tables("Employee")<br/>
<br/>
End Sub<br/>
Private Sub EmployeeBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)<br/>
Me.Validate()<br/>
Me.EmployeeBindingSource.EndEdit()<br/>
End Sub
Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click<br/>
dim da as data
<br/>
Dim dt As DataTable = SpayrollEmpDataSet1.Tables("Employee")<br/>
Me.DataGridView1.BindingContext(dt).EndCurrentEdit()<br/>
Me.EmployeeTableAdapter.Update(dt)<br/>
<br/>
End Sub
View the full article