|
-
Dec 25th, 2014, 10:09 PM
#1
Thread Starter
PowerPoster
[RESOLVED] Combobox - Showing Multiple Columns in DataTable in each ComboBox Item (multi column)
I am using SQL Server 2005 and VB.NET 2005.
I am using a Stored Procedure to obtain information from two tables in SQL Server 2005 as follows:
Code:
ALTER PROCEDURE GetCOA
(
@COATypeID AS INTEGER
)
AS
BEGIN
SET NOCOUNT ON
SELECT COAT.GLAccountTypeID AS COATID,
COAT.AccountType AS AcctType,
COA.GLAccountTypeID AS COAID,
COA.AccountNumber AS AcctNum,
COA.AccountName AS AcctName
FROM GL_ChartOfAccounts AS COA
INNER JOIN GL_ChartOfAccountTypes AS COAT
ON COATID = COAID
WHERE COATID = @COATypeID
ORDER BY AcctNum
END
I want to show the Account Number on the left and then the Account Name on the right for each item.
I am not sure what would be the best way of doing it.
Function to get the data from SQL Server into a DataTable
Code:
Public Function GetCOA(ByVal AccountType As Integer) As DataTable
'1 = Asset.
'2 - Liability.
'3 - Equity.
'4 - Income/Revenue.
'5 - Expenses.
Dim dt As DataTable = New DataTable
Dim dr As SqlDataReader
Using SetDatabaseConnection As SqlConnection = New SqlConnection(ConnectToDatabase)
Using cmd As SqlCommand = New SqlCommand
Try
With cmd
.CommandText = "GetCOA"
.CommandType = CommandType.StoredProcedure
.Connection = SetDatabaseConnection
.Parameters.Add("@COATypeID", SqlDbType.Int).Value = AccountType
End With
With SetDatabaseConnection
If .State = ConnectionState.Broken _
OrElse .State = ConnectionState.Closed Then
.Open()
End If
End With
dr = cmd.ExecuteReader
With dr
If .HasRows Then
dt.Load(dr)
End If
End With
Catch RangeEx As IndexOutOfRangeException
MessageBox.Show(RangeEx.ToString(), _
"Index Out Of Range Exception", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1)
Catch CastEx As InvalidCastException
MessageBox.Show(CastEx.ToString(), _
"Invalid Cast Exception", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1)
Catch ArgNullEx As ArgumentNullException
MessageBox.Show(ArgNullEx.ToString(), _
"Argument Null Exception", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1)
Catch ArgEx As ArgumentException
MessageBox.Show(ArgEx.ToString(), _
"Argument Exception", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1)
Catch SQLEx As SqlException
MessageBox.Show(SQLEx.ToString(), _
"SQL Exception", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1)
Catch InvalidOpEx As InvalidOperationException
MessageBox.Show(InvalidOpEx.ToString(), _
"Invalid Operation Exception", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1)
Catch NotSuppEx As NotSupportedException
MessageBox.Show(NotSuppEx.ToString(), _
"Not Supported Exception", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1)
Catch NullRefEx As NullReferenceException
MessageBox.Show(NullRefEx.ToString(), _
"Null Reference Exception", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1)
Finally
With SetDatabaseConnection
If .State = ConnectionState.Open Then
.Close()
End If
End With
End Try
End Using
End Using
Return dt
End Function
Form Load:
Code:
Try
With SubAccountOfAssets
.AutoCompleteMode = AutoCompleteMode.SuggestAppend
.AutoCompleteSource = AutoCompleteSource.ListItems
.DataSource = GetCOA(1)
.DisplayMember = "AcctName"
.ValueMember = "AcctNum"
End With
Catch CastEx As InvalidCastException
MessageBox.Show(CastEx.ToString(), _
"Invalid Cast Exception", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1)
Catch OutOfMemEx As OutOfMemoryException
MessageBox.Show(OutOfMemEx.ToString(), _
"Out Of Memory Exception", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1)
Catch ArgEx As ArgumentException
MessageBox.Show(ArgEx.ToString(), _
"Argument Exception", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1)
End Try
Errors are in the screen shots.
Line 24: .ValueMember = "AcctNum"
Line 253: dr = cmd.ExecuteReader
Last edited by BrailleSchool; Dec 25th, 2014 at 10:16 PM.
Reason: Added Line Numbers.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|