2/8/19

VB.NET DB Connectivity Coding Reference part - 3 | CRUD & Change Password

Previous post we have learned 
In this post, We're going to learn how to handle the crud operation and change user password concept.

This tutorial have two file.
1.StudentInfo Form
2.ChangePassword From

 Resource File Download 

StudentInfo Form (Design View):


Student From (Code View):

Imports System.Data.Sql
Imports System.Data.SqlClient

Public Class StudentInfo

    Dim sqlConn As New SqlConnection With {.ConnectionString = "server=Najathi\SQLEXPRESS;Database=Student;user=sa;pwd=pwd"}

    Dim sqlCmd As SqlCommand
    Dim sqlDA As SqlDataAdapter
    Dim sqlDataSet As DataSet

    Function hasConnection() As Boolean
        Try
            sqlConn.Open()


            Return True
        Catch ex As Exception
            MsgBox(ex.Message)
            Return False
        Finally
            sqlConn.Close()
        End Try
    End Function

    Sub runQuery(query As String)

        Try
            sqlConn.Open()

            sqlCmd = New SqlCommand(query, sqlConn)
            sqlDA = New SqlDataAdapter(sqlCmd)
            sqlDataSet = New DataSet
            sqlDA.Fill(sqlDataSet)



        Catch ex As Exception
            MsgBox(ex.Message)

            If sqlConn.State = ConnectionState.Open Then
                sqlConn.Close()
            End If

        Finally
            sqlConn.Close()
        End Try


    End Sub

    Sub insertRecord()

        Dim intAdmin, intActive As Integer

        If (Len(TextBoxUsername.Text) >= 6 And Len(TextBoxPassword.Text) >= 8) Then

            If (CheckBoxIsActive.Checked) Then intActive = 1 Else intActive = 0
            If (CheckBoxIsAdmin.Checked) Then intAdmin = 1 Else intAdmin = 0

        Else
            MsgBox("Username or Password is too short!")

        End If

        Dim insertQuery As String = "INSERT INTO Student (Name,Address,PhNo,IsAdmin,IsActive,Username,Password) VALUES ('" & TextBoxName.Text & "','" & TextBoxAddress.Text & "','" & TextBoxPhNo.Text & "'," & intAdmin & "," & intActive & ",'" & TextBoxUsername.Text & "','" & TextBoxPassword.Text & "')"

        'MsgBox(insertQuery)


        sqlConn.Open()

        sqlCmd = New SqlCommand(insertQuery, sqlConn)
        sqlCmd.ExecuteNonQuery()

    End Sub

    Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click

        Dim viewQuery As String = "SELECT * FROM Student WHERE Username='" & TextBoxUsername.Text & "'"
        runQuery(viewQuery)

        'MsgBox(view)

        If sqlDataSet.Tables(0).Rows.Count > 0 Then
            MsgBox("This Username is Already exits!")
        Else
            insertRecord()

            'Clear All
            TextBoxName.Text = ""
            TextBoxAddress.Text = ""
            TextBoxPhNo.Text = ""
            TextBoxUsername.Text = ""
            TextBoxPassword.Text = ""
            CheckBoxIsActive.Checked = True
            CheckBoxIsAdmin.Checked = False

        End If


    End Sub

    Private Sub ButtonTestConnection_Click(sender As Object, e As EventArgs) Handles ButtonTestConnection.Click

        If hasConnection() = True Then
            MsgBox("Connection was Succeed!")
        End If
    End Sub


    Private Sub ButtonViewAll_Click(sender As Object, e As EventArgs) Handles ButtonViewAll.Click

        runQuery("SELECT * FROM Student")

        If sqlDataSet.Tables(0).Rows.Count > 0 Then
            DataGridViewStudent.DataSource = sqlDataSet.Tables(0)
        End If

    End Sub

    Private Sub ButtonExit_Click(sender As Object, e As EventArgs) Handles ButtonExit.Click
        Me.Close()
    End Sub

    Private Sub ButtonClear_Click(sender As Object, e As EventArgs) Handles ButtonClear.Click

        'Clear All
        TextBoxName.Text = ""
        TextBoxAddress.Text = ""
        TextBoxPhNo.Text = ""
        TextBoxUsername.Text = ""
        TextBoxPassword.Text = ""
        CheckBoxIsActive.Checked = True
        CheckBoxIsAdmin.Checked = False

    End Sub

    Private Sub ButtonDelete_Click(sender As Object, e As EventArgs) Handles ButtonDelete.Click

        Dim deleteQuery As String = "DELETE FROM Student WHERE id=" & TextBoxID.Text
        MsgBox(deleteQuery)

        Try
            sqlConn.Open()

            If TextBoxID.Text <> "" Then
                If MsgBox("Do you really wish to delete " & TextBoxID.Text & "?", MsgBoxStyle.YesNo = MsgBoxResult.Yes) Then
                    sqlCmd = New SqlCommand(deleteQuery, sqlConn)
                    sqlCmd.ExecuteNonQuery()
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            sqlConn.Close()
        End Try




    End Sub

    Private Sub ButtonReset_Click(sender As Object, e As EventArgs) Handles ButtonReset.Click

        TextBoxName.Text = ""
        TextBoxAddress.Text = ""
        TextBoxPhNo.Text = ""
        TextBoxUsername.Text = ""
        TextBoxPassword.Text = ""
        CheckBoxIsActive.Checked = True
        CheckBoxIsAdmin.Checked = False

        TextBoxID.Visible=False


    End Sub

    Private Sub ButtonVisibility_Click(sender As Object, e As EventArgs) Handles ButtonVisibility.Click
        TextBoxID.Visible = True


    End Sub

    Public Sub updateQuery(query As String)
        Try
            sqlConn.Open()
            sqlCmd = New SqlCommand(query, sqlConn)

            Dim changeCount As Integer
            changeCount = sqlCmd.ExecuteNonQuery()


            If changeCount = 0 Then
                MsgBox("This item you wanted to update could not be found.")
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            sqlConn.Close()
        End Try



    End Sub

    Private Sub ButtonUpdate_Click(sender As Object, e As EventArgs) Handles ButtonUpdate.Click

        ChangePassword.Show()

    End Sub
End Class


ChangePassword (Design View):




ChangePassword (Code View):

Public Class ChangePassword
    Private Sub btnChangePassword_Click(sender As Object, e As EventArgs) Handles btnChangePassword.Click

        Dim Stu As New StudentInfo

        If TextBoxUsername.Text <> "" Then

            If TextBoxPassword.Text.Length >= 8 Then

                Dim updateQuery As String = "UPDATE Student SET Password='" & TextBoxPassword.Text & "' WHERE Username='" & TextBoxUsername.Text & "'"

                'MsgBox(updateQuery)

                Stu.updateQuery(updateQuery)

                MsgBox("Your Password has been successfully changed!")

            Else
                MsgBox("Password is too short. It must be at least 8 character")
            End If
        Else
            MsgBox("You must provide a user to update")
        End If



    End Sub
End Class


No comments:

Post a Comment

About

Hi, I'm Najathi.
I've started entrepreneurial company, Twin Brothers.Inc.
One is self-funded & the other is venture backed. I also send a weekly. where I share relevent, curated links.

Every Week I Publish a short post on writing, publishing, or content of IT Related

Contact Form

Name

Email *

Message *