December 17, 2011

Simple Visual Basic Project

Let's begin with simple Visual Basic Project (VB). To start with VB Project we need SQL Server and Microsoft Visual studio 2008 . SQL server is used to store data It act like a database various queries are processed and analysed in SQL Query Analyzer .We shall execute some simple SQL commands such as add, delete, update and search. 


Open  SQL Query Analyzer and log_in with username :sa and password sa or any default.. Now create database like:
                         create  database   student                                                        
and press Ctrl +f5 and then f5 so it will create database.
Now create some tables and add some attributes such as Name,Roll No.,address etc.

create table info(names char(50)not null,rollno int not null primary key)    
insert into info values ('tatya',25)                                                                   

Now table will be created  and  you can insert values in the table.
                     select * from info                                                                        
This will select all the tables and will executed.That's all about SQL Query Analyzer you can learn some complex queries and execute them.This is just a basic idea about SQL and How SQL works.
Now open VB Microsoft  Visual studio 2008 Click on file and New Project or just press  Ctrl +N .Under Project Types: Click on Visual Basic >>Windows. and Under Template click on Windows form Application and click ok..Now simple plan form will open now you can add button ,text box and lots of more features using Toolbox.
 Connect  SQL server to VB.
Click on View >>Server Explorer or (CTRL +ALT + S) and Right click on Data Connections and Add Connection...Now put your Server Name , Username and password, and select database Name and then Test Connection. 

Now design your forms add label and text and 4 buttons ADD,Delete,Update and Search. Now double click on add button and add code same for other button but change the names which you have taken in SQL.


SIMPLE   LOG-IN  CODE

Public Class Form1


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = "tatya" And TextBox2.Text = "tatya" Then


            Form5.Show()
            Me.Hide()
        Else
            MsgBox("invalid password", 0 + 16, "Error")
            TextBox1.Text = ""
        End If
    End Sub
End Class


START OF BASIC VB PROJECT CODE


Imports System.Data.SqlClient
Public Class Form1


CODE FOR  ADD OR INSERT


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim con As New SqlConnection
        Dim sql As String
        con.ConnectionString = "Data Source=ACER-FD5E85FC72;Initial Catalog=produc;User ID=sa;Password=sa"


        sql = "insert into Orderrss values(' " & TextBox1.Text & " ',' " & TextBox2.Text & " ',' " & TextBox3.Text & " ',' " & TextBox4.Text & " ',' " & TextBox5.Text & " ' ,' " & TextBox6.Text & " ')"


        Dim com As New SqlCommand(sql, con)
        con.Open()
        com.ExecuteNonQuery()
        MsgBox("Record Added", 0 + 64, "add record")
        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
        TextBox4.Clear()
        TextBox5.Clear()
        TextBox6.Clear()
        con.Close()
    End Sub


CODE FOR DELETE


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim con As New SqlConnection
        Dim sql As String
        con.ConnectionString = "Data Source=ACER-FD5E85FC72;Initial Catalog=produc;User ID=sa;Password=sa"
        sql = "delete Prod where pnumber= ' " & TextBox2.Text & " ' "
        Dim com As New SqlCommand(sql, con)
        con.Open()
        com.ExecuteNonQuery()
        MsgBox("Record Deleted", 0 + 16, "DELETE")
        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
        TextBox4.Clear()
        TextBox5.Clear()
        con.Close()


    End Sub

CODE FOR UPDATE


   Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim con As New SqlConnection
        Dim sql As String
        con.ConnectionString = "Data Source=ACER-FD5E85FC72;Initial Catalog=produc;User ID=sa;Password=sa"
        sql = "update Orderrss set onumber=' " & TextBox1.Text & " ',custid=' " & TextBox2.Text & " ', pnumber=' " & TextBox3.Text & " ',pquantity=' " & TextBox4.Text & " ',pcost=' " & TextBox5.Text & " ',bdate=' " & TextBox6.Text & " ' where onumber=' " & TextBox1.Text & " ' "
        Dim com As New SqlCommand(sql, con)
        con.Open()
        com.ExecuteNonQuery()
        MsgBox("Record Updated", 0 + 16, "UPDATE")
        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
        TextBox4.Clear()
        TextBox5.Clear()
        TextBox6.Clear()
        con.Close()


    End Sub


CODE FOR SEARCH


    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim con As New SqlConnection
        Dim cmd As New SqlCommand
        Dim rd As SqlDataReader
        Dim str As String
        con.ConnectionString = "Data Source=ACER-FD5E85FC72;Initial Catalog=produc;User ID=sa;Password=sa"
        str = TextBox1.Text
        con.Open()
        cmd = New SqlCommand("select *from Orderrss where onumber=' " & str & " ' ", con)
        rd = cmd.ExecuteReader()
        If rd.Read Then
            TextBox1.Text = rd("onumber").ToString
            TextBox2.Text = rd("custid").ToString
            TextBox3.Text = rd("pnumber").ToString
            TextBox4.Text = rd("pquantity").ToString
            TextBox5.Text = rd("pcost").ToString
            TextBox6.Text = rd("bdate").ToString


            MsgBox("Order Available")
        Else
            MsgBox("Order Cancelled", 0 + 16, "")
        End If
        rd.Close()
        con.Close()
    End Sub
End Class

How to get Connection string 
Go to Server explorer there will be a database which was added earlier Right click on that database and click on Properties .In sidebar the table with connection which contain connection string Now copy that whole connection string. 





Demo project1 
Demo project2


If you have any query related to vb and sql please feel free to ask and share it .   

No comments:
Write comments