pmn
pmn

Reputation: 2247

does not have a corresponding column in the data reader with the same name

I have Table named tbl_search with columns : id(int), title(nvarchar100), result(ntext) and i want using SQL query, like this :

    using (var db = new GoogleEntities())
      {
          const string selectCmd =
                @"Select top 1 title From tbl_search Where title=@title and id=@id ";

         var data = db.Database.SqlQuery<tbl_search>(
                selectCmd,
                new SqlParameter("@title", "wcf"),
                new SqlParameter("@id", 1)
                ).FirstOrDefault();



           if (data != null)
               {
                var serviceMember = data.ToString();
                label1.Text = serviceMember == "" ? "" : (serviceMember == "True" ? "On" : "Off");
               }
       }

but it give me an error :

The data reader is incompatible with the specified 'GoogleModel.tbl_search'. A member of the type, 'id', does not have a corresponding column in the data reader with the same name.

NOTE: this is my tbl_search class :

 public partial class tbl_search
{
    public int id { get; set; }
    public string title { get; set; }
    public string result { get; set; }
}

i have id in my table.. What is the problem!!

Upvotes: 21

Views: 51849

Answers (4)

Cryptc
Cryptc

Reputation: 3595

I did the following:

  1. Verified that the stored procedure was up-to-date (the "live" columns were updated).
  2. In EF, I removed, saved, then re-added the stored proc.
  3. Rebuilt and verified all of the types were mapped correctly (no errors).

After doing the above, the error was gone.

Upvotes: 0

Bhavya  G C
Bhavya G C

Reputation: 1

Please check the connection string in the web.config(Check if you are connected to correct domain).

In my case - my database connection string in web.config was referring to QA and I was modifying in Dev database.

Upvotes: 0

Nkululeko Mthembu
Nkululeko Mthembu

Reputation: 11

I solved this issue because I had a '@StartDate' in my last select and asp.net could not read it and returned it as 'Column1', so I have solved this by assigning @StartDate to a variable which is good practice anyway.

Upvotes: 0

codeworx
codeworx

Reputation: 2745

Your SQL Statement only returns the title not the full entity.

Change:

Select top 1 title From tbl_search Where title=@title and id=@id 

to:

Select top 1 * From tbl_search Where title=@title and id=@id 

Upvotes: 17

Related Questions