Showing posts with label LINQ. Show all posts
Showing posts with label LINQ. Show all posts

Wednesday, September 25, 2013

linq using Store Procedure

Add class……

using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;

[Table(Name="student")]
public class E_modal
{
    [Column(Name = "roll_no", IsPrimaryKey = true)]
    public int rno { get; set; }

    [Column(Name = "stud_name")]
    public string sn { get; set; }

    [Column(Name = "stud_course")]
    public string sc { get; set; }

    [Column(Name = "stud_fees")]
    public int sf { get ;set; }
      
}

.aspx page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DataContext d = new DataContext("initial catalog=batch4;data source=dev-pc;integrated security=yes");
       Table< E_modal > e1= d.GetTable< E_modal >();
       GridView1.DataSource = e1;
       GridView1.DataBind();
    }
}


 

Using stored procedure


Add class EM<ENTITY MODAL>
using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;

[Table(Name = "student")]
public class EM
{
    [Column(Name = "roll_no", IsPrimaryKey = true)]
    public int rno { get; set; }

    [Column(Name = "stud_name")]
    public string sn { get; set; }

    [Column(Name = "stud_course")]
    public string sc { get; set; }

    [Column(Name = "stud_fees")]
    public int sf { get; set; }

}

ADD CLASS DAL
using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Reflection;

public class DAL : DataContext
{
    public DAL(string x)
        : base(x)
    {

    }
    [Function(Name = "s_show")]
    public ISingleResult<EM> show()
    {
        IExecuteResult res = this.ExecuteMethodCall(this, (MethodInfo)MethodInfo.GetCurrentMethod());
        ISingleResult<EM> s = (ISingleResult<EM>)res.ReturnValue;
        return s;
    }

}


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
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DAL d = new DAL("initial catalog=batch4;data source=dev-pc;integrated security=yes");
        GridView1.DataSource = d.show();
        GridView1.DataBind();
    }
}




[3/29/2010 7:52:12 PM] sandeep karan: http://msdn.microsoft.com/en-us/library/ms998549.aspx
[3/29/2010 9:15:56 PM] sandeep karan: using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.Data.Linq;
using System.Data.Linq.Mapping;     

public class Class2:DataContext
{
 public Class2(string c):base(c)

 {
 
 }
    [Function(Name="sel_prod")]
    public ISingleResult<Class1> show()
    {
      IExecuteResult res=this.ExecuteMethodCall(this,(MethodInfo)MethodInfo.GetCurrentMethod());
      ISingleResult<Class1> s = (ISingleResult<Class1>)res.ReturnValue;
      return s;
    }
}
[3/29/2010 9:20:01 PM] sandeep karan: using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Data.Linq;
using System.Data.Linq.Mapping;

public class DAL:DataContext
{
    public DAL(string connection)
        : base(connection)
    {

    }


    [Function(Name="usp_SelectCustomer")]
    public ISingleResult<st>  getdata()
    {
       IExecuteResult obj=this.ExecuteMethodCall(this, (MethodInfo)MethodInfo.GetCurrentMethod());
       ISingleResult<st> ret=(ISingleResult<st>)obj.ReturnValue;
       return ret;
    }
    [Function(Name = "customerbyid")]
    public ISingleResult<st> getdataid([Parameter(Name = "cid", DbType = "Int")] System.Nullable<int> id)

    {
        IExecuteResult obj = this.ExecuteMethodCall(this, (MethodInfo)MethodInfo.GetCurrentMethod(),id);
        ISingleResult<st> ret = (ISingleResult<st>)obj.ReturnValue;
        return ret;
    }
   
    [Function(Name = "insertc")]
    public int InsertCustomer([Parameter(Name = "Cid", DbType = "Int")]
 int  CustId, [Parameter(Name = "Cname",DbType = "nvarchar(20)")]string custname)
    {
        IExecuteResult result = this.ExecuteMethodCall(this,
        ((MethodInfo)(MethodInfo.GetCurrentMethod())), CustId, custname);
        return (int)result.ReturnValue;
    }
}


Sunday, June 16, 2013

WCF+LINQ(Integration)

WCF+LINQ(Integration)
//BL.CS

using System;
using System.Data.Linq.Mapping;
using System.Runtime.Serialization;

[DataContract]
[Table(Name="student")]
public class BL
{
[DataMember]
[Column(Name="seat_no",IsPrimaryKey=true)]
public int sno {get;set;}

[DataMember]
[Column(Name="name")]
public string sn { get; set; }
}
//DAL.CS

