Friday, 18 December 2015

Example of using LINQ to SQL Database connection in Asp.Net

Example of using LINQ to SQL Database connection in Asp.Net

Step-1 :- Design Page like this



Source Code :-  
---------------------------------------

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>On Click Events</title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="Main">
        <div>
            <table style="width:60%">
                <tr>
                    <td>
                        NAME
                    </td>
                    <td>
                        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        Company
                    </td>
                    <td>
                        <asp:TextBox ID="txtCompany" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        City
                    </td>
                    <td>
                        <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                <td></td><td><asp:Button ID="btnsave" Text="save" runat="server"
                        onclick="btnsave_Click" /></td>
                </tr>
                <tr>
                <td></td><td>
                    <asp:Label ID="lblmsg" runat="server" Text="msg" Visible="false"></asp:Label></td>
                </tr>
            </table>
        </div>
        <div>
        <p>Show the Data</p>
            <table style="width:100%">
                <tr>
                    <td>
                    </td>
                    <td>
                        <asp:GridView ID="grv1" runat="server">
                        </asp:GridView>
                    </td>
                    <td>
                    </td>
                </tr>
            </table>
        </div>
        <div id="footer">
            <p style="text-align:left">
                @ Design By Dot Net By Vickypedia</p>
        </div>
    </div>
    </form>
</body>
</html>

---------------------------------------

Step-2 :- Create Table "tblEmp"

Source Code :-  
---------------------------------------

Create Table tblEmp
(
    ID int identity(1,1) primary key,
    Name varchar(50),
    Company varchar(50),
    City varchar(50),
)

---------------------------------------

Step-3 :-  Add DBML Class in Application



Step-4 :- After then drag table "tblEmp" on DBML Class





Step-5 :-then write code for the Button "btnSave" 
              and GridView  "grv1"

Source Code :-  
---------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace JavaScriprt_Examples
{
    public partial class firstPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ShowGrid();
            }
        }
        EmpDataBaseDataContext _eDBobj = new EmpDataBaseDataContext();

        protected void btnsave_Click(object sender, EventArgs e)
        {
            tblEmp tblEmpObj = new tblEmp();
            tblEmpObj.Name = txtName.Text;
            tblEmpObj.Company = txtCompany.Text;
            tblEmpObj.City = txtCity.Text;

            _eDBobj.tblEmps.InsertOnSubmit(tblEmpObj);
            _eDBobj.SubmitChanges();
            lblmsg.Visible = true;
           lblmsg.Text = "Add Successfully";
           clearform();
       
        }

        protected void clearform()
        {
            txtName.Text = "";
            txtCompany.Text = "";
            txtCity.Text = "";
        }


        protected void ShowGrid()
        {
            var v = from m in _eDBobj.tblEmps select m;
            grv1.DataSource = v;
            grv1.DataBind();
        }
    }
}
---------------------------------------

Step-6 :- Run the Application 







No comments:

Post a Comment

Factorial of a Number

Recently Viewed