Friday, December 30

Screen Shot of Select,Insert,Update,Delete Using LINQ to SQL in ASP.NET

Introduction:
 hi i am going to explain about linq query operations.I have used four tables as om,namah,shivaya and Test.
Source Code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="linqDemo.aspx.cs" Inherits="linqDemo" %>

<!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>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 156px;
        }
        .style3
        {
            width: 53px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="gv1" runat="server" ></asp:GridView>
       
        <table class="style1">
            <tr>
                <td align="right">
                    om</td>
                <td class="style2">
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
                <td class="style3">
                    namah</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td align="right">
                    shivaya</td>
                <td class="style2">
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </td>
                <td class="style3">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td align="right" class="style2">
                    <asp:Button ID="btnupdate" runat="server" onclick="btnupdate_Click"
                        Text="update" />
                </td>
                <td class="style3">
                    <asp:Button ID="btninsert" runat="server" onclick="btninsert_Click"
                        Text="insert" />
                </td>
                <td>
                    <asp:Button ID="btndelete" runat="server" onclick="btndelete_Click"
                        Text="delete" />
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td class="style2">
                    &nbsp;</td>
                <td class="style3">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td class="style2">
                    &nbsp;</td>
                <td class="style3">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
       
    </div>
    <div>
    <asp:GridView ID="Gv2" runat="server" ></asp:GridView>
       
    </div>
    <div>
    <asp:GridView ID="Gv3" runat="server" ></asp:GridView>
       
    </div>
    <div>
       
    </div>
    <div>
       
    </div>
    </form>
</body>
</html>
Code Behind:

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

public partial class linqDemo : System.Web.UI.Page
{
    myDataClassesDataContext dc = new myDataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
      
        gv1.DataSource = dc.oms;
        gv1.DataBind();

        var s = from n in dc.namahs select new {n.god,n.bless,n.you };
        Gv2.DataSource = s;
        Gv2.DataBind();
        Gv3.DataSource =dc.Tests;
        Gv3.DataBind();

    }
    protected void btninsert_Click(object sender, EventArgs e)
    {
      
        try
        {
            Test obj = new Test();
            obj.hello =TextBox1.Text;
            obj.hello21 = TextBox2.Text;
            obj.hello25 = TextBox3.Text;

            dc.Tests.InsertOnSubmit(obj);
            dc.SubmitChanges();
            Gv2.DataSource = dc.Tests;
            Gv2.DataBind();
        }
        catch(Exception ex)
        {
            if (ex.Message.Contains("PRIMARY KEY"))
            {
               
                Page.RegisterStartupScript("ScriptDescription", "<script type=\"text/javascript\">alert('This data is already exist');</script>");
            }
            else

            {
                throw;
            }
        }
    }
    protected void btndelete_Click(object sender, EventArgs e)
    {
       Test obj= dc.Tests.Single(b=>b.hello==TextBox1.Text);
       dc.Tests.DeleteOnSubmit(obj);
       dc.SubmitChanges();


    }
    protected void btnupdate_Click(object sender, EventArgs e)
    {
        Test obj = dc.Tests.Single(id=>id.hello==TextBox1.Text);
        obj.hello = TextBox1.Text;
        obj.hello21 = TextBox2.Text;
        obj.hello25 = TextBox3.Text;
        dc.SubmitChanges();
    }
}


Screen Shot of Database Design With Page I Have Used:





Thanks shibashish mohanty

No comments:

Post a Comment

Please don't spam, spam comments is not allowed here.

ShibashishMnty
shibashish mohanty