DataColumn.Expression Error - 'Cannot Find Column [ColumnName]'

S

SMH

Hi All,

I am currently learning .Net 2, studying for 70-528. I've hit a bit of
a brick wall with DataColumn.Expression. As I understand it, this can
be used to (For example) concatenate two columns to make a derived
column.

When I run my code, I get this error:

------------
Cannot find column [dcLastName].
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.Data.EvaluateException: Cannot find column
[dcLastName].

Source Error:

Line 75: LastNameFirstName.Expression = "dcLastName"
Line 76: LastNameFirstName.MaxLength = 70
Line 77: dtEmployee.Columns.Add(LastNameFirstName)
------------

Below is my code, hopefully someone can tell me whats going on,
because I have followed the book and it isnt working! Have spent a
good 30 minutes Googling, could not find anything useful, so perhaps
I've made a simple mistake in there some where??

Thanks for reading!

Simon.

------------
Public Function GetDataTable() As DataTable
'Create a table
Dim dtEmployee As New DataTable("Employee")

'Add columns to define our data structure
Dim dcEID As New DataColumn("Eid")
dcEID.DataType = GetType(String)
dcEID.MaxLength = 10
dcEID.Unique = True
dcEID.AllowDBNull = False
dcEID.Caption = "EID" 'Defines the heading to use for this
column when the data is used to populate a web server control
dtEmployee.Columns.Add(dcEID)

Dim dcFirstName As New DataColumn("FirstName")
dcFirstName.MaxLength = 35
dcFirstName.AllowDBNull = False
dtEmployee.Columns.Add(dcFirstName)

Dim dcLastName As New DataColumn("LastName")
dcLastName.AllowDBNull = False
dtEmployee.Columns.Add(dcLastName)

Dim DCguid As New DataColumn
DCguid.DataType = GetType(String)
Dim strGUID As Guid
DCguid.DefaultValue = strGUID

Dim salary As New DataColumn("salary", GetType(Decimal))
salary.DefaultValue = 0.0
dtEmployee.Columns.Add(salary)

'Derived column using an expression
Dim LastNameFirstName As New DataColumn("Lastname and
Firstname")
LastNameFirstName.DataType = GetType(String)
LastNameFirstName.Expression = "dcLastName + ', ' +
dcFirstName"
LastNameFirstName.MaxLength = 70
dtEmployee.Columns.Add(LastNameFirstName)

'Set the primary key for the table
dtEmployee.PrimaryKey = New DataColumn() {dcEID}

Return dtEmployee

End Function
------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top