In this using usercreate class and gridview user can insert more than one record at a time.
User created class
'System.Data.OleDb namespace deals with the database transcation Imports System.Data.OleDb Public Class Conn Dim con As New OleDbConnection Dim da As New OleDbDataAdapter Dim dt As New DataTable Dim str As New System.Text.StringBuilder Sub New() 'Using StrigBuilder class creating the connection str.Append("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=") 'AppDomain.CurrentDomain.BaseDirectory will return the path of bin folder str.Append(AppDomain.CurrentDomain.BaseDirectory) str.Append("yogita.mdb") str.Append(";Persist Security Info=False") 'MsgBox(str.ToString) con.ConnectionString = str.ToString
'If connection is opened or broken or there is any problem then it will first close the connection If con.State = ConnectionState.Open Or con.State = ConnectionState.Broken Then con.Close() End If
'When connection is closed it will open the connection If con.State = ConnectionState.Closed Then con.Open() End If End Sub 'Selectdata is function that will accept the table_name and return you fill data from that data in the form of DataTable Function SelectData(ByVal tablename As String) As DataTable Dim s As String s = "select * from " & tablename Dim cmd As New OleDbCommand cmd.Connection = con cmd.CommandType = CommandType.Text cmd.CommandText = s da = New OleDbDataAdapter(cmd) da.Fill(dt) cmd.Dispose() Return dt End Function 'It will take records from the form and save it in the table if there is any problem then it will return false else it will return true Function Insertdata(ByVal dt As DataTable) As Boolean Dim cmd As New OleDbCommand Dim i As Integer Dim flag As Boolean = False
For i = 0 To dt.Rows.Count - 1 cmd = New OleDbCommand str = New System.Text.StringBuilder 'Str will create the whole insert statement.
str.Append("insert into studentdetails values (") str.Append(dt.Rows(i).Item(0)) str.Append(",'") str.Append(dt.Rows(i).Item(1)) str.Append("','") str.Append(dt.Rows(i).Item(2)) str.Append("',") str.Append(dt.Rows(i).Item(3)) str.Append(")") cmd.Connection = con cmd.CommandType = CommandType.Text cmd.CommandText = str.ToString
'ExecuteNonQuery will return true if it will effect the table or will return false If cmd.ExecuteNonQuery() Then flag = True Else flag = False Exit For End If 'Clearing the memory cmd = Nothing str = Nothing Next
If flag = True Then Return True Else Return False
End If End Function End Class
Form1.vb
'It will import the class that i have created. Imports ThreetierAppl.Conn Public Class Form1 'Creating the object of conn class Dim obj As New Conn Dim dt As New DataTable Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load showdata() ' gv.Sort(gv.Columns(1), System.ComponentModel.ListSortDirection.Descending) End Sub Sub showdata() 'Creating the column checkbox column in datatable Dim col As New DataColumn col.Caption = "Check" col.DataType = System.Type.GetType("System.Boolean") col.DefaultValue = False dt = obj.SelectData("studentdetails") dt.Columns.Add(col) gv.DataSource = dt gv.Columns(4).DisplayIndex = (0) gv.Columns(4).HeaderText = "Select"
End Sub
Private Sub Insert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Insert.Click Dim dt1 As New DataTable 'Dt1 will have all the new records that are added from the user. dt1 = dt.GetChanges(DataRowState.Added) 'It will call the insertdata method of conn class and will return true or false. If obj.Insertdata(dt1) Then MsgBox("Data inserted") Else MsgBox("Error in insertion") End If End Sub End Class
You can check attachement also regards Neetu
AttachmentsThree Tier application (34465-2887-ThreetierAppl.rar)
|
No responses found. Be the first to respond and make money from revenue sharing program.
|