Create a web application for to display the phone no of an author using database

 

First Step:- Working with Database

Then Follow  :- 

Default7b.aspx 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default7b.aspx.cs" 
Inherits="Database.Default7b" %> 
 
<!DOCTYPE html> 
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
        <div> 
            <asp:DropDownList ID="DropDownList1" runat="server" 
DataSourceID="SqlDataSource1" DataTextField="FirstName" DataValueField="Phone">             </asp:DropDownList> 
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ 
ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM 
[Customer]"></asp:SqlDataSource> 
        </div> 
        <p> 
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Get Phone 
No." BackColor="Lime" /> 
        </p> 
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>     </form> 
</body> 
</html> 
 

Default.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 Database 
    public partial class Default7b : System.Web.UI.Page 
    { 
        protected void Page_Load(object sender, EventArgs e) 
        { 
            if (IsPostBack == false) 
            { 
                string connStr = 
                ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 
                SqlConnection con = new SqlConnection(connStr); 
                SqlCommand cmd = new SqlCommand("Select Distinct FirstName from Customer", con); 
                con.Open(); 
                SqlDataReader reader = cmd.ExecuteReader();     
                DropDownList1.DataTextField = "FirstName"; 
                DropDownList1.DataValueField = "Phone"; 
                DropDownList1.DataBind(); 
                reader.Close();                 
                con.Close(); 
            } 
        } 
 
        protected void Button1_Click(object sender, EventArgs e) 
        { 
            Label1.Text = "Your Phone Number is : "+DropDownList1.SelectedValue; 
        } 
    } 

Output:-



Post a Comment

0 Comments