hello sir i want to display this code as entity base but execute scalar does not run in this case an error occur
i have also comment out the old code where i have tried in this case
so how they can execute
cs
public void Fill()
{
using (var ctx = new industryEntities1())
{
var studentName = ctx.tbl_licencel.SqlQuery("select max(user_id)+1 as user_id from tbl_licencel").ToList();
user_id.Text = ctx.ExecuteScalar().ToString();
}
//SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);
//con.Open();
//SqlCommand cmd = new SqlCommand("select max(user_id)+1 as user_id from tbl_licencel", con);
//user_id.Text = cmd.ExecuteScalar().ToString();
//con.Close();
}
using (var ctx = new industryEntities1())
{
int user_id = context.Database.SqlQuery<int>( "select max(user_id)+1 as user_id from tbl_licencel").FirstOrDefault();
}
Member
55 Points
191 Posts
error occur when run code execute scalar ?
Jul 14, 2020 11:44 AM|prabhjot1313|LINK
hello sir i want to display this code as entity base but execute scalar does not run in this case an error occur
i have also comment out the old code where i have tried in this case
so how they can execute
cs
public void Fill()
{
using (var ctx = new industryEntities1())
{
var studentName = ctx.tbl_licencel.SqlQuery("select max(user_id)+1 as user_id from tbl_licencel").ToList();
user_id.Text = ctx.ExecuteScalar().ToString();
}
//SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);
//con.Open();
//SqlCommand cmd = new SqlCommand("select max(user_id)+1 as user_id from tbl_licencel", con);
//user_id.Text = cmd.ExecuteScalar().ToString();
//con.Close();
}
All-Star
53691 Points
24028 Posts
Re: error occur when run code execute scalar ?
Jul 14, 2020 12:06 PM|mgebhard|LINK
The syntax is; https://www.entityframeworktutorial.net/EntityFramework4.3/raw-sql-query-in-entity-framework.aspx
The code assumes user_id is an int.
Please read the reference docs.
https://docs.microsoft.com/en-us/ef/ef6/querying/raw-sql