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
}
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 have a repeater control for showing products, i want to use pagination for showing some of the products on every page using pagination, but when dataset shows data on first page and i click Next page it shows an error of set page validation=true please help me with this
public partial class WebForm1 : System.Web.UI.Page
{
Database _db = DatabaseFactory.CreateDatabase();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getdata();
}
else
{
getdata();
}
}
public void getdata()
{
DataSet ds = new DataSet();
DbCommand selectcmd = _db.GetStoredProcCommand("SHOPHIVESP");
ds = _db.ExecuteDataSet(selectcmd);
PagedDataSource Pds1 = new PagedDataSource();
Pds1.DataSource = ds.Tables[0].DefaultView;
Pds1.AllowPaging = true;
Pds1.PageSize = 30;
Pds1.CurrentPageIndex = CurrentPage;
lbl1.Text = "Showing Page: " + (CurrentPage + 1).ToString() + " of " + Pds1.PageCount.ToString();
btnPrevious.Enabled = !Pds1.IsFirstPage;
btnNext.Enabled = !Pds1.IsLastPage;
Repeater1.DataSource = Pds1;
Repeater1.DataBind();
}
public int CurrentPage
{
get
{
object s1 = this.ViewState["CurrentPage"];
if (s1 == null)
{
return 0;
}
else
{
return Convert.ToInt32(s1);
}
}
set { this.ViewState["CurrentPage"] = value; }
}
public void btnPrevious_Click(object sender, EventArgs e)
{
CurrentPage -= 1;
getdata();
}
public void btnNext_Click(object sender, EventArgs e)
{
CurrentPage += 1;
getdata();
}
}
please tell me where the problem is, it is the code in c# and just a repeater at front in asp.net help me with this,i am using a stored procedure to select name image price from DB
I am developing an application in which i have four comboBoxes i-e (comboBox1 for class, comboBox2 for group, comboBox3 for section and combBox4 for months) and a listBox. i have fetched data from database to all of these comboBoxes in "OnLoad Function".Now i want to show student names in listBox according to the comboBoxes. i-e when i select "class 7th" from class (comboBox1). It should display those students who are in selected class (class 7th), selected group (selected in comboBox2) and selected section (selected in comboBox3). Here is my code but it gives me SQL syntax exception and indexOutOfRange exception. The database attached is in mysql. "dbOperation" is the object of the class "myDatabse" in which I have connected the database and implemented all queries.
public partial class FeeVoucherPrint : Form
{
myDatabase dbOperation = new myDatabase();
DataTable table = new DataTable();
public FeeVoucherPrint()
{
InitializeComponent();
this.MaximizeBox = false;
this.MinimizeBox = false;
}
private void FeeVoucherPrint_Load(object sender, EventArgs e)
{
table = dbOperation.select("* from class");
comboBox1.DataSource = table;
comboBox1.DisplayMember = "name";
comboBox1.ValueMember = "id";
table = dbOperation.select("* from `group`");
comboBox2.DataSource = table;
comboBox2.DisplayMember = "name";
comboBox2.ValueMember = "id";
table = dbOperation.select("* from section");
comboBox3.DataSource = table;
comboBox3.DisplayMember = "name";
comboBox3.ValueMember = "id";
table = dbOperation.select("* from months");
comboBox4.DataSource = table;
comboBox4.DisplayMember = "month";
comboBox4.ValueMember = "id";
}
private void fill()
{
try
{
table = dbOperation.select("studentid from studentinclass where classid = " + comboBox1.SelectedValue + " and groupid = " + comboBox2.SelectedValue);
table = dbOperation.select("* from student where studid = " + table.Rows[0]["studentid"]);
listBox1.DataSource = table;
listBox1.DisplayMember = "name";
listBox1.ValueMember = "studid";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
fill();
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
fill();
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
fill()
}
}
Looks like you are using DataTable.Select method (dbOperation.select in this case) in a wrong way.
Gets an array of all DataRow objects that match the filter criteria.
That's why your studentid from studentinclass where part in first .Select and * from student where part in second .Select are unnecessarry. They are not filters.
Read remarks part of DataColumn.Expression property documentation.
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;