Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8996115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:42:37+00:00 2026-06-15T23:42:37+00:00

Hi I am trying to learn LINQ TO SQL and am trying to map

  • 0

Hi I am trying to learn LINQ TO SQL and am trying to map the data using the POCO approach but I seem to be getting an error.Here is my code I am using the NORTHWIND database Customers table:

   //Customers class
    public class Customers
    {
        public int ID { get;set; }
        public string CompanyName { get;set; }
        public string ContactName { get;set; }
    }


       //Customers xxml file
       <?xml version="1.0" encoding="utf-8" ?>
       <Database Name="NORTHWND" xmlns="http://schemas.microsoft.com/linqtosql/mapping/2007">
           <Table Name="dbo.Customers" Member="Customers">
              <Type Name="Customers">
                 <Column Name="CustomerID" Member="ID"/>
                 <Column Name="CompanyName" Member="CompanyName"/>
                 <Column Name="ContactName" Member="ContactName"/>
              </Type>
          </Table>
      </Database>

   //And this is the code I use to linq the data:
   if(!IsPostBack)
   {
        DataContext ctx = new DataContext(ConfigurationManager.ConnectionStrings["customers"].ConnectionString , 
                                                  XmlMappingSource.FromUrl(Server.MapPath("~/Customers.xml")));

        var customers = from c in ctx.GetTable<Customers>()
                                select c;

        GridView1.DataSource = customers;
        GridView1.DataBind();

    }

When I run the code I get this error at GridView.DataBind():

   Specified cast is not valid.
   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.InvalidCastException: Specified cast is not valid.

What am I doing wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:42:38+00:00Added an answer on June 15, 2026 at 11:42 pm

    The CustomerID column in Northwind is an NVARCHAR if I remember correctly…
    So change your Customer class to:

    public string ID { get;set; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn LINQ to SQL but having a very difficult time. I
I am trying to learn LINQ to SQL. I have successfully implemented insert method
I'm trying to learn LINQ to SQL and I'm able to query the database
I'm a big noob with Linq and trying to learn, but I'm hitting a
Hei I’m trying to learn Linq to SQL and wondering if somebody can suggest
I am trying to learn how to search XML trees with LINQ using VB.net.
im trying my best to learn LINQ but im still having a hard time
I'm trying to learn how from this site http://msdn.microsoft.com/en-us/library/cc265158(v=vs.95).aspx but the code won't compile
I am new to asp.net and I am trying to learn Linq to SQL.
I am trying to learn LINQ for a project. But I am a little

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.