Linq

Add to Favourites
Post to:

Binding data to Gridview using LINQ Open New C# website. Right click ( Add new items select Linq to SQL classes. In the server Explorer connect to the database. Drag the required table to the solution explorer of the dbml. Drag the Stored Procedure as well as. After drag and drop, the look and feel will be like below: In the database create one SP in the name of “sp_test1”. The SP will be like below : create procedure sp_test1 @sno int as select * from test1 where sno=@sno Add one GridView control and two buttons and name them as “btn_SP” and “btn_Query” to the Default.aspx. Add one LinqDataSource control as well. Configure the datasource for the LinqDataSource. 10. Select the dbml class for the context object. The table mapped will be listed in the table list and do the rest as shown in the picture. Now what we are going to do is : Bind the data using Linq Data Source. Bind the data using Limda Query. Bind the data using Stored Procedure. On the Page Load , binding data using Linq Data Source. protected void Page_Load(object sender, EventArgs e) { GridView1.Visible = true; if (!IsPostBack) { GridView1.DataSource = LinqDataSource1; GridView1.DataBind(); } } On the “btn_SP” click write the following code : protected void btn_SP_Click(object sender, EventArgs e) { BindGrid1(); } public void BindGrid1() { DataClassesDataContext rpt = new DataClassesDataContext(); GridView1.DataSource = rpt.sp_test1(1).ToList(); GridView1.DataBind(); } On the “btn_Query” click write the following code : protected void btn_Query_Click(object sender, EventArgs e) { BindGrid(); } public void BindGrid() { DataClassesDataContext rpt = new DataClassesDataContext(); var emp = from r in rpt.test1s where r.name != null orderby r.sno select new { NAME = (string) r.name, SNO =(int)r.sno , SEX =(string)r.sex}; //var emp = rpt.test1s.Where(r => r.name != null).ToList(); GridView1.DataSource = emp.ToList(); GridView1.DataBind(); } In this exercise ,what we have achieved are, on the page we are binding hole data from the table using linq data source to the gridview. On the SP button click, we are passing the parameter for the Stroed Procedure as (1) and the result will be binded. On the Limda query execution, Name and SNo columns are selected, where name is not null and the result is binded.

Description
Probably the most exciting new feature of LINQ is the ability to write in line SQL style queries, known as query expressions (using query syntax). This tutorial introduces the basic concepts, and comes complete with source code and demo applications

Comments

Want to learn?

Sign up and browse through relevant courses.

Name:
Your Email:
Password:
Country:
Contact no:


Area code Number
Subjects you are interested in:
Word verification: (Enter the text as in image)


Sign Up Already a member? Sign In
I agree to WizIQ's User Agreement & Privacy Policy
1 Follower

Your Facebook Friends on WizIQ

Give live classes, create & sell online courses

Try it free Plans & Pricing

Connect