Wednesday, July 5, 2017

example testing vb.net

-- module
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb
'Public con As New SqlConnection("Data Source=SIPPVMNAV01;Initial Catalog=zTech_Temp_Pen_20066;Integrated Security=True")

Public con As New SqlConnection("Data Source=SIPPVMNAV01;Initial Catalog=zTech_Temp_Pen_20066; User ID=sa; Password=password123$")
Public cmd As New SqlCommand
Public da As SqlDataAdapter
Public ds As DataSet
Public sqlReader As SqlClient.SqlDataReader
Public dt As New DataTable

=============================  connect or login
 Private Sub ConnectToSQL()
        Dim Password As String
        Dim userName As String
        Try
            If (con.State = ConnectionState.Open) Then
                con.Close()
            End If
            con.Open()
            cmd.Connection = con
            cmd.CommandText = " SELECT  Username, Password FROM   tbUser where Username = '" & txtUsername.Text & "' and Password = '" & txtPassword.Text & "'  "
            Dim lrd As SqlDataReader = cmd.ExecuteReader()
            If lrd.Read() Then
                userName = lrd("Username").ToString()
                Password = lrd("Password").ToString()
                If Password = txtPassword.Text And userName = txtUsername.Text Then
                    MessageBox.Show("Logged in successfully as " & userName, "", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Form2.Show()
                    Me.Hide()
                    txtPassword.Text = ""
                    txtUsername.Text = ""
                End If
            Else
                MessageBox.Show("Username and Password do not match..", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                txtPassword.Text = ""
                txtUsername.Text = ""
            End If

        Catch ex As Exception
            MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
        Finally
            con.Close() 'Whether there is error or not. Close the connection.
        End Try
    End Sub

=====================================
=========== load data into listview
'strQ = "SELECT * FROM tbCar ORDER BY [Model]"
'======>         load_data(strQ)
 Sub load_data(sqlQuery As String)
        Try
            If (con.State = ConnectionState.Open) Then
                con.Close()
            End If
            con.Open()
            cmd = New SqlClient.SqlCommand(sqlQuery, con)
            da = New SqlClient.SqlDataAdapter(cmd)
            ds = New DataSet
            da.Fill(ds, "Table")
   Dim itemcoll(100) As String
            Dim i As Integer = 0
            Dim j As Integer = 0
            ListView1.Items.Clear()
            ListView1.Columns.Clear()
            For i = 0 To ds.Tables(0).Columns.Count - 1
                Me.ListView1.Columns.Add(ds.Tables(0).Columns(i).ColumnName.ToString())
            Next

            For i = 0 To ds.Tables(0).Rows.Count - 1
                For j = 0 To ds.Tables(0).Columns.Count - 1
                    itemcoll(j) = ds.Tables(0).Rows(i)(j).ToString()
                Next
                Dim lvi As New ListViewItem(itemcoll)
                Me.ListView1.Items.Add(lvi)
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

========================
============= search
  Dim isSearch As Boolean = False
        If txtModel.Text <> "" And txtYear.Text <> "" Then
            strQ = "SELECT * FROM tbCar where Model like '%" & txtModel.Text & "%' and Year like '" & txtYear.Text & "' ORDER BY [Model]"
            isSearch = True
        ElseIf txtYear.Text <> "" And txtModel.Text = "" Then
            strQ = "SELECT * FROM tbCar where Year like '" & txtYear.Text & "' ORDER BY [Year]"
            isSearch = True
        ElseIf txtYear.Text = "" And txtModel.Text <> "" Then
            strQ = "SELECT * FROM tbCar where Model like '%" & txtModel.Text & "%' ORDER BY [Model]"
            isSearch = True
        Else
            strQ = "SELECT * FROM tbCar    ORDER BY [Model]"
            isSearch = True
        End If
        If isSearch = True Then
            load_data(strQ)       
        End If
 
==================== 
====================== code click on listview to show data in textbox
  If ListView1.SelectedItems.Count > 0 Then
            txtCode.Text = ListView1.Items(ListView1.SelectedIndices(0)).Text
            txtModel.Text = ListView1.Items(ListView1.SelectedIndices(0)).SubItems(1).Text
            txtYear.Text = ListView1.Items(ListView1.SelectedIndices(0)).SubItems(2).Text
            txtPrice.Text = ListView1.Items(ListView1.SelectedIndices(0)).SubItems(3).Text
            txtQty.Text = ListView1.Items(ListView1.SelectedIndices(0)).SubItems(4).Text
        End If
 
=========================
=============== code add to database
  Try
            If (con.State = ConnectionState.Open) Then
                con.Close()
            End If
            cmd.CommandType = System.Data.CommandType.Text
            cmd.CommandText = "Insert Into dbo.tbCar values ('" & txtModel.Text & "','" & txtYear.Text & "'," & txtPrice.Text & "," & txtQty.Text & ")"
            cmd.Connection = con
            con.Open()
            cmd.ExecuteNonQuery()
            strQ = "SELECT * FROM tbCar ORDER BY [Model]"
            load_data(strQ)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
 
============================= 
========== load combobox | combo box
 Sub load_combobox()
        Try
            If (con.State = ConnectionState.Open) Then
                con.Close()
            End If
            con.Open()
            cmd.Connection = con
            cmd.CommandText = "Select Model from tbCar"
            dr = cmd.ExecuteReader()
            Do While dr.Read = True
                cmbModel.Items.Add(dr.GetString(0).ToString())
            Loop
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

=======================================

No comments:

Post a Comment