C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » VB.NET Syntax »

Three Tier application


Posted Date: 28 Oct 2009    Resource Type: Code Snippets    Category: VB.NET Syntax
Author: NeetuMember Level: Diamond    
Rating: 1 out of 5Points: 25 (Rs 10)



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


Attachments

  • Three Tier application (34465-2887-ThreetierAppl.rar)


  • Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    Dealing with Three Tier Application  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: StreamReader and StreamWriter class
    Previous Resource: Word Pad in .net
    Return to Discussion Resource Index
    Post New Resource
    Category: VB.NET Syntax


    Post resources and earn money!
     
    More Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use