Thursday, 5 July 2018

007 LINQ with ASP.NET

LINQ was introduced .Net Framework 3.5 (Visual Studio 2010).LINQ apply OOPS concept with relational database. Asp.Net is a web application , that can be used with LINQ to retrieve and modify data from database and collections. ASP.NET can be used all with  LINQ,DLINQ,XLINQ .ASP.NET already have a template called LINQ to Asp.Net website template ,that can be used for this purpose. You can use blank Asp.Net template for this purpose.In this example ,we have used a blank Asp.Net template and a local sql server database. 

The first step is to create a html design with all the required field. We have created an interface that can accept  name ,roll , age of a student and a save button and a gridview to show the data.






Below is the Asp.net code.

------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
                 <div style="width:400px;">
                 <fieldset>
                                 <legend>Student:</legend>
                   <table >
                   <tr>
                   <td>
                      Name :
                   </td>
                   <td>
                       <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                  </td>
                  </tr>
                  <tr>
                 <td>
                    Roll :
                 </td>
                 <td>
                     <asp:TextBox ID="txtRoll" runat="server"></asp:TextBox>
                 </td>
                  </tr>
                 <tr>
                 <td>
                 Age :
                </td>
                <td>
                   <asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
                </td>
               </tr>
                <tr>
                <td>
                     <asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" />
                 
               </td>
              <td>
              </td>
             </tr>
             </table>
           </fieldset>
         </div>
<br/>
List of Student
<br/>
<br/>

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None">
                   <AlternatingRowStyle BackColor="White" />
                    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                    <SortedAscendingCellStyle BackColor="#FDF5AC" />
                   <SortedAscendingHeaderStyle BackColor="#4D0000" />
                   <SortedDescendingCellStyle BackColor="#FCF6C0" />
                  <SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
</form>
</body>
</html>

--------------------View of this Code-------------------------------------



Now from the left hand side , side tab "Database Explorer" ,create database connection and add new database database ,then you will find tables.We have a table , names "student".





Now from the project,right mouse click, Add-->New Item -->LINQ to SQL class. After adding LINQ to SQL class,double click on the class and drag your database table over it. A graphical representation of SQL table will be drawn over "dbml" file. 



Now  open C Sharp  code , create an instance of a line LINQ to SQL class file and set the connection constructor with database during the instance creation. Now create a object of the student class, fill the object with the data from the input text boxes click on save.You will see ,data has been saved to the databases tables successfully.


protected void Button1_Click(object sender, EventArgs e)
{
             LINQtoSQLDataContext db = new LINQtoSQLDataContext(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\aa\tutorial.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");

            student obj = new student();
            obj.Name = this.txtName.Text.ToString();
            obj.Roll = Convert.ToInt16(this.txtRoll.Text.ToString());
            obj.Age = Convert.ToInt16(this.txtAge.Text.ToString());

             db.students.InsertOnSubmit(obj);
             db.SubmitChanges();

}
 
Now write a select query on the table and bind the data with gridview data source.

 
protected void Page_Load(object sender, EventArgs e)
{
          LINQtoSQLDataContext db = new LINQtoSQLDataContext(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\aa\tutorial.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");

             var stu = (from ord in db.students
                            select ord).ToList<student>();


                    this.GridView1.DataSource = stu;
                    this.GridView1.DataBind();
}

You will see that the data tabular format, you saved data will be shown in Grid. Now you can further enter your Name , Roll number ,age and click on save.You will see that data has been saved and it will displaying on the Gridview.

There are several technique that work with LINQ to ASP.NET , we have gone through the simplest way for user understanding and avoiding complexity to understand.





















 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{

LINQtoSQLDataContext db = new LINQtoSQLDataContext(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\inetpub\wwwroot\WebSite4\App_Data\tutorial.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");

protected void Page_Load(object sender, EventArgs e)
{

var stu = (from ord in db.students
select ord).ToList<student>();


this.GridView1.DataSource = stu;
this.GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
student obj = new student();
obj.Name = this.txtName.Text.ToString();
obj.Roll = Convert.ToInt16(this.txtRoll.Text.ToString());
obj.Age = Convert.ToInt16(this.txtAge.Text.ToString());

db.students.InsertOnSubmit(obj);
db.SubmitChanges();

}
}



fd

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...