What
Is LINQ:-
LINQ is a new feature available from .net version 3.0
and above. It called as LANQUAGE INTEGRATED QUERY there are different flavours
of LINQ available like LINQ TO SQL, LINQ TO XML etc.. Taking advantage of LINQ
you can query the collection as if you are querying from a database.
Basic example
var users = from userVariable in studentCollection
select userVariable.ID;
Description:-
In this example we simply Define How
to Create insert update delete in gridview using LINQ. we all know the CRUD operation
in Gridview through SQL is also easy but the Advantage of the LINQ is that you
need not a connection and the length of code of CRUD operation is very shortly
than an SQL query.
also the LINQ is language integrated query, you can
easily perform all operation and very short time than SQL.
LINQ is a new feature available from .net version 3.0 and above. It called as LANQUAGE INTEGRATED QUERY there are different flavours of LINQ available like LINQ TO SQL, LINQ TO XML etc.. Taking advantage of LINQ you can query the collection as if you are querying from a database.
Basic example
var users = from userVariable in studentCollection
select userVariable.ID;
In this example we simply Define How to Create insert update delete in gridview using LINQ. we all know the CRUD operation in Gridview through SQL is also easy but the Advantage of the LINQ is that you need not a connection and the length of code of CRUD operation is very shortly than an SQL query.
also the LINQ is language integrated query, you can easily perform all operation and very short time than SQL.
the CRUD operation Demo through SQL is also available click here CRUDinGridview
Send SMS to user Mobile in asp.net Send SMS in asp.netEnter only Alphabet in TextBox Validation in Javascript for enter only Alphabet
linqiud.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="linqiud.aspx.cs" Inherits="linqiud" %>
<!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 id="Head1" runat="server">
<link rel="stylesheet" type="text/css" href="styles.css">
<script language="javascript">
function changeImg(valueTemp) {
var a = document.getElementById("<%= img.ClientID %>");
a.src = valueTemp.value;
}
</script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="p" runat="server" BorderColor="#FFFF99" BorderStyle="Solid"
GroupingText="Registration" ScrollBars="Auto"
>
<center>
<asp:TextBox ID="txtid" runat="server" Visible="false"></asp:TextBox>
<label for="password">Company Name</label> <asp:TextBox ID="txtcnm" runat="server" CssClass="txtfield"> </asp:TextBox>
<asp:RequiredFieldValidator ID="rfvFirstName"
runat="server"
ControlToValidate="txtcnm"
ErrorMessage="company Name can't be left blank"
Display="Dynamic"> </asp:RequiredFieldValidator>
<label for="email">person_name</label>
<asp:TextBox ID="txtpnm" runat="server" TextMode="MultiLine" CssClass="txtfield"></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtpnm"
ErrorMessage="person name can't be left blank"
Display="Dynamic"> </asp:RequiredFieldValidator>
<label for="email">company phoneno</label>
<asp:TextBox ID="txtcphno" runat="server" CssClass="txtfield"
> </asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4"
runat="server"
ControlToValidate="txtcphno"
ErrorMessage="phoneno can't be left blank"
SetFocusOnError="True" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server"
ErrorMessage="only number is allowed and 6 digit number" ControlToValidate="txtcphno"
Display="Dynamic" ValidationExpression="^[0-9]{6}"></asp:RegularExpressionValidator>
<label for="email">mobile no</label>
<asp:TextBox ID="txtccellno" runat="server" CssClass="txtfield"
> </asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5"
runat="server"
ControlToValidate="txtccellno"
ErrorMessage="cellno can't be left blank"
SetFocusOnError="True" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ErrorMessage="only number is allowed" ControlToValidate="txtccellno"
Display="Dynamic" ValidationExpression="^[0-9]{10}"></asp:RegularExpressionValidator>
<label for="email">Company_address</label>
<asp:TextBox ID="txtcadd" runat="server" TextMode="MultiLine" CssClass="txtfield"></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtcadd"
ErrorMessage="Address can't be left blank"
Display="Dynamic"> </asp:RequiredFieldValidator>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<label for="email">state</label>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
CssClass="txtfield" DataTextField="state" DataValueField="state"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<label for="email">
city</label> <asp:DropDownList ID="DropDownList2" runat="server" DataTextField="city"
DataValueField="city" AutoPostBack="True" CssClass="txtfield">
</asp:DropDownList>
<br />
<table class="txtfield" >
<tr>
<td rowspan="2"><b>Gender</b></td>
<td> <asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="True"
GroupName="abc" Text="male" />
<br />
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="abc"
AutoPostBack="True" Text="female"/>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
<label for="email">choose photo</label>
<asp:FileUpload ID="FileUpload1" runat="server" onchange="changeImg(this)" CssClass="btn"/>
<asp:Image ID="img" runat="server" AlternateText="aaa" style="position:absolute; left:650px;" Width="100" Height="50" />
<br />
<asp:Button id="Button1"
Text="Register"
CssClass="btn"
runat="server" onclick="Button1_Click"/>
<asp:Button id="Button3"
Text="Update"
CssClass="btn" Visible="false"
runat="server" onclick="Button3_Click"/>
<asp:GridView ID="GridView1" runat="server"
Font-Size="Large"
ForeColor="#333333" ShowFooter="True" CellPadding="4" GridLines="None"
AutoGenerateColumns="False" DataKeyNames="cid"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text='<%# Eval("cid") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("cid") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Company_name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("cname") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("cname") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Person_name">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("pname") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("pname") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Phone_no">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("cphoneno") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Eval("cphoneno") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile_no">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("mobileno") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Eval("mobileno") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("caddress") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Eval("caddress") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="state">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Eval("state") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="city">
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Eval("city") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gender">
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Eval("gender") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("image") %>' Width="80" Height="80" />
<asp:Label ID="i1" runat="server" Visible="false" Text='<%# Eval("image") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Delete" CausesValidation="False">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit" CausesValidation="False">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</center>
</asp:Panel>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
public partial class linqiud : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
grid();
fetchstate();
city();
}
}
public void grid()
{
DataClassesDataContext dc = new DataClassesDataContext();
var sel = from t in dc.hk_companies select t;
GridView1.DataSource = sel;
GridView1.DataBind();
}
public void fetchstate()
{
string con = @"Data
Source=SQLDB;Initial Catalog=Demo;User ID=Demod;Password=Demo1@";
string se = "select
distinct state from tempstate";
SqlConnection conn = new SqlConnection(con);
SqlCommand cmd = new SqlCommand(se, conn);
DataSet ds = new DataSet();
SqlDataAdapter sa = new SqlDataAdapter();
conn.Open();
sa.SelectCommand = cmd;
sa.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.DataBind();
conn.Close();
}
public void city()
{
string con = @"Data
Source=SQLDB;Initial Catalog=Demo;User ID=Demod;Password=Demo1@";
string se = "select city
from tempstate where state ='" +
DropDownList1.SelectedValue + "'";
SqlConnection conn = new SqlConnection(con);
SqlCommand cmd = new SqlCommand(se, conn);
DataSet ds = new DataSet();
SqlDataAdapter sa = new SqlDataAdapter();
conn.Open();
sa.SelectCommand = cmd;
sa.Fill(ds);
DropDownList2.DataSource = ds;
DropDownList2.DataBind();
conn.Close();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
city();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
Label l =
(Label)GridView1.Rows[e.NewEditIndex].FindControl("Label10");
Label l1 =
(Label)GridView1.Rows[e.NewEditIndex].FindControl("Label1");
Label l2 =
(Label)GridView1.Rows[e.NewEditIndex].FindControl("Label2");
Label l3 =
(Label)GridView1.Rows[e.NewEditIndex].FindControl("Label3");
Label l4 =
(Label)GridView1.Rows[e.NewEditIndex].FindControl("Label4");
Label l5 =
(Label)GridView1.Rows[e.NewEditIndex].FindControl("Label5");
Label l6 =
(Label)GridView1.Rows[e.NewEditIndex].FindControl("Label6");
Label l7 =
(Label)GridView1.Rows[e.NewEditIndex].FindControl("Label7");
Label l8 =
(Label)GridView1.Rows[e.NewEditIndex].FindControl("Label8");
Label l10 =
(Label)GridView1.Rows[e.NewEditIndex].FindControl("Label10");
Session["id"] = l.Text;
txtid.Text = l10.Text;
txtcnm.Text = l1.Text;
txtpnm.Text = l2.Text;
txtcphno.Text = l3.Text;
txtccellno.Text = l4.Text;
txtcadd.Text = l5.Text;
DropDownList1.SelectedValue = l6.Text;
city();
DropDownList2.SelectedValue = l7.Text;
if (l8.Text.Equals("male"))
{
RadioButton1.Checked = true;
}
else
{
RadioButton2.Checked = true;
}
Button3.Visible = true;
Button1.Visible = false;
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataClassesDataContext dc = new DataClassesDataContext();
var obj = (from v in dc.hk_companies where v.cid.ToString() ==
GridView1.DataKeys[e.RowIndex].Value.ToString() select v).SingleOrDefault();
dc.hk_companies.DeleteOnSubmit(obj);
dc.SubmitChanges();
grid();
}
public void clear()
{
txtcnm.Text = "";
txtpnm.Text = "";
txtcphno.Text = "";
txtccellno.Text = "";
txtcadd.Text = "";
RadioButton1.Checked = false;
RadioButton2.Checked = false;
}
//DropDownList1.SelectedIndex
= 0;
protected void Button1_Click(object sender, EventArgs e)
{
string no = null;
if (RadioButton1.Checked)
no = "male";
else
no = "female";
hk_company h = new hk_company();
h.cname = txtcnm.Text;
h.caddress = txtcadd.Text;
h.city = DropDownList2.SelectedValue;
h.state = DropDownList1.SelectedValue;
h.cphoneno = int.Parse(txtcphno.Text);
h.mobileno = Int64.Parse(txtccellno.Text);
h.gender = no;
h.pname = txtpnm.Text;
h.image = FileUpload1.FileName;
DataClassesDataContext dc = new DataClassesDataContext();
dc.hk_companies.InsertOnSubmit(h);
dc.SubmitChanges();
grid();
clear();
}
protected void Button3_Click(object sender, EventArgs e)
{
string no = null;
if (RadioButton1.Checked)
no = "male";
else
no = "female";
DataClassesDataContext dc = new DataClassesDataContext();
var h = (from v in dc.hk_companies where v.cid.ToString() == txtid.Text select
v).SingleOrDefault();
h.cname = txtcnm.Text;
h.caddress = txtcadd.Text;
h.state = DropDownList1.SelectedValue;
city();
h.city = DropDownList2.SelectedValue;
h.cphoneno = int.Parse(txtcphno.Text);
h.mobileno = Int64.Parse(txtccellno.Text);
h.gender = no;
h.pname = txtpnm.Text;
h.image = FileUpload1.FileName;
dc.SubmitChanges();
grid();
clear();
}
}
0 comments:
Post a Comment