using System;
using System.Data.Linq;
using System.Collections.Generic;
using System.Linq;
public class DAL
{
Table<BL> b2 = null;

public List<BL> Show_Data()
{
List<BL> st = new List<BL>(); //For manipulation
// For insert,update,delete
using (DataContext dt = new DataContext("initial catalog=cac;integrated security=yes;data source=WINDOWS-PC\\SQLEXPRESS"))
{
st = dt.GetTable<BL>().ToList();
}
return st;

}

public int save(BL b)
{
using (DataContext dt = new DataContext("initial catalog=cac;integrated security=yes;data source=WINDOWS-PC\\SQLEXPRESS"))
{
b2 = dt.GetTable<BL>(); //Call By rererence
b2.InsertOnSubmit(b);
dt.SubmitChanges();
return 1;

}

}
}

//Service.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Runtime.InteropServices;
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in code, svc and config file together.
public class Service : IService
{
public List<BL> Get_Student()
{

DAL d = new DAL();
return d.Show_Data();
}

public int Save_data(BL b)
{
DAL d2 = new DAL();
d2.save(b);
return 1;

}
}
/IService

//using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService" in both code and config file together.
[ServiceContract]
public interface IService
{
[OperationContract]
List<BL> Get_Student();

[OperationContract]
int Save_data(BL b);
}

//AT CLIENT SIDE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button2_Click(object sender, EventArgs e)
{
ServiceReference1.ServiceClient s = new ServiceReference1.ServiceClient();
GridView1.DataSource = s.Get_Student();
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
int r = Convert.ToInt32(TextBox1.Text);
string n = TextBox2.Text;

ServiceReference1.BL a = new ServiceReference1.BL();
a.sno = r;
a.sn = n;

ServiceReference1.ServiceClient s2 = new ServiceReference1.ServiceClient();
int f=s2.Save_data(a);
if (f == 1)
{
GridView1.DataSource = s2.Get_Student();
GridView1.DataBind();
}
}
}

Monday, October 15, 2012

How To Call Store Procedure with Linq in GridView

How To Call Store Procedure with Linq in GridView\

First we can add the two Class First Class name is DAL and second Class name EM(Entity framework).

ADD CLASS DAL
using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Reflection;

public class DAL : DataContext
{
    public DAL(string x)
        : base(x)
    {

    }
    [Function(Name = "s_show")]
    public ISingleResult<EM> show()
    {
        IExecuteResult res = this.ExecuteMethodCall(this, (MethodInfo)MethodInfo.GetCurrentMethod());
        ISingleResult<EM> s = (ISingleResult<EM>)res.ReturnValue;
        return s;
    }

}

Add class EM<ENTITY MODAL>
using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;

[Table(Name = "student")]
public class EM
{
    [Column(Name = "roll_no", IsPrimaryKey = true)]
    public int rno { get; set; }

    [Column(Name = "stud_name")]
    public string sn { get; set; }

    [Column(Name = "stud_course")]
    public string sc { get; set; }

    [Column(Name = "stud_fees")]
    public int sf { get; set; }

}

and after Create class we are create or developing FirstEnd Or Gridview 
define is this type


Add Coding .ASPX.CS Page

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
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DAL d = new DAL("initial catalog=batch4;data source=dev-pc;integrated security=yes");
        GridView1.DataSource = d.show();
        GridView1.DataBind();
    }
}

Add Design Page .ASPX

<%@ 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>Home || Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
        <br />
        <br />
    
    </div>
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
    </form>
</body>
</html>




Sunday, October 14, 2012

Linq by query insert update delete search

Linq by query insert update delete search
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace linqbyquery
{
public partial class Form1 : Form
{
public Form1()
{

InitializeComponent();
dc = new DataClasses1DataContext();
}
DataClasses1DataContext dc;

private void btnadd_Click(object sender, EventArgs e)
{
employee em = new employee { code = Convert.ToInt32(txtdeptt.Text), name = txtname.Text, salary = Convert.ToInt32(txtsal.Text) };
dc.employees.InsertOnSubmit(em);
dc.SubmitChanges();
MessageBox.Show("insert");
//S();
}

private void btnupdate_Click(object sender, EventArgs e)
{
var em=dc.employees.Single( c=> c.code==1);
em.name = txtname.Text;
dc.SubmitChanges();
MessageBox.Show("update");
}

private void btndal_Click(object sender, EventArgs e)
{
var d = dc.employees.Where(c => c.code == 1).First();
dc.employees.DeleteOnSubmit(d);
dc.SubmitChanges();
MessageBox.Show("delete");

}

private void btnsearch_Click(object sender, EventArgs e)
{
var x = from c in dc.employees where c.code == 1 select c;
dg1.DataSource = x;

}
//public void S()
//{
// var ans = from * in dc.employees ;
// dg1.DataSource = ans;
//}
}
}