stillwillyboy
Technical User
The following code is for the “Add Client Information” form. (There will also be a separate “Change...” and a separate “Delete…” form. If I can just get the Add, I think I can get the others.) Each of these forms will have three tabbed pages: “Basic Data”, “Additional Info” and “Work History”. “Additional Data” will have wage and other info. The tabbed pages will be showing the info.
Since each form has tabbed pages, how do I get the info from the DB to the “Additioanl Info” and "Work History" tabbed page? Only the info for the “Basic Data” tabbed page is appearing (i.e. Client Number, LastName, etc.) Does each tabbed page need it’s own separate connection to the DB? This somehow seems like overkill of some sort.
Also, how do I change this code to add new client (worker) info, not read current info from the DB? I want to start with the Client Number, then the Last Name, etc. when entering data on the form. Your basic data entry procedure.
Can the following code be shortened and/or improved?
Since each form has tabbed pages, how do I get the info from the DB to the “Additioanl Info” and "Work History" tabbed page? Only the info for the “Basic Data” tabbed page is appearing (i.e. Client Number, LastName, etc.) Does each tabbed page need it’s own separate connection to the DB? This somehow seems like overkill of some sort.
Also, how do I change this code to add new client (worker) info, not read current info from the DB? I want to start with the Client Number, then the Last Name, etc. when entering data on the form. Your basic data entry procedure.
Can the following code be shortened and/or improved?
Code:
Imports System.Data
Imports System.Data.SqlClient
Public Class frmAddClientInfo 'open ClientInfo form and make connection to DB
Private m_cnADONetConnection As New System.Data.SqlClient.SqlConnection()
Private m_daDataAdapter As SqlClient.SqlDataAdapter
Private m_cbCommandBuilder As SqlClient.SqlCommandBuilder
Private m_dtClients As New DataTable
Private m_rowPosition As Integer = 0 '0 is the first record in DB
Private Sub frmAddClientInfoConnectToDB_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
m_cnADONetConnection.ConnectionString = _
"Data Source=B-7M2KXC45F9FT4\SQLEXPRESS;Initial Catalog=ClientPayroll;Trusted_Connection=yes;User Id=B-7M2KXC45F9FT4\administrator"
m_cnADONetConnection.Open()
' Dim m_dadataadapter As New SqlDataAdapter
'need SQL to add a new row in table for new record
m_dadataadapter = New SqlClient.SqlDataAdapter("Select " & _
"tbl_ClntBasicInfo.ClientNumber, tbl_ClntBasicInfo.LastName, " & _
"tbl_ClntBasicInfo.FirstName, tbl_ClntBasicInfo.MiddleInitial, " & _
"tbl_ClntPayRates.Average, tbl_ClntPayRates.Fringe, " & _
"tbl_ClntClientBranch.BranchNumber " & _
"FROM (tbl_ClntBasicInfo " & _
"Inner Join tbl_ClntPayRates " & _
"ON tbl_ClntBasicInfo.ClientNumber = tbl_ClntPayRates.ClientNumber)" & _
"INNER JOIN tbl_ClntClientBranch " & _
"ON tbl_ClntBasicInfo.ClientNumber = tbl_ClntClientBranch.ClientNumber", m_cnADONetConnection)
'following line passes dataadapter to cmdbuilder
m_cbCommandBuilder = New SqlClient.SqlCommandBuilder(m_dadataadapter)
m_dadataadapter.Fill(m_dtClients) '<<info in DataTable
Me.AddNewRecord()
End Sub
'go to first empty row to add new record.
Private Sub frmAddClientInfo_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
m_cnADONetConnection.Close()
m_cnADONetConnection.Dispose()
End Sub
Private Sub AddNewRecord()
Dim m_dadataadapter As New SqlDataAdapter
Dim drNewRecord As System.Data.DataRow = m_dtClients.NewRow()
‘***conversion error msg at the following line. I know _ what 'it’s telling me, just not why it’s telling me. _
Using CInt to convert the client #.
drNewRecord("ClientNumber") = CInt(txtNewClientNumberBasicData.Text)
drNewRecord("LastName") = txtNewLastNameBasicData.Text
drNewRecord("FirstName") = txtNewFirstNameBasicData.Text
drNewRecord("MiddleInitial") = txtNewMiddleInitialBasicData
m_dtClients.Rows.Add(drNewRecord)
m_daDataAdapter.Update(m_dtClients)
m_rowPosition = m_dtClients.Rows.Count = -1
'Me.FindDesiredRecord()
Me.ShowCurrentRecord()
End Sub
Private Sub ShowCurrentRecord()
If m_dtClients.Rows.Count = 0 Then
txtnewClientNumberBasicData.Text = ""
txtNewLastNameBasicData.Text = ""
txtNewFirstNameBasicData.Text = ""
txtNewMiddleInitialBasicData.Text = ""
'the following are on the "Additional Data" tab form.
'txtAverageadditionaldata.Text = ""
'txtFringe.Text = ""
'txtLocation.Text = ""
Exit Sub
End If
txtNewClientNumberBasicData.Text = _
m_dtClients.Rows(m_rowPosition)("ClientNumber").ToString
txtNewLastNameBasicData.Text = _
m_dtClients.Rows(m_rowPosition)("LastName").ToString
txtNewFirstNameBasicData.Text = _
m_dtClients.Rows(m_rowPosition)("FirstName").ToString()
txtNewMiddleInitialBasicData.Text = _
m_dtClients.Rows(m_rowPosition)("MiddleInitial").ToString()
'the following are on the "Additional Data" tab form.
'
'txtAverage.Text = _
'm_dtClients.Rows(m_rowPosition)("Average").ToString()
'txtFringe.Text = _
'm_dtClients.Rows(m_rowPosition)("Fringe").ToString()
'txtLocation.Text = _
'm_dtClients.Rows(m_rowPosition)("BranchNumber").ToString()
End Sub
Private Sub frmConnectToSQLTest_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
m_cnADONetConnection.Close()
m_cnADONetConnection.Dispose()
End Sub
End Class