Introduction to LINQ in ASP.Net 3.5

There is a number of developer who get bore to interact with database. Off course, there were many things getting bore. Top of the list, the main reason, interacting with database, to write complex queries, closing database connections, maintaining database connection pools etc.

To get rid off from all these, Microsoft introduces Language Integrate Query names as LINQ. Using LINQ you can use SQL queries directly into your code. There is no need to maintain classes. There are many LINQ providers now, like Excel, Flicker, Google, Sharepoint, WMI etc.

However, the main purpose of LINQ provider is the code that lets you query a specific type of Data.

When you use LINQ, you will find, the code in LINQ is much similar with SQL. But main difference is the command order. Every statement starts with “from”. The reason for this is that intellisence is able to give you hints the moment you have specified the object in the from statement.

Following are the steps which help you to create first web application using asp.net along LINQ.

  1. Open VS 2008
  2. Create a new website, code behind language depends on you like VB or C#
  3. Right Click on Solution Explorer
  4. Add ASP.Net folder names App_Code
  5. After adding App_Code folder, right click to Add New Item
  6. Here, you select LINQ to SQL classes
  7. Name whatever you want, in my case I name Employee.dbml
  8. Now open Server Explorer
  9. Create a database, in my case, I create Employee along one table Employee_Table, the columns are id as int (PK), name as varchar(255), and designation varchar (255), id is auto incremented
  10. Now, click on Employee.dbml
  11. Drag new created Employee_Table, save it, and close it
  12. Right Click on Solution Explorer, Add New Item
  13. Select Web Page, in my case I name it Default.aspx
  14. Create two text boxes, one gridview, and one drop down list
  15. Now, open Default.aspx.cs
  16. Add a library “using System.Collections;”
  17. See the Code below
  18. .
  19. protected void Page_Load(object sender, EventArgs e)
  20. {
  21. if (!Page.IsPostBack)
  22. {
  23. EmployessDataContext db = new EmployessDataContext();
  24. var qry = from emp in db.Employee_Tables select emp.id;
  25. DropDownList1.DataSource = qry;
  26. DropDownList1.DataBind();
  27. this.Show();
  28. }
  29. }
  30. protected void Button1_Click(object sender, EventArgs e)
  31. {
  32. EmployessDataContext db = new EmployessDataContext();
  33. Employee_Table emp = new Employee_Table();
  34. emp.name = TextBox1.Text;
  35. emp.designation = TextBox2.Text;
  36. db.Employee_Tables.InsertOnSubmit(emp);
  37. db.SubmitChanges();
  38. TextBox2.Text = “”;
  39. TextBox1.Text = “”;
  40. this.Show();
  41. }
  42. public void Show()
  43. {
  44. EmployessDataContext db = new EmployessDataContext();
  45. var qry = from emp in db.Employee_Tables
  46. select emp;
  47. GridView1.DataSource = qry;
  48. GridView1.DataBind();
  49. }
  50. protected void Button2_Click(object sender, EventArgs e)
  51. {
  52. EmployessDataContext db = new EmployessDataContext();
  53. string id = DropDownList1.SelectedItem.ToString();
  54. Employee_Table emp_table = (from emp in db.Employee_Tables where emp.id == int.Parse(id) select emp).Single();
  55. emp_table.name = TextBox1.Text;
  56. emp_table.designation = TextBox2.Text;
  57. db.SubmitChanges();
  58. this.Show();
  59. }
  60. protected void Button3_Click(object sender, EventArgs e)
  61. {
  62. EmployessDataContext db = new EmployessDataContext();
  63. string id = DropDownList1.SelectedItem.ToString();
  64. Employee_Table emp_table = (from emp in db.Employee_Tables where emp.id == int.Parse(id) select emp).Single();
  65. db.Employee_Tables.DeleteOnSubmit(emp_table);
  66. db.SubmitChanges();
  67. this.Show();
  68. }
  69. classes will be used as given
  70. using System;
  71. using System.Configuration;
  72. using System.Data;
  73. using System.Linq;
  74. using System.Web;
  75. using System.Web.Security;
  76. using System.Web.UI;
  77. using System.Web.UI.HtmlControls;
  78. using System.Web.UI.WebControls;
  79. using System.Web.UI.WebControls.WebParts;
  80. using System.Xml.Linq;
  81. using System.Collections;

12 thoughts on “Introduction to LINQ in ASP.Net 3.5

  1. Pingback: online betting

  2. Pingback: Find great deals with bed bath and beyond bonus coupon

  3. Pingback: carl ken

  4. Pingback: Bed Bath and Beyond Store is a Woman's Dream Store

  5. Pingback: ge financial auto warranty services

  6. Hello, just attempting close to a number of information web sites, looks a pretty good technique which you are applying. Im at current utilizing Wp for several connected with the internet websites even so seeking to transform 1 among them to the web site any program similar to the one you have as a demo work. Anything particularly youll suggest regarding it?

  7. Im impressed, I must say. Really rarely do I come across a weblog thats both informative and entertaining, and let me tell you, youve hit the nail on the head. Your weblog is important; the issue is something that not sufficient folks are talking intelligently about. Im genuinely pleased that I stumbled across this in my search for some thing relating to this problem.

Leave a Reply