Sunday 14 October 2012

How to Insert data in data table using LINQ

How to Insert data in data table using LINQ

Answer is simple. 

First of all create a LINQ to SQLClass. For this Right Click on the project Name -> Add-> New Item->Then select Data-> then select LINQ to SQLClass and change then name of the class and press ADD Button. Here the class name is RegClass. Remember when you are changing the name the class name "DataContext" will be added with the name. For example here the Full name is RegClassDataContext. Now drag and drop the data table from server explorer to the RegClassDataContext and write the following code.

Here code is writen in a method called SaveData() which will be called where needed.(Here mstRegistration is data table)

 private void btnCalcel_Click(object sender, EventArgs e)
        {
            RegClassDataContext reg = new RegClassDataContext();
            mstRegistration obj = new mstRegistration();
            obj.Adress = txtAddress.Text;
            obj.Age = Convert.ToInt32(txtAge.Text);
            obj.ContactNo=Convert.ToDecimal(txtContactNo.Text);
            obj.AlternativeContactNo = Convert.ToDecimal(txtAlternateContactNo.Text);
            obj.Name = txtName.Text;
            obj.OPD = txtOPDReffered.Text;
            obj.Problem = txtProblem.Text;
            obj.RegistrationNo =Convert.ToDecimal( txtRegistration.Text);
            obj.RegistrationFee = Convert.ToInt32(txtRegistration.Text);
            obj.RegistrationYear = System.DateTime.Now.Year;
            reg.mstRegistrations.InsertOnSubmit(obj);
            reg.SubmitChanges();
        }

How to add data in datatable using LINQ

How to add data in datatable using LINQ in ASP.Net

Answer is very easy. First of all create a LINQ to SQLClass. For this Right Click on the project Name -> Add-> New Item->Then select Data-> then select LINQ to SQLClass and change then name of the class and press ADD Button. Here the class name is RegClass. Remember when you are changing the name the class name "DataContext" will be added with the name. For example here the Full name is RegClassDataContext. Now drag and drop the data table from server explorer to the RegClassDataContext and write the following code.

Here code is writen in a method called ShowData() which will be called where needed.(Here DatagridView1 is a grid view where data is showing and mstRegistrations is data table)

   public void ShowData()
        {
            RegClassDataContext reg = new RegClassDataContext();
            var query = from i in reg.mstRegistrations select i;
            dataGridView1.DataSource = query;
          
        }

For selecting data from multiple datatable (mstRegistrations and fee are data table):

public void ShowData()
        {
            RegClassDataContext reg = new RegClassDataContext();
            var query = from i in reg.mstRegistrations
                        from j in reg.fee select new { a, b };
            dataGridView1.DataSource = query;
          
        }