Trouble with creating a datatable using two different tables

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
When I create a form using only one table in a database this works fine. However when I create a form that uses at least two tables I am getting an error on my ReceiptsAdapter (or my SQLDataAdapter). Its saying that I am using the incorrect syntax and need
the keyword "From." I am assuming that I am getting this error because I am using multiple tables in my SQLCommand statement. Any suggestions on how to remedy this? Thanks
<pre>Imports System.Data
Imports System.Data.SqlClient


Public Class Receipts
Dim ReceiptsConnection As SqlConnection
Dim ReceiptsCommand As SqlCommand
Dim ReceiptsAdapter As SqlDataAdapter
Dim ReceiptsTable As DataTable
Dim ReceiptsManager As CurrencyManager
Dim MyState As String, MyBookmark As Integer


Private Sub Receipts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tabReceipts.Click


connect to database
ReceiptsConnection = New SqlConnection("Data Source=.SQLEXPRESS; AttachDbFilename=C:Program FilesCampaignToolsCamp.mdf; Integrated Security=True; Connect Timeout=30; User Instance = True")
ReceiptsConnection.Open()

establish command object
ReceiptsCommand = New SqlCommand("select from candidate_nm nm inner join candidate_rc rc on nm.nm_seq = rc.rc_nm_seq Order by nm_lname", ReceiptsConnection)

establish data adapter and data tables
ReceiptsAdapter = New SqlDataAdapter()
ReceiptsAdapter.SelectCommand = ReceiptsCommand
ReceiptsTable = New DataTable()
<span style="text-decoration:underline ReceiptsAdapter.Fill(ReceiptsTable)

bind controls to data table
txtReceiptsID.DataBindings.Add("Text", ReceiptsTable, "ReceiptsID")
cboContribType.DataBindings.Add("Text", ReceiptsTable, "Contributor Type")
txtPACInterest.DataBindings.Add("Text", ReceiptsTable, "PAC Interest")
cboContributionType.DataBindings.Add("Text", ReceiptsTable, "Contribution Type")
txtOrgname.DataBindings.Add("Text", ReceiptsTable, "Organization Name")
txtRecDate.DataBindings.Add("Text", ReceiptsTable, "Received Date")
txtPname.DataBindings.Add("Text", ReceiptsTable, "Prefix")
txtFname.DataBindings.Add("Text", ReceiptsTable, "First Name")
txtLname.DataBindings.Add("Text", ReceiptsTable, "Last Name")
txtMname.DataBindings.Add("Text", ReceiptsTable, "Middle")
txtSname.DataBindings.Add("Text", ReceiptsTable, "Suffix")
txtAddress1.DataBindings.Add("text", ReceiptsTable, "Primary Address")
txtAddress2.DataBindings.Add("text", ReceiptsTable, "Secondary Address")
txtCity.DataBindings.Add("Text", ReceiptsTable, "City")
txtState.DataBindings.Add("Text", ReceiptsTable, "State")
txtZip.DataBindings.Add("Text", ReceiptsTable, "Zip Code")
txtAmount.DataBindings.Add("Text", ReceiptsTable, "Amount")
cboMode.DataBindings.Add("Text", ReceiptsTable, "Mode")
txtInkDesc.DataBindings.Add("Text", ReceiptsTable, "Inkind Description")
txtOtherDesc.DataBindings.Add("Text", ReceiptsTable, "Other Description")
txtOccupation.DataBindings.Add("Text", ReceiptsTable, "Occupation")
txtEmployer.DataBindings.Add("Text", ReceiptsTable, "Employer")
QtyNum.DataBindings.Add("Text", ReceiptsTable, "Qty")
txtSpFname.DataBindings.Add("Text", ReceiptsTable, "First Name")
txtSpLname.DataBindings.Add("Text", ReceiptsTable, "Last Name")
txtSpPname.DataBindings.Add("Text", ReceiptsTable, "Prefix")
txtSpMidname.DataBindings.Add("Text", ReceiptsTable, "Middle")
txtSpOcc.DataBindings.Add("Text", ReceiptsTable, "Occupation")
txtSpEmployer.DataBindings.Add("Text", ReceiptsTable, "Employer")

