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!!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With