The following is a simple example.
Add two methods to obtain the dataset
Modify idal Code
Using System;
Using System. Collections. Generic;
Using System. text;
Namespace Idal
{
Public Interface Animalsidal
{
Bool Add (model. Animals );
Ilist < Model. Animals > Getlist ();
Ilist < Model. Animals > Getlistbyid ( Int ID );
}
}
Implement the following Dal:
Code
Using System;
Using System. Collections. Generic;
Using System. text;
Using Nhib.pdf;
Namespace Dal
{
Public Class Animalsdal: idal. animalsidal
{
Isession session;
Public Animalsdal ()
{
Session = Sessionmanger. getisession ();
}
Public Bool Add (model. Animals animal)
{
Bool Flag = False ;
Itransaction tran = Session. begintransaction ();
Try
{
Session. Save (animal );
Tran. Commit ();
Flag = True ;
}
Catch (Exception ex ){
Tran. rollback ();
Throw Ex;
}
Return Flag;
}
Public Ilist < Model. Animals > Getlist ()
{
Return Session. createquery ( " From animals a order by A. animalid DESC " ). List < Model. Animals > ();
// Like usage
// Return session. createquery ("from animals a where. animaltype like: Type "). setstring ("type", "% BB % "). list <model. animals> ();
}
Public Ilist < Model. Animals > Getlistbyid ( Int ID)
{
Return Session. createquery ( " From animals a where a. animalid =: Aid " )
. Setint32 ( " Aid " , ID)
. List < Model. Animals > ();
}
}
}
Hql is used here.
Bll:
Code
Using System;
Using System. Collections. Generic;
Using System. text;
Namespace Bll
{
Public Class Animalsbll
{
Idal. animalsidal aidal;
Public Animalsbll ()
{
Aidal = Factory. dalfactory. careateanimalidal ();
}
Public Bool Add (model. Animals animal)
{
Return Aidal. Add (animal );
}
PublicIlist<Model. Animals>Getlist ()
{
ReturnAidal. getlist ();
}
PublicIlist<Model. Animals>Getlistbyid (IntID)
{
ReturnAidal. getlistbyid (ID );
}
}
}
Presentation Layer: bind the query result to the gridview
Code
Using System;
Using System. Data;
Using System. configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Using System. Web. UI. htmlcontrols;
Using Bll;
Using Model;
Public Partial Class _ Default: system. Web. UI. Page
{
Animalsbll animalbll = New Animalsbll ();
Protected Void Page_load ( Object Sender, eventargs E)
{
If ( ! Ispostback)
{
This . Gridview1.datasource = Animalbll. getlist ();
This . Gridview1.databind ();
}
}
Protected Void Ddd_click ( Object Sender, eventargs E)
{
// Animals A = new animals ();
// A. animaltype = "pig1236 ";
// Bool B = animalbll. Add ();
Int ID = Convert. toint32 ( This . Textbox1.text. Trim ());
This . Gridview1.datasource = Animalbll. getlistbyid (ID );
This . Gridview1.databind ();
}
}