Wednesday, January 16, 2013

Code for Db connection and Insert,Update,Delete


About this code snippet: 

**This code snippet consist of DB connection. 

**Insert,Update,Delete are the three buttons in design window , this code is for windows application written in VB .Net.

**Again i like to mention this resource is to help very basic programmers who struggle to get the code for insert,update,delete and making connection to database , So if you are pro programmers just avoid this :)

**I have a sql server and database name is 'jas' and table name is login it contains 2 fields userid and password.

DB connection:

Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strconnection As String = "Data Source=192.168.0.6;Initial Catalog=Jas;User ID=sa;Password=*****"
dbconnection = New SqlConnection(strconnection)
dbconnection.Open()
End Sub


Insert,Update,Delete:

Imports System.Data.SqlClient
Public Class Form1
Dim dbconnection
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

End Sub

Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim str As String = "insert into login values('" + TextBox1.Text + "','" + TextBox2.Text + "')"
Dim cmd As New SqlCommand(str, dbconnection)
cmd.ExecuteNonQuery()
Dim str1 As String = "select count(*) from login where userid='" + TextBox1.Text + "'and password='" + TextBox2.Text + "'"
Dim cmd1 As New SqlCommand(str1, dbconnection)
Dim count As Integer = cmd1.ExecuteScalar
If count > 0 Then
MsgBox("Successfully inserted")
Else
MsgBox("Not successfull")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Public Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Dim str As String = "delete from login where userid='" + TextBox1.Text + "'"
Dim cmd As New SqlCommand(str, dbconnection)
cmd.ExecuteNonQuery()
Dim str1 As String = "select count(*) from login where userid='" + TextBox1.Text + "'"
Dim cmd1 As New SqlCommand(str1, dbconnection)
Dim count As Integer = cmd1.ExecuteScalar
If count = 0 Then
MsgBox("Successfully Deleted")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Try
Dim str As String = "update login set password='" + TextBox2.Text + "' where userid='" + TextBox1.Text + "'"
Dim cmd As New SqlCommand(str, dbconnection)
cmd.ExecuteNonQuery()
Dim str1 As String = "select count(*) from login where userid='" + TextBox1.Text + "'"
Dim cmd1 As New SqlCommand(str1, dbconnection)
Dim count As Integer = cmd1.ExecuteScalar
If count > 0 Then
MsgBox("Successfully updated")
Else
MsgBox("Not updated")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class


**Designing window is just of 2 text box and 3 buttons**

No comments :

Post a Comment