Friday, October 2, 2009

My first VB.NET Database Application Tutorial Source Code

INTRODUCTION
This practical guide is created for those friends who are newbie

in VB.NET database programming and searching help in Google for understanding the concept of
VB.NET. I have explained few examples in this book for creating data entry
module. This book is easy and after going through the examples given in book
you will be able to get an idea for creating Database Programming.

When I was learning VB.NET I was searching for a tutorial

which will explain step-by-step guide for creating a database programming but
fail to get so, then I have decided that after learning I will put tutorial on
net for new learner of VB.Net which will explain each and every line as we
learned at nursery.

Although I have tried to explain each and every step by

putting screenshots but you might get any trouble if so please feel free to
contact me at ranjansatpathy@gmail.com


Chapter – I

Requirements
Before creating data entry form in VB.NET you need to

install the VB.NET Express Edition 2008, SQL Server Express & SQL Server
Management Studio which is easily available in Microsoft Website.

For downloading Microsoft VB.NET 2008

Figure 1.0 : Screenshot of VB.NET
URL:
For downloading Microsoft SQL SERVER Express

Figure 1.1 : Screenshot of SQL SERVER
URL:
For downloading SQL Server Management Studio Express



Figure 1.2: Screen
Shot of Management Studio



URL:










Chapter – II





Database Designing





To create a new database Challan
you need to open management studio as shown in Figure
1.2 ,
Right click on database tree select new database and type the database name
challan and click Ok .








Creating table



From the very left top corner click on new query type the
following script to create the table party.









create table Party (

slno int primary key,
compcd varchar(50),

cname varchar(50),

add1 text,

add2 text,

add3 text,

tin text,

cstate text,

supcd text,

central text,

excom text,

mailname text)






and click on execute button to create the table
Party.








Chapter – III



Creating Form and Module

Now Open VB.NET and create a new project



Create a Module from Project Add Module and type the
following.

Imports System.Data.SqlClient

Module Module1
Public cn As New SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=CHALLAN;Data Source=RANJAN\SQLEXPRESS")
Public da As New SqlDataAdapter
Public DS As New DataSet
End Module

Note: In above connection parameter
you need to change the datasource as showing in your
SQL SERVER. As it is RANJAN\SQLEXPRESS in my PC
.

Design

the form by putting the shown objects, and change the objects name according to
the table fieldname
SlNo,CompCd,CName,Add1,Add2,Add3, TinNo,Cstate,SupCd

Central,ExCom

and MailName











Chapter – IV



Code writing





Now you have designed the Form now you need to put some code
in event of
combobox changing, click of add button, click
of save button just type what I am giving below and try to understand the each
and every line of code what it is doing when a user is clicking on certain
button or object.





Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If cn.State = ConnectionState.Closed Then
cn.Open()
End If
da = New SqlDataAdapter("SELECT * FROM PARTY", cn)
DA.Fill(DS, "TABLE")
COMBOBOX1.DATASOURCE = DS.Tables(0)
ComboBox1.DisplayMember = DS.Tables(0).Columns("cname").ToString()
ComboBox1.ValueMember = DS.Tables(0).Columns("COMPCD").ToString()
End Sub
Private Sub CALL_dATA()
''Try
If cn.State = ConnectionState.Closed Then
cn.Open()
End If
Dim dr As SqlDataReader
Dim SRCH As String
SRCH = ComboBox1.SelectedValue.ToString
''MsgBox(SRCH)
Dim cmd As New SqlCommand("select * from party where compcd = '" & SRCH & "'", cn)
dr = cmd.ExecuteReader()
dr.Read()
SlNo.Text = dr("slno")
CompCd.Text = dr("compcd")
CName.Text = dr("cname")
Add1.Text = dr("add1")
Add2.Text = dr("add2")
Add3.Text = dr("add3")
TinNo.Text = dr("tin")
Cstate.Text = dr("cstate")
SupCd.Text = dr("supcd")
Central.Text = dr("central")
ExCom.Text = dr("excom")
MailName.Text = IIf(IsDBNull(dr("mailname")), "", dr("MAILNAME"))
cn.Close()
'' Catch ex As Exception
''MsgBox(ex.Message)
''End Try
End Sub
Private Sub ComboBox1_DropDownClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.DropDownClosed
CALL_dATA()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
save_data()
MsgBox("RECORDSAVED")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Sub save_data()
If cn.State = ConnectionState.Closed Then
cn.Open()
End If
''Dim dr As
SqlDataReader()
Dim dbread()
Dim cmd1 As New SqlCommand()
cmd1 = New SqlCommand("insert into pARTY (SlNo,CompCd,CName,Add1,Add2,Add3,Tin,Cstate,SupCd,Central,ExCom) VALUES (" & SlNo.Text & ", '" & CompCd.Text & "','" & CName.Text & "', '" & Add1.Text & "', '" & Add2.Text & "', '" & Add3.Text & "', '" & TinNo.Text & "', '" & Cstate.Text & "', '" & SupCd.Text & "', '" & Central.Text & "', '" & ExCom.Text & "')", cn)
dbread = cmd1.ExecuteNonQuery()
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SlNo.Clear()
CompCd.Clear()
CName.Clear()
Add1.Clear()
Add2.Clear()
Add3.Clear()
TinNo.Clear()
Cstate.Clear()
SupCd.Clear()
Central.Clear()
ExCom.Clear()
MailName.Clear()
End Sub
End Class


Now with the help of above example you will be able to
design form for any data entry module.








No comments: