Create a web application to display records by using database

 Display.aspx 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Display.aspx.cs" Inherits="_6B.Display" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 

<head runat="server"> 

    <title>Customer Details</title> 

    <h1>Customer Records</h1> 

</head> 

<body> 

    <form id="form1" runat="server"> 

        <div> 

            <asp:Button ID="Button1" runat="server" Text="Display Details " Width="122px" 

OnClick="Button1_Click" /><br/><br /> 

            <asp:Label ID="Label1" runat="server" Text=" "></asp:Label>         </div> 

    </form> 

</body> 

</html> 

Display.aspx.cs 

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 _6B 

    public partial class Display : System.Web.UI.Page 

    { 

        protected void Page_Load(object sender, EventArgs e) 

        { 

        } 

        protected void Button1_Click(object sender, EventArgs e) 

        { 

            string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; 

            SqlConnection con = new SqlConnection(connStr);             

            con.Open(); 

            SqlCommand cmd = new SqlCommand("Select * from Customer", con); 

            SqlDataReader reader = cmd.ExecuteReader(); 

            while (reader.Read()) 

            { 

                Label1.Text += reader["Id"].ToString() + "          " + reader["Name"].ToString() + "          " + reader["City"].ToString() + "       " + reader["State"].ToString() + "<br>"; 

            } 

            reader.Close();             con.Close(); 

        } 

    } 

Web.config 

<?xml version="1.0" encoding="utf-8"?> 

<!-- 

  For more information on how to configure your ASP.NET application, please visit   https://go.microsoft.com/fwlink/?LinkId=169433 

  --> 

<configuration> 

  <system.web> 

    <compilation debug="true" targetFramework="4.7.2"/> 

    <httpRuntime targetFramework="4.7.2"/> 

  </system.web> 

  <system.codedom> 

    <compilers> 

      <compiler language="c#;cs;csharp" extension=".cs"         type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 

        warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/> 

      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"         type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, 

PublicKeyToken=31bf3856ad364e35" 

        warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 

/define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/> 

    </compilers> 

  </system.codedom> 

  <connectionStrings> 

    <add name="connStr" connectionString="Data 

Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\RupamSolution\source\repos

\6B\6B\App_Data\CustomerData.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>  

  </connectionStrings>

</configuration> 

Output:- 



Post a Comment

0 Comments