You are on page 1of 3

Imports System Imports System.Data Imports System.Data.

SqlClient Public Class autosales Dim ConnStr As String = "Data Source = sefakor; Initial Catalog = autosales; Integrated Security = True;" Dim DBConn As New SqlConnection(ConnStr) Dim DBCommd As SqlCommand Dim DBAdapter As SqlDataAdapter Dim DBDataset As DataSet Dim DBDataView As DataView Dim DBCrmanager As CurrencyManager Dim bm As BindingManagerBase Private Sub autosales_Load(ByVal sender As System.Object, ByVal e As System. EventArgs) Handles MyBase.Load 'TODO: This line of code loads data into the 'AutosalesDataSet.Customers ' table. You can move, or remove it, as needed. Me.CustomersTableAdapter.Fill(Me.AutosalesDataSet.Customers) End Sub Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.Ev entArgs) Handles BtnAdd.Click 'check fields will return a value if it is blank, the ff will be skipped 'else we display the msg in a messagebox If checkfields() <> "" Then _ MessageBox.Show(checkfields) : Exit Sub Dim sql As String = "insert_customer" DBCommd = New SqlCommand(sql, DBConn) DBCommd.CommandType = CommandType.StoredProcedure DBCommd.Parameters.Add("@NAME", System.Data.SqlDbType.NVarChar).Value = txtName.Text() DBCommd.Parameters.Add("@ADDRESS", System.Data.SqlDbType.NVarChar).Value = txtAddress.Text() DBCommd.Parameters.Add("@TEL", System.Data.SqlDbType.NVarChar).Value = t xttelephone.Text() Try DBConn.Open() DBCommd.ExecuteNonQuery() MessageBox.Show("Customer added Successfully") emptyfields() Catch ex As Exception MessageBox.Show("Customer not added") Finally DBConn.Close() DBCommd = Nothing End Try End Sub Private Sub BtnUpdate_Click(ByVal sender As System.Object, ByVal e As System .EventArgs) Handles BtnUpdate.Click Dim sql As String = "update_customer"

DBCommd = New SqlCommand(sql, DBConn) DBCommd.CommandType = CommandType.StoredProcedure DBCommd.Parameters.Add("@NAME", System.Data.SqlDbType.NVarChar).Valu e = txtName.Text() DBCommd.Parameters.Add("@ADDRESS", System.Data.SqlDbType.NVarChar).V alue = txtAddress.Text() DBCommd.Parameters.Add("@TEL", System.Data.SqlDbType.NVarChar).Value = txttelephone.Text() DBCommd.Parameters.Add("@ID", System.Data.SqlDbType.Int).Value = txtCust ID.Text() Try DBConn.Open() DBCommd.ExecuteNonQuery() MessageBox.Show("Customer Updated Successfully") emptyfields() Catch ex As Exception MessageBox.Show("Customer Update failed") Finally DBConn.Close() DBCommd = Nothing End Try End Sub Private Sub emptyfields() Me.txtName.Text = "" Me.txtAddress.Text = "" Me.txtCustID.Text = "" Me.txttelephone.Text = "" End Sub Function checkfields() As String Dim msg As String = "" If txtName.Text = "" Then msg &= "Name field is empty!" & vbCrLf End If If txtAddress.Text = "" Then msg &= "Address field is empty!" & vbCrLf End If If txttelephone.Text = "" Then msg &= "Telephone field is empty!" & vbCrLf End If Return msg End Function Private Sub BtnfindCust_Click(ByVal sender As System.Object, ByVal e As Syst em.EventArgs) Handles BtnfindCust.Click Dim id As Integer Dim sql As String = "SELECT * from Customers WHERE CustID = @ID" Dim reader As SqlDataReader id = CInt(InputBox("enter customer ID")) DBCommd = New SqlCommand(sql, DBConn) DBCommd.Parameters.Add("@ID", System.Data.SqlDbType.Int).Value = id Try DBConn.Open() reader = DBCommd.ExecuteReader() If reader.Read Then Me.txtCustID.Text = reader(0) Me.txtName.Text = reader(1) Me.txtAddress.Text = reader(2) Me.txttelephone.Text = reader(3)

End If Catch ex As Exception MessageBox.Show("something bad happened") Finally DBConn.Close() DBCommd = Nothing End Try End Sub Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System .EventArgs) Handles btnDelete.Click Dim sql As String = "delete_customer" DBCommd = New SqlCommand(sql, DBConn) DBCommd.CommandType = CommandType.StoredProcedure DBCommd.Parameters.Add("@ID", System.Data.SqlDbType.Int).Value = txtCust ID.Text() Try DBConn.Open() DBCommd.ExecuteNonQuery() MessageBox.Show("Customer deleted ") emptyfields() Catch ex As Exception MessageBox.Show("Customer delete failed") Finally DBConn.Close() DBCommd = Nothing End Try

End Sub Private Sub BtnFirst_Click(ByVal sender As System.Object, ByVal e As System. EventArgs) Handles BtnFirst.Click txtCustID.DataBindings.Add("Text", DataGridView1, "Name") txtCustID.DataBindings.Add("Text", DataGridView1, "CustID") txtCustID.DataBindings.Add("Text", DataGridView1, "Address") txtCustID.DataBindings.Add("Text", DataGridView1, "Telephone")

End Sub End Class

You might also like