ADO.NET Web Form Example
20 February 2023

ADO.NET Web Form Example

By

We can create a web form that has ADO.NET connectivity. A simple web form that has form controls can be submitted to the server. ADO.NET allows us to store the submitted values to store into SQL Server database.

Here, we are creating a web form application that connects to the SQL Server database.

This web form contains the following source code.

WebForm

// WebFormAdoNet.aspx

  1. <%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”WebFormAdoNet.aspx.cs”   
  2. Inherits=”ado.netWebFormExample.WebFormAdoNet” %>  
  3. <!DOCTYPE html>  
  4. <html xmlns=”http://www.w3.org/1999/xhtml”>  
  5. <head runat=”server”>  
  6.     <title></title>  
  7.     <style type=”text/css”>  
  8.         .auto-style1 {  
  9.             width: 100%;  
  10.         }  
  11.         .auto-style2 {  
  12.             width: 100px;  
  13.         }  
  14.         .auto-style3 {  
  15.             width: 95px;  
  16.         }  
  17.     </style>  
  18. </head>  
  19. <body>  
  20.     <form id=”form1″ runat=”server”>  
  21.         <div>  
  22.             <table class=”auto-style1″>  
  23.                 <tr>  
  24.                     <td class=”auto-style2″>  
  25.                        <asp:Label runat=”server” Text=”User Name” ID=”usernamelabelId”></asp:Label></td>  
  26.                     <td>  
  27.                        <asp:TextBox ID=”UsernameId” runat=”server”></asp:TextBox></td>  
  28.                 </tr>  
  29.                 <tr>  
  30.                     <td class=”auto-style2″>  
  31.                         <asp:Label runat=”server” Text=”Email ID”></asp:Label></td>  
  32.                     <td>  
  33.                         <asp:TextBox ID=”EmailId” runat=”server”></asp:TextBox></td>  
  34.                 </tr>  
  35.                 <tr>  
  36.                     <td class=”auto-style2″>  
  37.                         <asp:Label runat=”server” Text=”Contact”></asp:Label></td>  
  38.                     <td>  
  39.                         <asp:TextBox ID=”ContactId” runat=”server”></asp:TextBox></td>  
  40.                 </tr>  
  41.                 <tr>  
  42.                     <td class=”auto-style2″></td>  
  43.                     <td>  
  44.                         <asp:Button ID=”ButtonId” runat=”server” Text=”Submit” OnClick=”ButtonId_Click” /></td>  
  45.                 </tr>  
  46.             </table>  
  47.         </div>  
  48.     <div>  
  49.         <asp:Label ID=”Label1″ runat=”server”></asp:Label>  
  50.     </div>  
  51.     </form>  
  52.     <table class=”auto-style1″>  
  53.         <tr>  
  54.             <td class=”auto-style3″>  
  55.                 <asp:Label ID=”Label2″ runat=”server”></asp:Label></td>  
  56.             <td>  
  57.                 <asp:Label ID=”Label5″ runat=”server”></asp:Label></td>  
  58.         </tr>  
  59.         <tr>  
  60.             <td class=”auto-style3″>  
  61.                 <asp:Label ID=”Label3″ runat=”server”></asp:Label></td>  
  62.             <td>  
  63.                 <asp:Label ID=”Label6″ runat=”server”></asp:Label></td>  
  64.         </tr>  
  65.         <tr>  
  66.             <td class=”auto-style3″>  
  67.                 <asp:Label ID=”Label4″ runat=”server”></asp:Label></td>  
  68.             <td>  
  69.                 <asp:Label ID=”Label7″ runat=”server”></asp:Label></td>  
  70.         </tr>  
  71.     </table>  
  72.     </body>  
  73. </html>  

CodeBehind

// WebFormAdoNet.aspx.cs

  1. using System;  
  2. using System.Data.SqlClient;  
  3. namespace ado.netWebFormExample  
  4. {  
  5.     public partial class WebFormAdoNet : System.Web.UI.Page  
  6.     {  
  7.         protected void Page_Load(object sender, EventArgs e)  
  8.         {  
  9.         }  
  10.         protected void ButtonId_Click(object sender, EventArgs e)  
  11.         {  
  12.             SqlConnection con = null;  
  13.             try  
  14.             {  
  15.                 // Creating Connection  
  16.                 con = new SqlConnection(“data source=.; database=student; integrated security=SSPI”);  
  17.                 // Writing insert query  
  18.                 string query = “insert into student(name,email,contact)values(‘”+UsernameId.Text+ “‘,  
  19.                 ‘” + EmailId.Text + “‘,'” + ContactId.Text + “‘)”;  
  20.                 SqlCommand sc = new SqlCommand(query,con);  
  21.                 // Opening connection  
  22.                 con.Open();  
  23.                 // Executing query  
  24.                 int status = sc.ExecuteNonQuery();  
  25.                 Label1.Text = “Your record has been saved with the following details!”;  
  26.                 // ———————– Retrieving Data —————— //  
  27.                 SqlCommand cm = new SqlCommand(“select top 1 * from student”, con);  
  28.                 // Executing the SQL query  
  29.                 SqlDataReader sdr = cm.ExecuteReader();  
  30.                 sdr.Read();  
  31.                     Label2.Text = “User Name”; Label5.Text = sdr[“name”].ToString();  
  32.                     Label3.Text = “Email ID”;  Label6.Text = sdr[“email”].ToString();  
  33.                     Label4.Text = “Contact”;   Label7.Text = sdr[“contact”].ToString();                  
  34.             }  
  35.             catch (Exception ex)  
  36.             {  
  37.                 Console.WriteLine(“OOPs, something went wrong.” + ex);  
  38.             }  
  39.             // Closing the connection  
  40.             finally  
  41.             {  
  42.                 con.Close();  
  43.             }            
  44.         }  
  45.     }  
  46. }  

Output:

It produces the following output to the browser.

ADO Net Webform Example 1

Fill the form and submit data.

ADO Net Webform Example 2

After submitting, it store and retrieve the data from the SQL Server database.

ADO Net Webform Example 3
Prev Post

ស្វែងយល់អំពី Entity Framework

Next Post

ADO.NET MVC Example

post-bars