Wednesday, 1 November 2017

Create a Window Form in Vb.Net and Insert, Update and Delete the record in SqlServer Database


*Two Namespaces are must be required for SqlServer Connectivity:-
1. Import System.Data
2. Import System.Data.SqlClient

Insert Record in SQLServer


Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim sex As String
        If RadioButton1.Checked = True Then
            sex = "Male"
        Else
            If RadioButton2.Checked = True Then
                sex = "Female"
            End If
        End If

        Dim StrCon As String = "Data Source=PRADEEP-HP;Initial Catalog=Sample;Integrated Security=True"
        Dim SqlCon As SqlConnection = New SqlConnection(StrCon)
        Dim SqlQuery As String = "Insert into EMPTable(EmpName,EmpAddress,EmpDOB,Password,Sex) values('" + TextBoxName.Text + "','" + TextBoxAdd.Text + "','" + DateTimePicker1.Value.ToString() + "','" + TextBoxPWD.Text + "','" + sex + "')"
        Dim SqlCom As SqlCommand = New SqlCommand(SqlQuery, SqlCon)
        SqlCon.Open()
        Dim rowAffected As Integer
        rowAffected = SqlCom.ExecuteNonQuery()
        If (rowAffected > 0) Then
            lblMsg.Text = "Record inserted Successfully"
           //MessageBox.Show("Record inserted Successfully")
        Else
            lblMsg.Text = "Record are not inserted"
            //MessageBox.Show("Record are not inserted")
        End If

    End Sub

Update Record in SQLServer

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim Sex As String
        If RadioButton1.Checked = True Then
            Sex = "Male"
        Else
            Sex = "Female"
        End If
        Dim StrCon As String = "Data Source=PRADEEP-HP;Initial Catalog=Sample;Integrated Security=True"
        Dim SqlCon As SqlConnection = New SqlConnection(StrCon)
        Dim SqlQuery As String = "Update Registration set Name='" + txtName.Text + "',Address='" + txtAddress.Text + "',PhoneNo=" + txtPhNo.Text + ",DOB='" + DateTimePicker1.Value.ToString() + "',Gender='" + Sex + "' where RegId='" + txtRegId.Text + "'"
        Dim SqlCom As SqlCommand = New SqlCommand(SqlQuery, SqlCon)
        SqlCon.Open()
        Dim rowAffected As Integer
        rowAffected = SqlCom.ExecuteNonQuery()
        If (rowAffected > 0) Then
            lblMsg.Text = " Update Record Successfully"
        Else
            lblMsg.Text = "Records are not Updated"
        End If
    End Sub

Delete Record in SQLServer

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim StrCon As String = "Data Source=PRADEEP-HP;Initial Catalog=Sample;Integrated Security=True"
        Dim SqlCon As SqlConnection = New SqlConnection(StrCon)
        Dim SqlQuery As String = "Delete from Registration where RegId='" + txtRegId.Text + "'" '//Name='" + txtName.Text + "',Address='" + txtAddress.Text + "',PhoneNo=" + txtPhNo.Text + ",DOB='" + DateTimePicker1.Value.ToString() + "'
        Dim SqlCom As SqlCommand = New SqlCommand(SqlQuery, SqlCon)
        SqlCon.Open()
        Dim rowAffected As Integer
        rowAffected = SqlCom.ExecuteNonQuery()
        If (rowAffected > 0) Then
            lblMsg.Text = " Delete Record Successfully"
        Else
            lblMsg.Text = "Records are not Deleted"
        End If
    End Sub

Reset Record


Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        TextBoxName.Text = ""
        TextBoxAdd.Text = ""
        lblMsg.Text = ""
        TextBoxPWD.Text = ""

    End Sub

Next Record

Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        Dim obj2 As New Form2
        obj2.Show()
        Me.Hide()
    End Sub

https://www.youtube.com/channel/UCKLRUr6U5OFeu7FLOpQ-FSw/videos

0 comments

Post a Comment