I'm not very clear with the problem you have. But based onthe input let me try to explain.One option is to save the changes as soon as the user enters it, This is possible if we listen for ColumnChanged events coming off the DataTable and you can prompt a message whether to save the record or not when they try tp enter another one. you can also keep a hidden field in the DataTable to set status of the save.
For this to work you need to include the below import statement at the very begining
Imports System.Data.SqlClient
'Use the 127.0.0.1 if you are accessing local sql server, else give the IP of the SQL server
'Also give the username and passord
dim ConnectionString as String = "data source=127.0.0.1;user id=sa;password="
'This is the query, modify this as per your requirement
dim SQLQuery as String = "Select * from YOURTABLENAME"
'Create the connection
Dim Connection as new SqlConnection(ConnectionString)
Connection.Open()
dim adapter as new SqlAdapter(SqlQuery,Connection)
dim Bldr as New SqlCommandBuilder(adapter)
adapter.update(DATATABLENAME)
adapter.dispose()
connection.Close
Regards
ManojRajan
Jawaban Yang lain
Imports System.Data.SqlClient
Public Class frmDataTable
Inherits System.Windows.Forms.Form
Private cn As New SqlConnection("server=(local);uid=sa;pwd=;database=emp")
Private WithEvents dadept As New SqlDataAdapter("select * from dept", cn)
Private dt As DataTable
Private Sub frmDataTable_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dt = New DataTable("Dept")
Dim dc1 As New DataColumn("dname")
dt.Columns.Add(dc1)
Dim dc2 As New DataColumn("loc")
dt.Columns.Add(dc2)
dgdept.DataSource = dt
txtDname.Focus() 'THIS IS TEXTBOX FOR DEPT NAME
End Sub
Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
Dim drow As DataRow = dt.NewRow
drow("dname") = txtDname.Text
drow("loc") = txtDloc.Text 'THIS IS TEXTBOX FOR LOC NAME
dt.Rows.Add(drow)
btnClear_Click(sender, e) 'THIS BUTTON CLEAR CONTENTS
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim cb As New SqlCommandBuilder(dadept)
Dim dt1 As DataTable
Try
dt1 = CType(dgdept.DataSource, DataTable)
dadept.Update(dt1)
dt.AcceptChanges()
MessageBox.Show("Data Updated To Databse...")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
txtDname.Clear()
txtDloc.Clear()
txtDname.Focus()
End Sub
Private Sub dadept_RowUpdated(ByVal sender As Object, ByVal e As System.Data.SqlClient.SqlRowUpdatedEventArgs) Handles dadept.RowUpdated
If e.Status = UpdateStatus.ErrorsOccurred Then
e.Row.RowError = "Sandeep Says " & e.Errors.Message
e.Status = UpdateStatus.Continue
End If
End Sub
End Class
Tidak ada komentar:
Posting Komentar