Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!!

like image 214
pejman Avatar asked Jul 14 '14 08:07

pejman


1 Answers

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 
like image 132
codeworx Avatar answered Oct 31 '22 13:10

codeworx