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

Discussion in 'ASP .Net' started by SMH, Oct 14, 2007.

  1. SMH

    SMH Guest

    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
    ------------
     
    SMH, Oct 14, 2007
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    611
  2. VISVAMBHAR TRIBANG RAJAT AMOL TANMAY RASIK VISHAL

    How to format column in datacolumn

    VISVAMBHAR TRIBANG RAJAT AMOL TANMAY RASIK VISHAL, Sep 1, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    479
    VISVAMBHAR TRIBANG RAJAT AMOL TANMAY RASIK VISHAL
    Sep 1, 2006
  3. Lee Franke

    Help with DataColumn.Expression

    Lee Franke, Oct 14, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    325
    Lee Franke
    Oct 14, 2003
  4. gg

    datacolumn.expression - Minimum, maximum and average

    gg, Jan 6, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    311
    Mike McIntyre
    Jan 10, 2004
  5. steveski
    Replies:
    3
    Views:
    805
    Ken Cox [Microsoft MVP]
    Jan 26, 2005
Loading...

Share This Page