Tuesday, August 31, 2010

Insertion Using Entity Model



This is the Entity data Model NationalBankEntities3 in the program.
We are going to add Items into the table EmployeeDetail

Now we must write the code like below

NationalBankModel3.NationalBankEntities3 m = new NationalBankModel3.NationalBankEntities3();
NationalBankModel3.EmployeeDetail Emp = new NationalBankModel3.EmployeeDetail() { EmpName = "Smilu", EmpAddress = "EKM", EmpPhNo = "987654", EmpDob =Convert.ToDateTime("01/01/01") };

m.AddToEmployeeDetails(Emp);
m.SaveChanges();

Here AddToEmployeeDetails will be the method which will insert the data of the Entity we send.
SaveChages() will store the data into the table

Tuesday, August 10, 2010

Linq to XML

Here we are just going to see how we can associate XML with LinQ.

First create an XML file with the structure like this CLICK HERE

Now add a GridView to your design.

We need

System.Xml.Linq;

namespace for working of XLINQ

Now in the page load event add the code like this..
------------------------------------------------------
var query = from s in XElement.Load(MapPath("Students.xml")).Elements("Student1")
select new
{
Name = (string)s.Element("StName"),
Address = (string)s.Element("StAddress"),
PhNo = (string)s.Element("StPHNO"),
flag = (int)s.Element("Flag")
};

GridView1.DataSource=query;
GridView1.DataBind();
------------------------------------------------------
In the above code we are using XElement.Load method to Load the Students.xml file and we must mention the Node inside which we have put the informations.

(string)s.Element("StName")
The above method converts the data inside the Node to the type we have specified and stores into it....
When we execute it we will get a GridView which contains columns Name, Address, PhNo, and Flag..


Students.XML File

<Students>
<Student>
<StID>S001</StID>
<StName>Simi</StName>
<StAddress>EKM</StAddress>
<StPHNO>987654123</StPHNO>
<Flag>1</Flag>
</Student>
<Student>
<StID>S002</StID>
<StName>Ravi</StName>
<StAddress>EKM</StAddress>
<StPHNO>654987</StPHNO>
<Flag>0</Flag>
</Student>
<Student>
<StID>S003</StID>
<StName>Manu</StName>
<StAddress>TVM</StAddress>
<StPHNO>987654123</StPHNO>
<Flag>1</Flag>
</Student>
<Student>
<StID>S004</StID>
<StName>Sooraj</StName>
<StAddress>EKM</StAddress>
<StPHNO>64568</StPHNO>
<Flag>0</Flag>
</Student>
<Student>
<StID>S005</StID>
<StName>Remya</StName>
<StAddress>EKM</StAddress>
<StPHNO>987654123</StPHNO>
<Flag>1</Flag>
</Student>
</Students>

Monday, August 9, 2010

JOIN - Linq to Objects

We have two List<> methods namely GetStudents() which get us the information about the Students and GetFlagNames() which give us information about Flag.

public List GetStudents()
{
return new List
{
new Student{Name="Smilu",Address="EKM",PhNo="987654",flag=1},
new Student{Name="JK", Address="Parur",PhNo="897643",flag=0},
new Student{Name="Kiran", Address="EKM",PhNo="798654",flag=1}
};
}

public List GetFlagNames()
{
return new List
{
new FlagName{flagID=0,flagName="CS"},
new FlagName{flagID=1,flagName="Network"}
};
}
---------------------------------------------------
We store the list object into a variable called "mystudents" and "flags" respectively...
var mystudents = GetStudents();
var flags = GetFlagNames();

This query represents the Joining using LinQ
var query = from s in mystudents
join f in flags on s.flag equals f.flagID
select new { s.Name, s.PhNo, f.flagName };


Sunday, August 8, 2010

Linq to Objects - Arithmetic Operations

Linq to objects can have arithmetic operations like

  • Min
  • Max
  • Average
  • Sum
  • Count etc....
This is the List collection we are going to use....

