private void button1_Click(object sender, EventArgs e) //save button for Form4. Note: we differentiate between insert and update operation { CompanyDBDataContext dc = new CompanyDBDataContext(); //establish connection if (textBox1.ReadOnly == false) // insert operation is happening { Employee obj = new Employee(); obj.Eno = int.Parse(textBox1.Text); obj.Ename = textBox2.Text; obj.Job = textBox3.Text; obj.Salary = decimal.Parse(textBox4.Text); obj.Dname = textBox5.Text; dc.Employees.InsertOnSubmit(obj); //pending insert dc.SubmitChanges(); //commit the data MessageBox.Show("Record inserted into the table."); } else //update operation triggered on Form3 and now new entries must be inserted in this Form4 { Employee obj = dc.Employees.SingleOrDefault(E => E.Eno == int.Parse(textBox1.Text)); //reference to existing record. Use lambda to access values obj.Ename = textBox2.Text; //user modified value is in textbox obj.Job = textBox3.Text; obj.Salary = decimal.Parse(textBox4.Text); obj.Dname = textBox5.Text; dc.SubmitChanges(); MessageBox.Show("Record updated in the table."); } { } }
private void Form5_Load(object sender, EventArgs e) { dc = new CompanyDBDataContext(); ISingleResult <Employee_SelectResult> tab = dc.Employee_Select("Department Of IT"); // u will automaqtically get the results into the tab dataGridView1.DataSource = tab; }
private void LoadData() { dc = new CompanyDBDataContext(); Table <Employee> tab = dc.Employees; dgView.DataSource = tab; }
private void Button1_Click(object sender, EventArgs e) { CompanyDBDataContext d = new CompanyDBDataContext(); if (textBox1.ReadOnly == false) // as we this method will be called while we press insert as well a update , so the difference between the insert and the update is the textBox.ReadOnly as false or true. Hence we can use it as a condition to differentiate if its a insert or update. { Employee obj = new Employee(); // to access the Employee class we created the instance //Assigning the values to the property obj.Eno = int.Parse(textBox1.Text); //here we have to explicitly convert the string value to int .int.Parse converts strings to int. obj.Ename = textBox2.Text; //it is string obj.Job = textBox3.Text; obj.Salary = decimal.Parse(textBox4.Text); // because we used type money in database so we have to convert into decimal or int obj.Dname = textBox5.Text; d.Employees.InsertOnSubmit(obj); //InsertOnSubmit(obj) inserts the record to the table but wont commit (appending k) d.SubmitChanges(); // this will commit the data changes MessageBox.Show("The Record has been inserted into the table"); } else { //Creating a refernce to an existing record Employee obj = d.Employees.SingleOrDefault(E => E.Eno == int.Parse(textBox1.Text)); //here the Eno. becomes a refernce to the existing records obj.Ename = textBox2.Text; //now if user doesnt modifies it then the old value will be overridden to the old values but if the user modifies it with a new value then the new value overrides the old value. obj.Job = textBox3.Text; obj.Salary = decimal.Parse(textBox4.Text); obj.Dname = textBox5.Text; d.SubmitChanges(); // this will commit the data changes MessageBox.Show("The Record has been Updated into the table", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
private void Form1_Load(object sender, EventArgs e) { CompanyDBDataContext dc = new CompanyDBDataContext(); Table <Employee> tab = dc.Employees; dataGridView1.DataSource = tab; //bind table to dataGridView }
private void Form1_Load(object sender, EventArgs e) { CompanyDBDataContext dc = new CompanyDBDataContext(); //dataGridView1.DataSource = dc.Employees; //remember this ,this will show all the data from the database table Employee Table <Employee> tab = dc.Employees; dataGridView1.DataSource = tab; // THE TABLE IS NOW BOUND TO GRIDVIEW }
private void Form2_Load(object sender, EventArgs e) { dc = new CompanyDBDataContext(); //create DataContext to connect to database Emps = dc.Employees.ToList(); //Create list of Employees. ToList() converts returned table to list ShowData(); }
private void Form6_Load(object sender, EventArgs e) { dc = new CompanyDBDataContext(); }
private void LoadData() { dc = new CompanyDBDataContext(); dgView.DataSource = dc.Employees; }
private void Form2_Load(object sender, EventArgs e) { dc = new CompanyDBDataContext(); //this is used to establish connection with the database emp = dc.Employees.ToList(); //once u call the property ToList() then the data of the data will be transfered to the list ShowData(); }