You are on page 1of 4

ASB.

NET One tire Lab Manual By Aleka Melese

ASP.NET Using C# One Tire Lab Manual


Presentation Layer
<table style=" font-family:Times New Roman; font-size:medium; font-weight:bolder">
<tr><td>Name</td><td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox></td></tr>
<tr><td>Sex</td><td>
<asp:TextBox ID="txtSex" runat="server"></asp:TextBox></td></tr>
<tr><td>Age</td><td>
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox></td></tr>
<tr><td>Address</td><td>
<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox></td></tr>
<tr><td></td><td>
<asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" />
<asp:Label ID="lblMsg" runat="server" ForeColor="Red" ></asp:Label>
</td></tr>
</table>
Code Behind for Insert Data to DB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace Onrtire
{
public partial class WebForm1 : System.Web.UI.Page
{
string str =
ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}

protected void Button1_Click(object sender, EventArgs e)


{
try
{
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("IN_LabOne", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Sex", txtSex.Text);

1|Page University of Gondar


ASB.NET One tire Lab Manual By Aleka Melese

cmd.Parameters.AddWithValue("@Age", txtAge.Text);
cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
lblMsg.Text = "Successfully Inserted";
txtName.Text = "";
txtSex.Text = "";
txtAge.Text = "";
txtAddress.Text = "";
}
catch (Exception ex)
{
//lblMsg.Text = "Not Successfully Inserted";
lblMsg.Text = ex.ToString();
}
}
}
}
Code Behind for Select, Update and Delete Data from DB
Presentation Layer
<table style=" font-family:Times New Roman; font-size:medium; font-weight:bolder">
<tr><td>ID</td><td>
<asp:TextBox ID="txtID" runat="server"></asp:TextBox></td></tr>
<tr><td>Name</td><td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox></td></tr>
<tr><td>Sex</td><td>
<asp:TextBox ID="txtSex" runat="server"></asp:TextBox></td></tr>
<tr><td>Age</td><td>
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox></td></tr>
<tr><td>Address</td><td>
<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox></td></tr>
<tr><td></td><td>
<asp:Button ID="Select" runat="server" Text="Select" onclick="Select_Click" />
<asp:Button ID="Update" runat="server" Text="Update" onclick="Button1_Click" />
<asp:Button ID="Delete" runat="server" Text="Delete" onclick="Delete_Click" />
<asp:Label ID="lblMsg" runat="server" ForeColor="Red" ></asp:Label>
</td></tr>
</table>
Code Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

2|Page University of Gondar


ASB.NET One tire Lab Manual By Aleka Melese

using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace Onrtire
{
public partial class Update_Select : System.Web.UI.Page
{
string str =
ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}

protected void Button1_Click(object sender, EventArgs e)


{
try
{
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("UP_LabOne", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ID", txtID.Text);
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Sex", txtSex.Text);
cmd.Parameters.AddWithValue("@Age", txtAge.Text);
cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
lblMsg.Text = "Successfully Updated";
txtName.Text = "";
txtSex.Text = "";
txtAge.Text = "";
txtAddress.Text = "";
}
catch (Exception ex)
{
//lblMsg.Text = "Not Successfully Updated";
lblMsg.Text = ex.ToString();
}
}
protected void Delete_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(str);

3|Page University of Gondar


ASB.NET One tire Lab Manual By Aleka Melese

SqlCommand cmd = new SqlCommand("DE_LabOne", con);


cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ID", txtID.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
lblMsg.Text = "Successfully Deleted";

}
catch (Exception ex)
{
//lblMsg.Text = "Not Successfully Deleted";
lblMsg.Text = ex.ToString();
}

protected void Select_Click(object sender, EventArgs e)


{
try
{
SqlConnection con = new SqlConnection(str);
con.Open();
SqlCommand cmd = new SqlCommand("[SE_LabOne]", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ID", txtID.Text);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
txtName.Text = dr["Name"].ToString();
txtSex.Text = dr["Sex"].ToString();
txtAge.Text = dr["Age"].ToString();
txtAddress.Text = dr["Address"].ToString();
lblMsg.Text = "Successfully Seleted";
}
}
catch (Exception ex)
{
//lblMsg.Text = "Not Successfully Deleted";
lblMsg.Text = ex.ToString();
}
}
}
}

4|Page University of Gondar

You might also like