establish currency manager
ReceiptsManager = DirectCast(Me.BindingContext(ReceiptsTable), CurrencyManager)

Me.Show()
Call SetState("View")



End Sub


Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
ReceiptsManager.Position = 0

End Sub

Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click

If ReceiptsManager.Position = 0 Then Console.Beep()
ReceiptsManager.Position -= 1

End Sub

Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
ReceiptsManager.Position += 1

End Sub

Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
ReceiptsManager.Position = ReceiptsManager.Count - 1

End Sub


Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
If Not (ValidateData()) Then Exit Sub
Dim SavedName As String = txtLname.Text
Dim SavedRow As Integer
Try
ReceiptsManager.EndCurrentEdit()
ReceiptsTable.DefaultView.Sort = "txtlName"
SavedRow = ReceiptsTable.DefaultView.Find(SavedName)
ReceiptsManager.Position = SavedRow
MessageBox.Show("Record saved.", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information)
Call SetState("View")
Catch ex As Exception
MessageBox.Show("Error saving record.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try


End Sub
Private Function ValidateData() As Boolean
Dim Message As String = ""
Dim AllOK As Boolean = True
Return (AllOK)
End Function


Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

Dim Response As Windows.Forms.DialogResult
Response = MessageBox.Show("Are you sure you want to delete this record?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
If Response = Windows.Forms.DialogResult.No Then
Exit Sub
End If
Try
ReceiptsManager.RemoveAt(ReceiptsManager.Position)
Catch ex As Exception
MessageBox.Show("Error deleting record.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub



Private Sub SetState(ByVal AppState As String)

MyState = AppState
Select Case AppState
Case "View"
txtFname.ReadOnly = True
txtPname.ReadOnly = True
txtMname.ReadOnly = True
txtLname.ReadOnly = True
txtSname.ReadOnly = True
txtOrgname.ReadOnly = True
txtAddress1.ReadOnly = True
txtAddress2.ReadOnly = True
txtCity.ReadOnly = True
txtState.ReadOnly = True
txtZip.ReadOnly = True
txtOccupation.ReadOnly = True
txtEmployer.ReadOnly = True
txtAmount.ReadOnly = True
txtInkDesc.ReadOnly = True
txtOtherDesc.ReadOnly = True
txtSpEmployer.ReadOnly = True
txtSpFname.ReadOnly = True
txtSpLname.ReadOnly = True
txtSpMidname.ReadOnly = True
txtSpOcc.ReadOnly = True
txtSpPname.ReadOnly = True
btnPrevious.Enabled = True
btnNext.Enabled = True
btnAddNew.Enabled = True
btnSave.Enabled = False
btnCancel.Enabled = False
btnEdit.Enabled = True
btnDelete.Enabled = True
cboContribType.Focus()
Case "Add", "Edit"
txtFname.ReadOnly = False
txtPname.ReadOnly = False
txtMname.ReadOnly = False
txtLname.ReadOnly = False
txtSname.ReadOnly = False
txtOrgname.ReadOnly = False
txtAddress1.ReadOnly = False
txtAddress2.ReadOnly = False
txtCity.ReadOnly = False
txtState.ReadOnly = False
txtZip.ReadOnly = False
txtOccupation.ReadOnly = False
txtEmployer.ReadOnly = False
txtAmount.ReadOnly = False
txtInkDesc.ReadOnly = False
txtOtherDesc.ReadOnly = False
txtSpEmployer.ReadOnly = False
txtSpFname.ReadOnly = False
txtSpLname.ReadOnly = False
txtSpMidname.ReadOnly = False
txtSpOcc.ReadOnly = False
txtSpPname.ReadOnly = False
btnPrevious.Enabled = False
btnNext.Enabled = False
btnAddNew.Enabled = False
btnSave.Enabled = True
btnCancel.Enabled = True
btnEdit.Enabled = False
btnDelete.Enabled = False
cboContribType.Focus()
Case "AddOrganization"
txtFname.BackColor = Color.Gray
txtFname.ReadOnly = True
txtPname.BackColor = Color.Gray
txtPname.ReadOnly = True
txtMname.BackColor = Color.Gray
txtMname.ReadOnly = True
txtLname.BackColor = Color.Gray
txtLname.ReadOnly = True
txtSname.BackColor = Color.Gray
txtSname.ReadOnly = True
txtOrgname.ReadOnly = False
txtAddress1.ReadOnly = False
txtAddress2.ReadOnly = False
txtCity.ReadOnly = False
txtState.ReadOnly = False
txtZip.ReadOnly = False
txtAmount.ReadOnly = False
txtInkDesc.ReadOnly = False
txtOtherDesc.ReadOnly = False
txtOccupation.BackColor = Color.Gray
txtOccupation.ReadOnly = True
txtEmployer.BackColor = Color.Gray
txtEmployer.ReadOnly = True
txtSpEmployer.BackColor = Color.Gray
txtSpFname.BackColor = Color.Gray
txtSpLname.BackColor = Color.Gray
txtSpMidname.BackColor = Color.Gray
txtSpOcc.BackColor = Color.Gray
txtSpPname.BackColor = Color.Gray
txtSpEmployer.ReadOnly = False
txtSpFname.ReadOnly = False
txtSpLname.ReadOnly = False
txtSpMidname.ReadOnly = False
txtSpOcc.ReadOnly = False
txtSpPname.ReadOnly = False
btnPrevious.Enabled = False
btnNext.Enabled = False
btnAddNew.Enabled = False
btnSave.Enabled = True
btnCancel.Enabled = True
btnEdit.Enabled = False
btnDelete.Enabled = False
cboContribType.Focus()
Case "AddIndividual"
txtFname.ReadOnly = False
txtPname.ReadOnly = False
txtMname.ReadOnly = False
txtLname.ReadOnly = False
txtSname.ReadOnly = False
txtOrgname.ReadOnly = True
txtOrgname.BackColor = Color.Gray
txtAddress1.ReadOnly = False
txtAddress2.ReadOnly = False
txtCity.ReadOnly = False
txtState.ReadOnly = False
txtZip.ReadOnly = False
txtOccupation.ReadOnly = False
txtEmployer.ReadOnly = False
txtAmount.ReadOnly = False
txtInkDesc.ReadOnly = False
txtOtherDesc.ReadOnly = False
txtSpEmployer.ReadOnly = False
txtSpFname.ReadOnly = False
txtSpLname.ReadOnly = False
txtSpMidname.ReadOnly = False
txtSpOcc.ReadOnly = False
txtSpPname.ReadOnly = False
btnPrevious.Enabled = False
btnNext.Enabled = False
btnAddNew.Enabled = False
btnSave.Enabled = True
btnCancel.Enabled = True
btnEdit.Enabled = False
btnDelete.Enabled = False
cboContribType.Focus()

End Select
End Sub

Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
Try
MyBookmark = ReceiptsManager.Position
ReceiptsManager.AddNew()
Call SetState("Add")
Catch ex As Exception
MessageBox.Show("Error adding record.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Call SetState("Add")

End Sub

Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
Call SetState("Edit")

End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
ReceiptsManager.CancelCurrentEdit()
If MyState = "Add" Then
ReceiptsManager.Position = MyBookmark
End If
Call SetState("View")
ReceiptsManager.CancelCurrentEdit()
Call SetState("View")
End Sub

Private Sub frmAuthors_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If MyState = "Edit" Or MyState = "Add" Then
MessageBox.Show("You must finish the current edit before stopping the application.", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
e.Cancel = True
Else
Try
save changes to database
Dim ReceiptsAdapterCommands As New SqlCommandBuilder(ReceiptsAdapter)
ReceiptsAdapter.Update(ReceiptsTable)
Catch ex As Exception
MessageBox.Show("Error saving database to file:" + ControlChars.CrLf + ex.Message, "Save Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
close the connection
ReceiptsConnection.Close()
dispose of the objects
ReceiptsConnection.Dispose()
ReceiptsCommand.Dispose()
ReceiptsAdapter.Dispose()
ReceiptsTable.Dispose()
End If



End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()

End Sub
End Class
[/code]
<hr class="sig jim neal

View the full article
 
Back
Top