public List GetStudents()
{
return new List
{
new Student{Name="Smilu",Address="EKM",PhNo="987654",flag=1},
new Student{Name="JK", Address="Parur",PhNo="897643",flag=0},
new Student{Name="Binu", Address="EKM",PhNo="798654",flag=1}
};
}

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

For using this we need to first create a var variable and assign the list into it...

var StudentInfo=GetStudents();

LabelMaxValue.Text = st.Max(s => s.flag).ToString();
LabelMin.Text=st.Min(s=>s.flag).ToString();
LabelCount.Text = st.Count.ToString();
LabelSum.Text = st.Sum(s => s.flag).ToString();
LabelAverage.Text = st.Average(s => s.flag).ToString();

Here you can see like s=> s.flag. This is a method showing the specific field we need to work on.



Wednesday, August 4, 2010

How to Change Text of a Label inside a MasterPage from its inherited Page.

((Label)this.Master.FindControl("Label1")).Text = "Text to be changed";

This code will find the control inside the Masterpage and Change it.

Wednesday, July 28, 2010

ADO.NET Entity Framework

Create a EDM by adding a new ADO.NET Entity Data Model. Add your Database to it...

Some terms used.....
EDM - Entity Data Model (Mapping Layer)
CSDL - Conceptual Schema Definition Language (Conceptual Layer)
SSDL - Stored Schema Definition Language (Logical Layer)

Extension .edmx

Codes after creating the EDM file....

NationalBankModel.NationalBankEntities mm = new NationalBankModel.NationalBankEntities();
//var query = from n in mm.EmployeeDetails
// select n;


//var query = from n in mm.EmployeeDetails
// where mm.Logins.Any(Emp => Emp.EmpID >3)
// select new {n.EmpID, n.EmpName, n.EmpAddress, n.EmpPhNo };

var query = from n in mm.EmployeeDetails
//join oo in mm.Logins on n.EmpID equals oo.EmpID
where mm.Logins.Any(Emp => Emp.EmpID == 3)
//where n.EmpID > 2
select new { n.EmpID, n.EmpName, n.EmpAddress, n.EmpPhNo };

GridView1.DataSource = query;
GridView1.DataBind();

foreach (var emp in mm.EmployeeDetails)
{
ListItem li = new ListItem();
li.Text = emp.EmpID + " ";
if (!emp.Logins.IsLoaded)
{
emp.Logins.Load();
}

foreach (var p in emp.Logins)
{
li.Text += "Login Name: " + p.username + " ";
}

BulletedList1.Items.Add(li);
}

Monday, July 26, 2010

Custom Reporting using Crystalreports

Namespaces to use.

using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;

Code....
SqlConnection con=new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=NationalBank;Integrated Security=True");
SqlDataAdapter da=new SqlDataAdapter("select * from EmployeeDetails where EMpname like 'g%'",con);

DataSet ds = new DataSet();
da.Fill(ds, "d");


ReportDocument rpt = new ReportDocument();
rpt.Load(Server.MapPath(@"~\CrystalReport.rpt"));
rpt.SetDataSource(ds.Tables["d"]);
CrystalReportViewer1.ReportSource = rpt;

Thursday, July 15, 2010

Taking index of a row in Gridview using RowCommand

For taking data using rowcommand property....

When you create the code of Gridview place the code like this for your customized Controls(Button)


<asp:imagebutton id="imgProfile" width="100px" height="100px" runat="server" imageurl="'<%#">' CommandName="cmdImage" CommandArgument='<%# Container.DataItemIndex %>' />
Here look at the COmmand argument property it should be like this. Then only we can take the Index of the Gridview.

Now, in Code behind you can call it like this....

string name = ((TextBox)GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("TextBoxName")).Text;

thus you can work on it getting the index of the row.

Fetch data from Footer of Gridview

Taking data from the Footer of a Gridview control in asp.net

GridViewRow row = GridView1.FooterRow;
if (e.CommandName == "cmdSUBMIT")
{
string name = ((TextBox)row.FindControl("TextBoxName")).Text;
Response.Write(name);
}