.Net.net code to insert new record in database using C#. Database name: College.accdb Table name: students Table structure: std_id number std_name text std_age number Table content (before insert): 2 abcd 21 <%@ Page Language="C#" %> <%@ Import namespace="system.data" %> <%@ Import Namespace="System.Data.OleDb" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <script runat="server"> protected void btn_insert_click(object sender, EventArgs e) OleDbConnection cn; OleDbCommand cm; String query; cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Kashif\Documents\college.accdb"); query = "insert into students values(" + std_id.text + ",'" + std_name.text + "'," + std_age.text + ")"; cm = new OleDbCommand(query, cn); try cn.open(); cm.executenonquery(); catch (OleDbException ex) Response.Write("Error: " + ex.message); finally if (cn.state == ConnectionState.Open) cn.close(); std_name.text = ""; Page 1 of 10
std_age.text = ""; std_id.text = ""; </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>dynamic data entry using ASP.net and ADO.net</title> </head> <body> <form id="form1" runat="server"> <div> New Student entry student id: <asp:textbox ID="std_id" runat="server"></asp:textbox> student name: <asp:textbox ID="std_name" runat="server"></asp:textbox> age: <asp:textbox ID="std_age" runat="server"></asp:textbox> <asp:button ID="btn_insert" runat="server" Text="Insert" onclick="btn_insert_click" /> </div> </form> </body> </html> Table content (after insert): std_id=1, std_name=xyz, std_age=20 1 xyz 20 2 abcd 21 Page 2 of 10
.net code to delete record in database using C#. Database name: College.accdb Table name: students Table structure: std_id number std_name text std_age number Table content (before delete): 2 abcd 21 <%@ Page Language="C#" %> <%@ Import namespace="system.data" %> <%@ Import Namespace="System.Data.OleDb" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <script runat="server"> protected void btn_del_click(object sender, EventArgs e) OleDbConnection cn; OleDbCommand cm; String query; cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Kashif\Documents\college.accdb"); query = "delete from students where std_id=" + del_id.text; cm = new OleDbCommand(query, cn); try cn.open(); cm.executenonquery(); catch (OleDbException ex) Response.Write("Error: " + ex.message); finally if (cn.state == ConnectionState.Open) cn.close(); del_id.text = ""; Page 3 of 10
</script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>dynamic data entry using ASP.net and ADO.net</title> </head> <body> <form id="form1" runat="server"> <div> Delete Student Student id: <asp:textbox ID="del_id" runat="server"></asp:textbox> <asp:button ID="btn_del" runat="server" Text="Delete" onclick="btn_del_click" /> </div> </form> </body> </html> Table content (after delete): std_id=2 Page 4 of 10
.net code to update record in database using C#. Database name: College.accdb Table name: students Table structure: std_id number std_name text std_age number Table content (before update): 2 Abc 21 <%@ Page Language="C#" %> <%@ Import namespace="system.data" %> <%@ Import Namespace="System.Data.OleDb" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <script runat="server"> protected void btn_update_click(object sender, EventArgs e) OleDbConnection cn; OleDbCommand cm; String query; cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Kashif\Documents\college.accdb"); query = "update students set std_name=?, std_age=? where std_id=?"; cm = new OleDbCommand(query, cn); try cm.parameters.add(new OleDbParameter("std_name",update_name.Text)); cm.parameters.add(new OleDbParameter("std_age",update_age.Text)); cm.parameters.add(new OleDbParameter("std_id",update_id.Text)); cn.open(); cm.executenonquery(); catch (OleDbException ex) Response.Write("Error: " + ex.message); finally Page 5 of 10
if (cn.state == ConnectionState.Open) cn.close(); update_name.text = ""; update_age.text = ""; update_id.text = ""; </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>dynamic data entry using ASP.net and ADO.net</title> </head> <body> <form id="form1" runat="server"> <div> Update Student Student id: <asp:textbox ID="update_id" runat="server"></asp:textbox> Student name: <asp:textbox ID="update_name" runat="server"></asp:textbox> age: <asp:textbox ID="update_age" runat="server"></asp:textbox> <asp:button ID="btn_update" runat="server" onclick="btn_update_click" Text="Update" /> </div> </form> </body> </html> Table content (after update): std_id=2, std_name=abcd, std_age=20 2 abcd 20 Page 6 of 10
.net code to view records in tabular format from database using C#. Database name: College.accdb Table name: students Table structure: std_id number std_name text std_age number <%@ Page Language="C#" %> <%@ Import namespace="system.data" %> <%@ Import Namespace="System.Data.OleDb" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <script runat="server"> protected void btn_view_click(object sender, EventArgs e) OleDbConnection cn; OleDbCommand cm; String query; OleDbDataReader reader; cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Kashif\Documents\college.accdb"); query = "select * from students"; cm = new OleDbCommand(query, cn); try cn.open(); reader = cm.executereader(); Response.Write("<table border='1'>"); Response.Write("<tr><th>ID</th><th>Name</th><th>Age</th></tr>"); while(reader.read()) Response.Write("<tr>"); Response.Write("<td>"+reader["std_id"]+"</td>"); Response.Write("<td>" + reader["std_name"] + "</td>"); Response.Write("<td>" + reader["std_age"] + "</td>"); Response.Write("</tr>"); Response.Write("</table>"); catch (OleDbException ex) Response.Write("Error: " + ex.message); Page 7 of 10
finally if (cn.state == ConnectionState.Open) cn.close(); </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>dynamic data entry using ASP.net and ADO.net</title> </head> <body> <form id="form1" runat="server"> <div> <asp:button ID="btn_view" runat="server" Text="View Records" onclick="btn_view_click" /> </div> </form> </body> </html> 2 abcd 20 Page 8 of 10
Life cycle of ASP.net application. ASP.Net life cycle specifies, how: ASP.Net processes pages to produce dynamic output The application and its pages are instantiated and processed ASP.Net compiles the pages dynamically The ASP.Net life cycle could be divided into two groups: Application Life Cycle Page Life Cycle The application life cycle has the following stages: 1. User makes a request for accessing application resource, a page. Browser sends this request to the web server. 2. A unified pipeline receives the first request and the following events take place: 1. An object of the ApplicationManager class is created. 2. An object of the HostingEnvironment class is created to provide information regarding the resources. 3. Top level items in the application are compiled. 3. Response objects are created. the application objects: HttpContext, HttpRequest and HttpResponse are created and initialized. 4. An instance of the HttpApplication object is created and assigned to the request. The request is processed by the HttpApplication class. Different events are raised by this class for processing the request. Page 9 of 10
ASP.net code to display today s date and time. <%@ Page Language="C#" %> <!DOCTYPE html /> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>show Date & Time</title> </head> <body> <% Response.Write(DateTime.Now); %> </body> </html> 5/8/2016 11:10:39 AM Page 10 of 10