hello buddies now i am going to explain bout ,how to create objects and how to pass values from one layer to other layer and creating proc for this app..simply fallow the code...i have taken 3 class files in App_Code and taken 1 .aspx file.let's start from BEL.
creating properties in BEL.
public class BEL
{
public BEL()
{
//
// TODO: Add constructor logic here
//
}
private string _fname;
public string Fname
{
get { return _fname; }
set { _fname = value; }
}
private string _lname;
public string Lname
{
get { return _lname; }
set { _lname = value; }
}
private int _age;
public int Age
{
get { return _age; }
set { _age = value; }
}
}
Code In Web.Config file
<connectionStrings>
<add name="ConStr" connectionString="Data Source=SHANKAR;Initial Catalog=ltc_gamble;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
write the fallowing code in DAL
public class PersonDAL3
{
string connStr = ConfigurationManager.ConnectionStrings["ConStr"].ToString();
public PersonDAL3()
{ }
public int Insert(BEL objBEL)
{
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
SqlCommand dCmd = new SqlCommand("InsertData", conn);
dCmd.CommandType = CommandType.StoredProcedure;
try
{
dCmd.Parameters.AddWithValue("@fname",objBEL.Fname);
dCmd.Parameters.AddWithValue("@lname", objBEL.Lname);
dCmd.Parameters.AddWithValue("@age",objBEL.Age);
return dCmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
dCmd.Dispose();
conn.Close();
conn.Dispose();
}
}
}
Write the fallowing code in BAL
public class PersonBAL3
{
public PersonBAL3()
{}
public int Insert(BEL objBELinbl)
{
PersonDAL3 pDAL = new PersonDAL3();
try
{
return pDAL.Insert(objBELinbl);
}
catch
{
throw;
}
finally
{
pDAL = null;
}
}
}
Write the fallowing source code in .aspx source file with in the head tag
<asp:Label ID="lblMessage" runat="Server" ForeColor="red" EnableViewState="False"></asp:Label>
<table style="border: 2px solid #cccccc;">
<tr style="background-color: #C0C0C0; color: White;">
<th colspan="3">
Add Records
</th>
</tr>
<tr>
<td>
First Name:
</td>
<td>
<asp:TextBox ID="txtFirstName" runat="Server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="req1" runat="Server" Text="*" ControlToValidate="txtFirstName"
Display="dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Last Name:
</td>
<td>
<asp:TextBox ID="txtLastName" runat="Server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="req2" runat="Server" Text="*" ControlToValidate="txtLastName"
Display="dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Age:
</td>
<td>
<asp:TextBox ID="txtAge" runat="Server" Columns="4"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="req3" runat="Server" Text="*" ControlToValidate="txtAge"
Display="dynamic"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="Comp1" runat="Server" Text="Only integer" ControlToValidate="txtAge"
Operator="DataTypeCheck" Type="Integer"></asp:CompareValidator>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="AddRecords" />
</td>
</tr>
</table>
Write this code in .aspx.cs file button_click event
public partial class Spider : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void AddRecords(object sender, EventArgs e)
{
if (!Page.IsValid)
return;
int intResult = 0;
// Page is valid, lets go ahead and insert records
// Instantiate BAL object
BEL objBEL = new BEL();
PersonBAL3 pBAL = new PersonBAL3();
// Instantiate the object we have to deal with
objBEL.Fname = txtFirstName.Text;
objBEL.Lname = txtLastName.Text;
objBEL.Age = Int32.Parse(txtAge.Text);
try
{
intResult = pBAL.Insert(objBEL);
if (intResult > 0)
lblMessage.Text = "New record inserted successfully.";
else
lblMessage.Text = "FirstName [<b>"+ txtFirstName.Text +"</b>] alredy exists, try another name";
}
catch (Exception ee)
{
lblMessage.Text = ee.Message.ToString();
}
finally
{
pBAL = null;
}
}
}
Here is the table and procedure for fallowing App.
CREATE TABLE INSERTING
(
PERSONID INT PRIMARY KEY IDENTITY(101,1),
FNAME VARCHAR(30),
LNAME VARCHAR(30),
AGE INT
)
ALTER PROC InsertData
@FNAME VARCHAR(30),
@LNAME VARCHAR(30),
@AGE INT
AS
BEGIN
INSERT INTO INSERTING(FNAME,LNAME,AGE)VALUES(@FNAME,@LNAME,@AGE)
END
EXEC InsertData 'SHANKAR','PARSHIMONI',24
Enjoy the coading,Thanks for visiting this blog,
creating properties in BEL.
public class BEL
{
public BEL()
{
//
// TODO: Add constructor logic here
//
}
private string _fname;
public string Fname
{
get { return _fname; }
set { _fname = value; }
}
private string _lname;
public string Lname
{
get { return _lname; }
set { _lname = value; }
}
private int _age;
public int Age
{
get { return _age; }
set { _age = value; }
}
}
Code In Web.Config file
<connectionStrings>
<add name="ConStr" connectionString="Data Source=SHANKAR;Initial Catalog=ltc_gamble;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
write the fallowing code in DAL
public class PersonDAL3
{
string connStr = ConfigurationManager.ConnectionStrings["ConStr"].ToString();
public PersonDAL3()
{ }
public int Insert(BEL objBEL)
{
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
SqlCommand dCmd = new SqlCommand("InsertData", conn);
dCmd.CommandType = CommandType.StoredProcedure;
try
{
dCmd.Parameters.AddWithValue("@fname",objBEL.Fname);
dCmd.Parameters.AddWithValue("@lname", objBEL.Lname);
dCmd.Parameters.AddWithValue("@age",objBEL.Age);
return dCmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
dCmd.Dispose();
conn.Close();
conn.Dispose();
}
}
}
Write the fallowing code in BAL
public class PersonBAL3
{
public PersonBAL3()
{}
public int Insert(BEL objBELinbl)
{
PersonDAL3 pDAL = new PersonDAL3();
try
{
return pDAL.Insert(objBELinbl);
}
catch
{
throw;
}
finally
{
pDAL = null;
}
}
}
Write the fallowing source code in .aspx source file with in the head tag
<asp:Label ID="lblMessage" runat="Server" ForeColor="red" EnableViewState="False"></asp:Label>
<table style="border: 2px solid #cccccc;">
<tr style="background-color: #C0C0C0; color: White;">
<th colspan="3">
Add Records
</th>
</tr>
<tr>
<td>
First Name:
</td>
<td>
<asp:TextBox ID="txtFirstName" runat="Server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="req1" runat="Server" Text="*" ControlToValidate="txtFirstName"
Display="dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Last Name:
</td>
<td>
<asp:TextBox ID="txtLastName" runat="Server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="req2" runat="Server" Text="*" ControlToValidate="txtLastName"
Display="dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Age:
</td>
<td>
<asp:TextBox ID="txtAge" runat="Server" Columns="4"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="req3" runat="Server" Text="*" ControlToValidate="txtAge"
Display="dynamic"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="Comp1" runat="Server" Text="Only integer" ControlToValidate="txtAge"
Operator="DataTypeCheck" Type="Integer"></asp:CompareValidator>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="AddRecords" />
</td>
</tr>
</table>
Write this code in .aspx.cs file button_click event
public partial class Spider : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void AddRecords(object sender, EventArgs e)
{
if (!Page.IsValid)
return;
int intResult = 0;
// Page is valid, lets go ahead and insert records
// Instantiate BAL object
BEL objBEL = new BEL();
PersonBAL3 pBAL = new PersonBAL3();
// Instantiate the object we have to deal with
objBEL.Fname = txtFirstName.Text;
objBEL.Lname = txtLastName.Text;
objBEL.Age = Int32.Parse(txtAge.Text);
try
{
intResult = pBAL.Insert(objBEL);
if (intResult > 0)
lblMessage.Text = "New record inserted successfully.";
else
lblMessage.Text = "FirstName [<b>"+ txtFirstName.Text +"</b>] alredy exists, try another name";
}
catch (Exception ee)
{
lblMessage.Text = ee.Message.ToString();
}
finally
{
pBAL = null;
}
}
}
Here is the table and procedure for fallowing App.
CREATE TABLE INSERTING
(
PERSONID INT PRIMARY KEY IDENTITY(101,1),
FNAME VARCHAR(30),
LNAME VARCHAR(30),
AGE INT
)
ALTER PROC InsertData
@FNAME VARCHAR(30),
@LNAME VARCHAR(30),
@AGE INT
AS
BEGIN
INSERT INTO INSERTING(FNAME,LNAME,AGE)VALUES(@FNAME,@LNAME,@AGE)
END
EXEC InsertData 'SHANKAR','PARSHIMONI',24
Enjoy the coading,Thanks for visiting this blog,
1 comment:
this is the best understandable and bug free code.....
Post a Comment