Você está na página 1de 19

window application in 3 tier Architecture

How to make project in .net 3 tier architecture Step1 > Make a blank solution by New?project?Other Project Type?Visual Studio Solution?Blank Solution. Then provide eg, Name :- test Location:-d: Step2> Go to the location you will found that there is a folder test and when you double click on it you Will find test solution. Then add two new folder function and application Function:- folder will contain class library and .dll Application;- folder will contain web form or window form and so on.. Step3> Go to studio Solution Explorer and add three class library Solution(R-Click)?Add?New Project Language?c# Templates?Class Library Location?d:\test\function Name of class library are BOclass, BALclass, DALclass Step4> Go to studio Solution Explorer and add window form Solution(R-Click)?Add?New Project Language?c# Templates?form Location?d:\test\application Name?testform Step5>Build each project BOclass,BALclass,DALclass,testform Step6>BALclass(R-Click)?Addrefference?Project {BOclass,DALclass} Step7>DALclass(R-Click)?Addrefference?Project {BOclass,DALclass} Step8>testform(R-Click)?Addrefference?Project {BOclass,BALclass,DALclass} Step9>Build Solution Step10>Add app.config for database access now there is code for boclass,balclass,dalclass,windowform App.config

BOclass using using using using System; System.Collections.Generic; System.Linq; System.Text;

namespace BOclass { public class boaddclient { public string c_name { get; set; } public string c_add { get; set; } public string c_mobile { get; set; } } } DALclass using using using using using using using using System; System.Collections.Generic; System.Linq; System.Text; System.Configuration; System.Data; System.Data.OleDb; BOclass;

