.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> LINQ >> Post New Resource Bookmark and Share   

 Subscribe to Articles

LINQ to SQL

Posted By:Fahim       Posted Date: October 04, 2010    Points: 5    Category: LINQ    URL: http://www.dotnetspark.com  

I am going to explain how to insert a new record in Data Base using LINQ to SQL Classes in ASP.NET Web Application
 

First create a new connection in Server Explorer which points to your database.


We will now generate our data layer using LINQ To SQL. To do this you need to add a new item to the data layer project of type LINQ to SQL Classes. We will name it RMD as shown below.


After adding a LINQ to SQL Class we are presented with a designer surface. Here we can simply drag the tables which will become part of our data layer. For this article we will drag all tables on the designer by selecting them all in one go. Our designer should look like this after dragging all tables on it.



The .cs file contains the code for our data layer. Let's examine the code that has been generated for us. We will look at the generator class.

[Table(Name="dbo.Generator")]
public partial class Generator : INotifyPropertyChanging, INotifyPropertyChanged

The class itself is decorated with Table attribute and the Name property has been assigned the actual table name we have in our database. Generator class also implements INotifyPropertyChanging and INotifyPropertyChanged interfaces. These interfaces are used for databinding. Generator class also contains one property per column. Let's look at the GeneratorDescription property.

[Column(Storage="_Gen_id", AutoSync=AutoSync.OnInsert, DbType="Decimal(18,0) NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
    public decimal Gen_id
    {
        get
        {
            return this._Gen_id;
        }
        set
        {
            if ((this._Gen_id != value))
            {
                this.OnGen_idChanging(value);
                this.SendPropertyChanging();
                this._Gen_id = value;
                this.SendPropertyChanged("Gen_id");
                this.OnGen_idChanged();
            }
        }
    }

Columns are decorated with Column attribute and values are passed in for Storage, DbType and CanBeNull which indicates if the column can be null or not.

Let us now created a data entry form for Generators.  We will insert a new web form in our ASP.NET project and call it GeneratorEntry. To start with we will make sure that our form contains fields required to insert a Genertaor. Our form after completion will look like this.



We expect a new row to be inserted into Generator table when Save button is clicked. This code achieves data insertion into Generator table for us.

protected void BtnSave_Click(object sender, EventArgs e)
    {
        using (RMDDataContext context = new RMDDataContext())
        {
            Generator generator = new Generator();
           
            
              generator.GPRS = Convert.ToDecimal(TxtGPRS.Text);
              generator.Loc_id =Convert.ToInt16(DdlLocationId.SelectedValue);
              generator.Activated = CbActivated.Checked;
              generator.InstallDate = Convert.ToDateTime(TxtInstallDate.Text);
              generator.RemoveDate = Convert.ToDateTime(TxtRemoveDate.Text);
              generator.HoursInUse = Convert.ToDecimal(TxtHrsInUse.Text);
              generator.Health = Convert.ToDecimal(TxtHealth.Text);
              generator.GPS_Position = TxtGpsPositon.Text;
              generator.Comments = TxtComments.Text;

              context.Generators.InsertOnSubmit(generator);
            context.SubmitChanges();

        }
    }

In this tutorial we have not written a single SQL statement to insert data into a database. This is the beauty of LINQ To SQL.

Thank You


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend