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.