I am trying to show all the data of t1 table in a gridview using Entity Framework but I am getting an error
LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression.
This is my code:
protected void Button2_Click(object sender, EventArgs e)
{
var v = (from obj in de.t1
where obj.Id == Convert.ToInt32(TextBox5.Text)
select obj).ToList();
GridView1.DataSource = v;
GridView1.DataBind();
}
Entity Framework is trying to execute Convert.ToInt32(TextBox5.Text) on the SQL side, which it obviously cannot do.
So, introduce a temporary variable to store the result of conversion:
var tempId = Convert.ToInt32(TextBox5.Text);
Then pass it to the where clause:
...
where obj.Id == tempId
...
The problem you are running into is the fact that the entity framework doesn't know how to translate Convert.ToInt32 into a valid SQL expression. Simply cast the textbox value to an integer before you query, and things should be fine.
protected void Button2_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(TextBox5.Text);
var v = (from obj in de.t1
where obj.Id == id
select obj).ToList();
GridView1.DataSource = v;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
var objId = Convert.ToInt32(TextBox5.Text);
var v = (from obj in de.t1
where obj.Id == objId
select obj).ToList();
GridView1.DataSource = v;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
var id = Convert.ToInt32(TextBox5.Text);
var v = (from obj in de.t1
where obj.Id == id
select obj).ToList();
GridView1.DataSource = v;
GridView1.DataBind();
}
Related
I'm trying to get an input as text and save it to SQL table,
the code works fine if the string isn't too long, but when you exceed from about 70 characters the SaveChanges(); command isn't able to save the data.
SQL data type is set to nvarchar(max). Any idea?
private void btnSave_Click(object sender, EventArgs e)
{
Model.TBL_USERS USR = new Model.TBL_USERS();
USR.USR_NAME = txtNAME.Text;
USR.USR_LAST = txtLAST.Text;
USR.USR_MOBILE = txtMOBILE.Text;
USR.USR_TEL1 = txtTEL1.Text ;
USR.USR_ADDRESS = txtADDRESS.Text;
USR.USR_COMPANY = txtCOMPANY.Text;
//USR.USR_COMPANY = "=chrome..69i57.13150j0j4&sourceid=chrome&ie=UTF-8";
DB.TBL_USERS.Add(USR);
DB.SaveChanges();
dataGridView1.DataSource = "";
dataGridView1.DataSource = DB.TBL_USERS.ToList();
}
this is the whole code in the solution :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Model.SAMA_User_CRM_V1 DB = new Model.SAMA_User_CRM_V1();
int IDGRID;
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = DB.TBL_USERS.ToList();
}
private void btnSave_Click(object sender, EventArgs e)
{
Model.TBL_USERS USR = new Model.TBL_USERS();
USR.USR_NAME = txtNAME.Text;
USR.USR_LAST = txtLAST.Text;
USR.USR_MOBILE = txtMOBILE.Text;
USR.USR_TEL1 = txtTEL1.Text ;
USR.USR_ADDRESS = txtADDRESS.Text;
USR.USR_COMPANY = txtCOMPANY.Text;
//USR.USR_COMPANY = "=chrome..69i57.13150j0j4&sourceid=chrome&ie=UTF-8";
DB.TBL_USERS.Add(USR);
DB.SaveChanges();
dataGridView1.DataSource = "";
dataGridView1.DataSource = DB.TBL_USERS.ToList();
}
private void btnDELETE_Click(object sender, EventArgs e)
{
var qdel = DB.TBL_USERS.Where(x => x.ID == IDGRID).ToList();
foreach (var item in qdel)
{
DB.TBL_USERS.Remove(item);
}
DB.SaveChanges();
dataGridView1.DataSource = "";
dataGridView1.DataSource = DB.TBL_USERS.ToList();
}
private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
{
IDGRID = (int)dataGridView1.Rows[e.RowIndex].Cells["ID"].Value;
}
}
}
considering the comments i found out where the problem is :
initially the model was created with a SQL table that used nvarchar(50) and after changing that to nvarchar(500) or nvarchar(max) the model.edmx needed to be updated ... after updating the model all things worked like a charm .
I am trying to show data from DataTable into GridView using a LINQ query but I am not able to understand that why this code is not working. Actually GridView shows RowError HasError message. I am totally confused.
Here's my code
protected void Page_Load(object sender, EventArgs e)
{
DataTable table = (DataTable)Session["userTable"];
string compare = Request.QueryString["id"].ToString();
var data = from x in table.AsEnumerable()
where x.Field<string>("Userid") == compare
select x;
GridView1.DataSource = data;
GridView1.DataBind();
}
Your query returns IEnumerable<DataRow> to convert it to Datatble use CopyToDataTable() extension. MSDN
protected void Page_Load(object sender, EventArgs e)
{
DataTable table = (DataTable)Session["userTable"];
string compare = Request.QueryString["id"].ToString();
IEnumerable<DataRow> data = from x in table.AsEnumerable()
where x.Field<string>("Userid") == compare
select x;
DataTable boundTable = data.CopyToDataTable<DataRow>();
GridView1.DataSource = boundTable;
GridView1.DataBind();
}
Try this,
var data = from x in table.AsEnumerable()
where x.Field<string>("UserId").ToUpper().ToString().Equals(compare.ToUpper().ToString())
select x;
DataTable boundTable = data.AsDataView().ToTable();
return boundTable;
I just learn linq and trying to make insert update delete with linq and ado.net data entity model. Now I'm have some problem, when I'm insert some data, the datagridview not show that, I must close the program and run it again to show the latest data that I'm entry. Here the code :
namespace TesDB
{
public partial class Form1 : Form
{
private Database1Entities de = new Database1Entities();
int index;
public Form1()
{
InitializeComponent();
var query = from x in de.MsEmployees
select x;
dataGridView1.DataSource = query;
dataGridView1.Refresh();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void loadtext()
{
textBox1.Text = dataGridView1.Rows[index].Cells[0].Value.ToString();
textBox2.Text = dataGridView1.Rows[index].Cells[1].Value.ToString();
textBox3.Text = dataGridView1.Rows[index].Cells[2].Value.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
string EmployeeID = dataGridView1.Rows[index].Cells[0].Value.ToString();
var employee = (from x in de.MsEmployees
where x.EmployeeID == EmployeeID
select x).First();
employee.EmployeeID = textBox1.Text;
employee.EmployeeName = textBox2.Text;
employee.Salary = int.Parse(textBox3.Text);
de.SaveChanges();
Refresh();
}
private void button3_Click(object sender, EventArgs e)
{
string EmployeeID = dataGridView1.Rows[index].Cells[0].Value.ToString();
// int id1 = int.Parse(EmployeeID);
var Employee = (from x in de.MsEmployees
where x.EmployeeID == EmployeeID
select x).First();
de.MsEmployees.DeleteObject(Employee);
de.SaveChanges();
Refresh();
}
private void button1_Click(object sender, EventArgs e)
{
string employeeID = dataGridView1.Rows[index].Cells[0].Value.ToString();
var employee = (from x in de.MsEmployees
where x.EmployeeID == employeeID
select x.EmployeeID).First();
MsEmployee me = new MsEmployee();
me.EmployeeID = textBox1.Text;
me.EmployeeName = textBox2.Text;
me.Salary = int.Parse(textBox3.Text);
de.AddToMsEmployees(me);
de.SaveChanges();
this.Refresh();
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
index = e.RowIndex;
loadtext();
}
}
}
`
I'm using refresh() that work for Update and Delete but it not work for insert does anyone know how to refresh that table when I'm done insert?
make a method of refresh() and paste ur Query inside and call it behind the button
public void refresh()
{
//Paste ur Query here and call it behind the button_click Event
}
I am using 2 gridview in my project, i binded the employee on one grid and i want to show the orders of each employee on the second. i am stuck at filtering the other employees, when i hit the show orders button it shows me all the orders, how can i point at the right index(the employee selected)? .
My code:
private void Form3_Load(object sender, EventArgs e)
{
using (NorthWindDataContext db = new NorthWindDataContext())
{
var query =
from d in db.Employees
select new
{
d.FirstName
};
dataGridView1.DataSource = query;
}
}
private void displayOrdersToolStripMenuItem_Click(object sender, EventArgs e)
{
using (NorthWindDataContext db = new NorthWindDataContext())
{
var query =
from o in db.Orders
select new
{
o.ShipName <----problem here :(
};
dataGridView2.DataSource = query;
}
}
Please help
Thanks
Use GridView.SelectedValue Property.
You need to write something similar to:
private void displayOrdersToolStripMenuItem_Click(object sender, EventArgs e)
{
using (NorthWindDataContext db = new NorthWindDataContext())
{
var query =
from o in db.Orders.
Where(item => item.ID == dataGridView1.SelectedValue)
select new
{
o.ShipName <----problem here :(
};
dataGridView2.DataSource = query;
}
}
Using Linq to sql through bindingsource control in WinForms, I could not get this to work:
private void textBox1_TextChanged(object sender, EventArgs e)
{
productBindingSource.Filter = string.Format("ProductName LIKE '*{0}*'", textBox1.Text);
MessageBox.Show("Changed");
}
NorthwindDataContext dc;
private void FrmFilter_Load(object sender, EventArgs e)
{
// create new data context
dc = new NorthwindDataContext();
// set the binding source data source to the full order table
var qry = (from p in dc.Products select p).ToList();
this.productBindingSource.DataSource = dc.GetTable<Product>();
}
When I type some letter in the textbox nothing happens in the datagridview.
Thanks for advices ...
Try changing your code to look like this:
NorthwindDataContext dc;
private void FrmFilter_Load(object sender, EventArgs e)
{
dc = new NorthwindDataContext();
this.productBindingSource.DataSource = dc.GetTable<Product>();
productDataGridView.DataSource = productBindingSource;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
this.productBindingSource.Filter = string.Format("ProductName LIKE '*{0}*'",
textBox1.Text);
}
Make sure your TextChanged event is wired up and actually running. Also, I took qry out of the example since you weren't using it anywhere in the posted code.
older edits:
You shouldn't have to reset the DataSource on the grid.
Try changing it to this:
private void textBox1_TextChanged(object sender, EventArgs e) {
if (textBox1.Text == string.Empty) {
productBindingSource.RemoveFilter();
} else {
productBindingSource.Filter = string.Format("ProductName LIKE '*{0}*'", _
textBox1.Text);
}
}
I would avoid worrying about replacing those special characters at the moment. Get the filter working first.
Here is a working example with just a DataGridView and a TextBox on a form:
private DataTable dt = new DataTable("Test");
private BindingSource bs;
public Form1() {
InitializeComponent();
dt.Columns.Add("ProductName", typeof(string));
DataRow dr1 = dt.NewRow();
dr1["ProductName"] = "One A";
dt.Rows.Add(dr1);
DataRow dr2 = dt.NewRow();
dr2["ProductName"] = "One B";
dt.Rows.Add(dr2);
DataRow dr3 = dt.NewRow();
dr3["ProductName"] = "Two A";
dt.Rows.Add(dr3);
DataRow dr4 = dt.NewRow();
dr4["ProductName"] = "Two B";
dt.Rows.Add(dr4);
bs = new BindingSource(dt, null);
dataGridView1.DataSource = bs;
}
private void textBox1_TextChanged(object sender, EventArgs e) {
if (textBox1.Text == string.Empty) {
bs.RemoveFilter();
} else {
bs.Filter = string.Format("ProductName LIKE '*{0}*'", textBox1.Text);
}
}
this work just fine for me!
this.productBindingSource.Filter = null;