namespace DALclass { public class daladdclient { public int dalclient(boaddclient boinfo) { string str = ConfigurationSettings.AppSettings["mycon"]; OleDbConnection con = new OleDbConnection(str); string qry = "insert into client(c_name,c_address,c_mobileno) values('" + boinfo.c_name + "','" + boinfo.c_add + "','" + boinfo.c_mobile + "')"; //string qry1="select count(*) from client where c_name='"+boinfo.c_name+"' and

c_mobileno='"+boinfo.c_mobile+"'"; //string qry2 = "select c_id from client where c_name='" + boinfo.c_name + "' and c_mobileno='" + boinfo.c_mobile + "'"; OleDbCommand cmd = new OleDbCommand(qry, con); //OleDbCommand cmd1 = new OleDbCommand(qry1, con); //OleDbCommand cmd2 = new OleDbCommand(qry2, con); try { con.Open(); //int rt1 =Convert.ToInt32( cmd1.ExecuteScalar()); //if (rt1 > 0) //{ // int rt2=Convert.ToInt32(cmd2.ExecuteScalar()); // return rt2; //} //else //{ int rt = cmd.ExecuteNonQuery(); return rt; //} } catch { throw; } finally { con.Close(); con.Dispose(); } } } }

BALclass using using using using using System; System.Collections.Generic; System.Linq; System.Text; BOclass;

using DALclass;

namespace BALclass { public class baladdclient { public int balclient(boaddclient boinfo) { daladdclient dalinfo = new daladdclient(); return dalinfo.dalclient(boinfo); } } } Testform.cs(Design) savebtn and close btn

Testform.cs using using using using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; BOclass; BALclass; DALclass;

namespace digital { public partial class client : Form { public client() { InitializeComponent(); } private void btnclose_Click(object sender, EventArgs e)

{ foreach (Form f in Application.OpenForms) { if (f.GetType() == typeof(newsale)) { f.Activate(); return; } } Form newform = new newsale(); newform.MdiParent = this.MdiParent; newform.Dock = DockStyle.Fill; newform.Show(); newform.Focus(); this.Close(); } private void btnsave_Click(object sender, EventArgs e) { if (txtcname.Text.Trim().Length == 0) { MessageBox.Show("Enter Client name...."); } else if (txtcaddress.Text.Trim().Length == 0) { MessageBox.Show("Enter Client address...."); } else if (txtcmobile.Text.Trim().Length == 0) { MessageBox.Show("Enter Client Mobile no...."); } else { try { baladdclient balinfo = new baladdclient(); boaddclient boinfo = new boaddclient(); boinfo.c_name = txtcname.Text.Trim(); boinfo.c_add = txtcaddress.Text.Trim(); boinfo.c_mobile = txtcmobile.Text.Trim(); int rt = balinfo.balclient(boinfo); if (rt > 0)

{ MessageBox.Show("Client Inserted Successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); txtcname.Text = ""; txtcaddress.Text = ""; txtcmobile.Text = ""; } else { MessageBox.Show("Some Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception p) { MessageBox.Show(p.Message.ToString()); } } } private void mobile_keypress(object sender, KeyPressEventArgs e) { char ch = e.KeyChar; if (!char.IsDigit(ch) && ch != 8) { e.Handled = true; } }

} } -----------------------------------------------------------------------------------------------------------------------------------------

3 tier architecture example in asp.net with C#


Introduction Here I will explain about uses of 3-Tier architecture and how to create or implement 3-tier architecture for our project in asp.net

Description 1. What is the use of 3-tier architecture and why we go for that architecture? 2. First we need to know what 3-Tier architecture is. 3. How to create 3-Tier architecture for our project? Uses of 3-Tier Architecture 1. To make application more understandable. 2. Easy to maintain, easy to modify application and we can maintain good look of architecture. 3. If we use this 3-Tier application we can maintain our application in consistency manner. Basically 3-Tier architecture contains 3 layers 1. Application Layer or Presentation Layer 2. Business Access Layer(BAL) or Business Logic Layer(BLL) 3. Data Access Layer(DAL) Here I will explain each layer with simple example that is User Registration Application Layer or Presentation Layer Presentation layer contains UI part of our application i.e., our aspx pages or input is taken from the user. This layer mainly used for design purpose and get or set the data back and forth. Here I have designed my registration aspx page like this

This is Presentation Layer for our project Design your page like this and double click on button save now in code behind we need to write statements to insert data into database this entire process related to Business Logic Layer and Data Access Layer.

Now we will discuss about Business Access Layer or Business Logic Layer Business Access Layer (BAL) or Business Logic Layer (BLL) This layer contains our business logic, calculations related with the data like insert data, retrieve data and validating the data. This acts as a interface between Application layer and Data Access Layer Now I will explain this business logic layer with my sample I have already finished form design (Application Layer) now I need to insert user details into database if user click on button save. Here user entering details regarding Username, password, Firstname, Lastname, Email, phone no, Location. I need to insert all these 7 parameters to database. Here we are placing all of our database actions into data access layer (DAL) in this case we need to pass all these 7 parameters to data access layers. In this situation we will write one function and we will pass these 7 parameters to function like this String Username= InserDetails (string Username, string Password, string Email, string Firstname, string Lastname, string phnno, string Location) If we need this functionality in another button click there also we need to declare the parameters like string Username, string Password like this rite. If we place all these parameters into one place and use these parameters to pass values from application layer to data access layer by using single object to whenever we require how much coding will reduce think about it for this reason we will create entity layer or property layer this layer comes under sub of group of our Business Logic layer Don't get confuse just follow my instructions enough How we have to create entity layer it is very simple Right click on your project web application---> select add new item ----> select class file in wizard ---> give name as BEL.CS because here I am using this name click ok Open the BEL.CS class file declare the parameters like this in entity layer Dont worry about code its very simple for looking its very big nothing is there just parameters declaration thats all check I have declared whatever the parameters I need to pass to data access layer I have declared those parameters only BEL.CS

#region Variables /// <summary> /// User Registration Variables /// </summary> private string _UserName; private string _Password; private string _FirstName; private string _LastName; private string _Email;

private string _Phoneno; private string _Location; private string _Created_By; #endregion /// <summary> /// Gets or sets the <b>_UserName</b> attribute value. /// </summary> /// <value>The <b>_UserName</b> attribute value.</value> public string UserName { get { return _UserName; } set { _UserName = value; } } /// <summary> /// Gets or sets the <b>_Password</b> attribute value. /// </summary> /// <value>The <b>_Password</b> attribute value.</value> public string Password { get { return _Password; } set { _Password = value; } } /// <summary> /// Gets or sets the <b>_FirstName</b> attribute value. /// </summary> /// <value>The <b>_FirstName</b> attribute value.</value> public string FirstName { get { return _FirstName; } set { _FirstName = value; } }

/// <summary> /// Gets or sets the <b>_LastName</b> attribute value. /// </summary> /// <value>The <b>_LastName</b> attribute value.</value> public string LastName { get { return _LastName; } set { _LastName = value; } } /// <summary> /// Gets or sets the <b>_Email</b> attribute value. /// </summary> /// <value>The <b>_Email</b> attribute value.</value> public string Email { get { return _Email; } set { _Email = value; } } /// <summary> /// Gets or sets the <b>_Phoneno</b> attribute value. /// </summary> /// <value>The <b>_Phoneno</b> attribute value.</value> public string Phoneno { get { return _Phoneno; } set { _Phoneno = value; } } /// /// /// /// <summary> Gets or sets the <b>_Location</b> attribute value. </summary> <value>The <b>_Location</b> attribute value.</value>

public string Location { get { return _Location; } set { _Location = value; } } /// <summary> /// Gets or sets the <b>_Created_By</b> attribute value. /// </summary> /// <value>The <b>_Created_By</b> attribute value.</value> public string Created_By { get { return _Created_By; } set { _Created_By = value; } Our parameters declaration is finished now I need to create Business logic layer how I have create it follow same process for add one class file now give name called BLL.CS. Here one point dont forget this layer will act as only mediator between application layer and data access layer based on this assume what this layer contains. Now I am writing the following BLL.CS(Business Logic layer)

#region Insert UserInformationDetails /// <summary> /// Insert UserDetails /// </summary> /// <param name="objUserBEL"></param> /// <returns></returns> public string InsertUserDetails(BEL objUserDetails) { DAL objUserDAL = new DAL(); try { return objUserDAL.InsertUserInformation(objUserDetails); } catch (Exception ex) { throw ex; } finally

{ objUserDAL = null; } } #endregion Here if you observe above code you will get doubt regarding these what is BEL objUserDetails DAL objUserDAL = new DAL(); and how this method comes return objUserDAL.InsertUserInformation(objUserDetails); Here BEL objUserDetails means we already created one class file called BEL.CS with some parameters have you got it now I am passing all these parameters to Data access Layer by simply create one object for our BEL class file What is about these statements I will explain about it in data access layer DAL objUserDAL = new DAL(); return objUserDAL.InsertUserInformation(objUserDetails); this DAL related our Data access layer. Check below information to know about that function and Data access layer Data Access Layer(DAL) Data Access Layer contains methods to connect with database and to perform insert,update,delete,get data from database based on our input data I think its to much data now directly I will enter into DAL Create one more class file like same as above process and give name as DAL.CS Write the following code in DAL class file

//SQL Connection string string ConnectionString = ConfigurationManager.AppSettings["LocalConnection"].ToString(); #region Insert User Details /// <summary> /// Insert Job Details /// </summary> /// <param name="objBELJobs"></param> /// <returns></returns> public string InsertUserInformation(BEL objBELUserDetails) { SqlConnection con = new SqlConnection(ConnectionString); con.Open();

SqlCommand cmd = new SqlCommand("sp_userinformation", con); cmd.CommandType = CommandType.StoredProcedure; try { cmd.Parameters.AddWithValue("@UserName",objBELUserDetails.UserName); cmd.Parameters.AddWithValue("@Password", objBELUserDetails.Password); cmd.Parameters.AddWithValue("@FirstName", objBELUserDetails.FirstName); cmd.Parameters.AddWithValue("@LastName", objBELUserDetails.LastName); cmd.Parameters.AddWithValue("@Email", objBELUserDetails.Email); cmd.Parameters.AddWithValue("@PhoneNo", objBELUserDetails.Phoneno); cmd.Parameters.AddWithValue("@Location", objBELUserDetails.Location); cmd.Parameters.AddWithValue("@Created_By", objBELUserDetails.Created_By); cmd.Parameters.Add("@ERROR", SqlDbType.Char, 500); cmd.Parameters["@ERROR"].Direction = ParameterDirection.Output; cmd.ExecuteNonQuery(); string strMessage = (string) cmd.Parameters["@ERROR"].Value; con.Close(); return strMessage; } catch (Exception ex) { throw ex; } finally { cmd.Dispose(); con.Close(); con.Dispose(); } } #endregion Here if you observe above functionality I am getting all the parameters by simply creating BEL objBELUserDetails. If we create one entity file we can access all parameters through out our project by simply creation of one object for that entity class based on this we can reduce redundancy of code and increase re usability Observe above code have u seen this function before? in BLL.CS i said i will explain it later got it in DAL.CS I have created one function InsertUserInformation and using this one in BLL.CS by simply creating one object of DAL in BLL.CS. Here you will get one doubt that is why BLL.CS we can use this DAL.CS directly into our code behind we already discuss Business logic layer provide interface between DAL and Application layer by using this we can maintain consistency to our application. Now our Business Logic Layer is ready and our Data access layer is ready now how we can use this in our application layer write following code in your save button click like this

protected void btnsubmit_Click(object sender, EventArgs e) { string Output = string.Empty;

if (txtpwd.Text == txtcnmpwd.Text) { BEL objUserBEL = new BEL(); objUserBEL.UserName = txtuser.Text; objUserBEL.Password = txtpwd.Text; objUserBEL.FirstName = txtfname.Text; objUserBEL.LastName = txtlname.Text; objUserBEL.Email = txtEmail.Text; objUserBEL.Phoneno = txtphone.Text; objUserBEL.Location = txtlocation.Text; objUserBEL.Created_By = txtuser.Text; BLL objUserBLL = new BLL(); Output = objUserBLL.InsertUserDetails(objUserBEL); } else { Page.RegisterStartupScript("UserMsg", "<Script language='javascript'>alert('" + "Password mismatch" + "');</script>"); } lblErrorMsg.Text = Output; } Here if you observe I am passing all parameters using this BEL(Entity Layer) and we are calling the method InsertUserDetails by using this BLL(Business Logic Layer) Now run your applciation test with debugger you can get idea clearly.

PayPal example project C# ASP.Net


December 1, 2011 13:31 by Aidan A few weeks ago I spent some time integrating PayPal Express Checkout into http://bookhashtags.com so that I could take payments for the featured book spot. Whilst PayPal produces a lot of documentation and provides some tools to help generate code these were not always that helpful. For example the code wizard produces code that doesn't build and uses a very old version of the API which means that a lot of the things you can now do according to the documentation just didn't work and it wasn't immediately obvious why. I have put together a bare bones Visual Studio 2010 project that will give you a quick understanding of the basics of the three Express Checkout calls (SetExpressCheckout, GetExpressCheckoutDetails and DoExpressCheckout) you need to make to get things working. To get the project to run you will need to set up a PayPal sandbox account here https://developer.paypal.com

You will need to add your sandbox account username, password and signature in the NVPAPICaller class. Once that is done just press play, change the default values on the default.aspx page and hit the Pay button. The example only adds one item and doesn't handle shipping costs. For more information on parameter names etc. see the PayPal Express Checkout Integration pdf Download the project

SMS Application( Send Sms from App To Mobile)


his contains student Data according to their qualifications.A Sms is send to them means For Example: if we select B.A then students having B.A qualifications can be send Sms by picking their mobileno This Application is very usefult to send Bulk Messages(SMS) It contains Master Pages as Create Users Change Password Create Qualification Student Data Vacancy Information Transactions are SendMsg Page This Application is Designed to Send Sms of jobs according to their Qualifications. Use Admin as username and Password to Login

Attachments
SMS Application(send Sms From Application) (809-21417-SmsApplication.rar)

Easy Example for Model View Presenter in ASP.NET - C#

Model View Presenter


Model View Presenter is a software approach pattern conceived as derivative of Model View Controller.

What is Model?
Model is a domain level or business level object. A model accomodates application data and provides behaviours to systematically access it. It should be independent, it should not have any association with user interface issues. So we can reuse the same model for different type of UI level applications.

What is View?
A View is a windowpane for showing the model data or elements to the application users. Model does not have direct link to the views. View know about thier Models, but not the other way around.

What is Presenter?
Presenter will address the user input and use this to manipulate the model data. View will pass the user input actions to the Presenter for interpretation. Presenter will act on the received data from View and communicate with Model and produce results to the View.

Here is the classic example for implementing and understanding Model View Presenter pattern in an ASP.Net application. I believed this will helps you to get a good start in Model View Presenter. Step 1: Create an interface for a business object (Model). For example:
Collapse | Copy Code
public interface ICircleModel { double getArea(double radius); }

Step 2: Create a class for Model


Collapse | Copy Code
public class CModel: ICircleModel { public CModel(){} public double getArea(double radius) {

} }

return Math.PI * radius * radius;

Step 3: Create an interface for View


Collapse | Copy Code
public interface IView { string RadiusText { get; set;} string ResultText { get; set;} }

Step 4: Create UI like below with a Label, TextBox and Button. Textbox for getting radius of the circle, Label is for displaying the area and Button is for calculating the Area
Collapse | Copy Code
<html xmlns="<a href="%22http://www.w3.org/1999/xhtml %22">http://www.w3.org/1999/xhtml</a>"> <head runat="server"> <title>MVP-Easy Example for ASP.Net C#</title> </head> <body> <form id="form1" runat="server"> <table> <tr> <th colspan="2">Calculate Area of Circle</th> </tr> <tr> <td>Enter Radius</td> <td><asp:TextBox ID="TextRadius" runat="server"></asp:TextBox></td> </tr> <tr> <td>Result:</td> <td><asp:Label ID="LabelResult" runat="server" ForeColor="red"></asp:Label></td> </tr> <tr align="right"> <td colspan="2"><asp:Button ID="ButtonResult" runat="server" Text="Get Area?" OnClick="ButtonResult_Click" /></td> </tr> </table> </form> </body> </html>

Step 5: Create a Presenter class for collecting user inputs from View and pass view details to the Model.
Collapse | Copy Code
public class CPresenter { IView mview;

public CPresenter(IView view) { mview = view; } public double CalculateCircleArea() { CModel model = new CModel(); mview.ResultText = model.getArea(double.Parse(mview.RadiusText)).ToString(); return mview.ResultText.ToString(); } }

Step 6: Code-behind of ASPX page - View is communicating to the Model via Presenter
Collapse | Copy Code
public partial class _Default : System.Web.UI.Page,IView { protected void Page_Load(object sender, EventArgs e) { } protected void ButtonResult_Click(object sender, EventArgs e) { CPresenter presenter = new CPresenter(this); presenter.CalculateCircleArea(); } public string RadiusText { get{return TextRadius.Text;} set{TextRadius.Text = value;} } public string ResultText { get { return LabelResult.Text; } set { LabelResult.Text = value; } } }

Now, I am sure, you are also feeling the same, it is pretty simple :)

3 Tier Architecture in ASP.NET


This is a 3 Tier Architecture Project, How you can create a 3 tier architecture in asp.net. This Project Contains of 3 Different Projects. 1. Data Tier 2. Business Tier 3. Application Tier Data Tier : Here Data tier is a different Project, where all the functions which are useful for

accessing database is created, like to Fetch Record, Auto Number Generation, Delete Record etc. That all are Public Functions. Business Tier : Here We have to write any business logic and calls the information (function) from data tier. and pass it to the Application Tier. Application Tier : Application Tier is only having a controls over it. and fetch the functions from Business tier. So It's nice Project Created for Beginners who can get little bit idea about these 3 tier architecture. ------------------------------------------------------------------------------------------------------------------

Você também pode gostar