Asp.Net Tutorials And Project Code


SQL Tutorial Class

Sunday, February 16, 2020

How to retrieve value from database in asp.net

Get value from database in asp.net c#: Connect with database and get value from database.

Example of retrieving value from database:
[Note: I have inserted value in previous code project. Click on previous code project]


Asp.Net Form
How to retrieve value from database in asp.net C#






































Form Design Code

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Label ID="Label1" runat="server" style="z-index: 1; left: 65px; top: 111px; position: absolute" Text="Date Of Birth*"></asp:Label>
        <asp:Label ID="Label2" runat="server" style="z-index: 1; left: 107px; top: 80px; position: absolute" Text="Gender*"></asp:Label>
        <asp:RadioButton ID="rother" runat="server" style="z-index: 1; left: 304px; top: 78px; position: absolute" Text="Other" />
        <asp:RadioButton ID="rfemale" runat="server" style="z-index: 1; left: 222px; top: 79px; position: absolute" Text="Female" />
        <asp:RadioButton ID="rmale" runat="server" style="z-index: 1; left: 157px; top: 77px; position: absolute" Text="Male" />
        <asp:Label ID="Label3" runat="server" style="z-index: 1; left: 99px; top: 165px; position: absolute" Text="Country*"></asp:Label>
        <asp:Label ID="Label4" runat="server" style="z-index: 1; left: 122px; top: 194px; position: absolute" Text="City*"></asp:Label>
        <asp:Label ID="Label7" runat="server" style="z-index: 1; left: 93px; top: 274px; position: absolute" Text="Zip code*"></asp:Label>
        <asp:Label ID="Label8" runat="server" style="z-index: 1; left: 110px; top: 300px; position: absolute" Text="Phone"></asp:Label>
        <asp:Label ID="Label11" runat="server" style="z-index: 1; left: 89px; top: 407px; position: absolute" Text="Education"></asp:Label>
        <asp:Label ID="Label12" runat="server" style="z-index: 1; left: 51px; top: 380px; position: absolute" Text="Education Type*"></asp:Label>
        <asp:Label ID="Label13" runat="server" style="z-index: 1; left: 77px; top: 327px; position: absolute" Text="Mobile No*"></asp:Label>
        <asp:Label ID="Label14" runat="server" style="z-index: 1; left: 108px; top: 354px; position: absolute" Text="EMail*"></asp:Label>
        <asp:Label ID="Label9" runat="server" style="z-index: 1; left: 121px; top: 249px; position: absolute; bottom: 257px" Text="Stat*"></asp:Label>
        <asp:Label ID="Label10" runat="server" style="z-index: 1; left: 113px; top: 221px; position: absolute" Text="Street*"></asp:Label>
        <asp:Label ID="Label5" runat="server" style="z-index: 1; left: 93px; top: 140px; position: absolute" Text="Address*"></asp:Label>
        <asp:Label ID="Label6" runat="server" style="z-index: 1; left: 86px; top: 51px; position: absolute" Text="Full Name*"></asp:Label>
        <asp:TextBox ID="txtstat" runat="server" style="z-index: 1; left: 158px; top: 247px; position: absolute"></asp:TextBox>
        <asp:TextBox ID="txtcity" runat="server" style="z-index: 1; left: 158px; top: 194px; position: absolute"></asp:TextBox>
        <asp:TextBox ID="txtphone" runat="server" style="z-index: 1; left: 158px; top: 301px; position: absolute"></asp:TextBox>
        <asp:TextBox ID="txtremarks" runat="server" style="z-index: 1; left: 505px; top: 75px; position: absolute; height: 81px; width: 260px" TextMode="MultiLine"></asp:TextBox>
        <asp:TextBox ID="txtcustid" runat="server" style="z-index: 1; left: 160px; top: 21px; position: absolute; width: 71px;"></asp:TextBox>
        <asp:Button ID="btnfind" runat="server" OnClick="btnfind_Click" style="z-index: 1; left: 253px; top: 18px; position: absolute" Text="Find" />
        <asp:TextBox ID="txtemail" runat="server" style="z-index: 1; left: 158px; top: 354px; position: absolute"></asp:TextBox>
        <asp:TextBox ID="txtuniversityname" runat="server" style="z-index: 1; left: 158px; top: 463px; position: absolute"></asp:TextBox>
        <asp:TextBox ID="txtinterest" runat="server" style="z-index: 1; left: 505px; top: 49px; position: absolute; width: 365px"></asp:TextBox>
        <asp:TextBox ID="txtuniversitycountry" runat="server" style="z-index: 1; left: 158px; top: 493px; position: absolute"></asp:TextBox>
        <asp:Label ID="Label15" runat="server" style="z-index: 1; left: 37px; top: 493px; position: absolute" Text="University Country"></asp:Label>
        <asp:Label ID="Label16" runat="server" style="z-index: 1; left: 52px; top: 462px; position: absolute" Text="University Name"></asp:Label>
        <asp:Label ID="Label17" runat="server" style="z-index: 1; left: 105px; top: 435px; position: absolute; right: 862px" Text="Stream"></asp:Label>
        <asp:TextBox ID="txtmobile" runat="server" style="z-index: 1; left: 158px; top: 326px; position: absolute"></asp:TextBox>
        <asp:TextBox ID="txteducation" runat="server" style="z-index: 1; left: 157px; top: 407px; position: absolute"></asp:TextBox>
        <asp:TextBox ID="txtfullname" runat="server" style="z-index: 1; left: 160px; top: 50px; position: absolute; width: 240px"></asp:TextBox>
        <asp:TextBox ID="txtzipcode" runat="server" style="z-index: 1; left: 158px; top: 273px; position: absolute"></asp:TextBox>
        <asp:TextBox ID="txtaddress" runat="server" style="z-index: 1; left: 159px; top: 137px; position: absolute"></asp:TextBox>
        <asp:TextBox ID="txtstreet" runat="server" style="z-index: 1; left: 158px; top: 221px; position: absolute"></asp:TextBox>
        <asp:TextBox ID="txtdbo" runat="server" style="z-index: 1; left: 158px; top: 107px; position: absolute"></asp:TextBox>
        <asp:TextBox ID="txtcountry" runat="server" style="z-index: 1; left: 159px; top: 166px; position: absolute"></asp:TextBox>
        <asp:DropDownList ID="ddledutype" runat="server" style="z-index: 1; left: 158px; top: 380px; position: absolute; bottom: 123px;">
            <asp:ListItem>Academy</asp:ListItem>
            <asp:ListItem>Non Academy</asp:ListItem>
        </asp:DropDownList>
        <asp:DropDownList ID="ddlstream" runat="server" style="z-index: 1; left: 158px; top: 435px; position: absolute">
        </asp:DropDownList>
        <asp:TextBox ID="TextBox1" runat="server" style="z-index: 1; left: 450px; top: 260px; position: absolute"></asp:TextBox>
        <asp:Label ID="Label18" runat="server" style="z-index: 1; left: 445px; top: 48px; position: absolute" Text="Interest*"></asp:Label>
        <asp:Label ID="Label19" runat="server" style="z-index: 1; left: 438px; top: 76px; position: absolute" Text="Remarks*"></asp:Label>
        <asp:Button ID="btncancle" runat="server" style="z-index: 1; left: 810px; top: 203px; position: absolute" Text="Cancle" />
        <asp:TextBox ID="txtregisterdate" runat="server" style="z-index: 1; left: 504px; top: 168px; position: absolute; width: 205px"></asp:TextBox>
        <asp:Label ID="lblerror" runat="server" style="z-index: 1; left: 306px; top: 22px; position: absolute"></asp:Label>
        <asp:Label ID="Label21" runat="server" style="z-index: 1; left: 414px; top: 169px; position: absolute; right: 514px" Text="Register Date"></asp:Label>
    
        <asp:Label ID="Label20" runat="server" style="z-index: 1; left: 79px; top: 23px; position: absolute" Text="CustomerId"></asp:Label>
    
    </div>
        <p>
            &nbsp;</p>
    </form>
</body>
</html>





FindCustomerDetails.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.SqlClient;
using System.Data;
namespace MyProject
{
    public partial class FindCustomerDetails : System.Web.UI.Page
    {
        SqlConnection con;
        public void DBConnection()
        {
            con = new SqlConnection("Server=Kool-PC;Database=CenterDB;user=sa;password=a;trusted_connection=yes");
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnfind_Click(object sender, EventArgs e)
        {
            if(txtcustid.Text.Trim().Equals(""))
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter customer id.')</script>");
                txtcustid.Focus();
            }
            else
            {
                getCustomerId();
            }
        }
        
        private void getCustomerId()
        {
            DBConnection();
            con.Open();
            try
            {
                SqlCommand cmd;
                cmd = new SqlCommand("Select * From CustomerDetails Where CustomerId='"+txtcustid.Text.Trim()+"'", con);
                SqlDataReader dr = cmd.ExecuteReader();
                if(dr.HasRows)
                {
                   dr.Read();
                   txtfullname.Text = dr["CustomerName"].ToString();
                   txtdbo.Text = dr["DateOfBirth"].ToString();
                   txtaddress.Text = dr["Address"].ToString();
                   txtcountry.Text = dr["Country"].ToString();
                   txtcity.Text = dr["City"].ToString();
                   txtstreet.Text = dr["Street"].ToString();
                   txtstat.Text = dr["Stat"].ToString();
                   txtzipcode.Text = dr["ZipCode"].ToString();
                   txtphone.Text = dr["Phone"].ToString();
                   txtmobile.Text = dr["Mobile"].ToString();
                   txtemail.Text = dr["Email"].ToString();
                   ddledutype.Text = dr["EducationType"].ToString();
                   txteducation.Text = dr["Education"].ToString();
                   ddlstream.Text = dr["Stream"].ToString();
                   txtuniversityname.Text = dr["UniversityName"].ToString();
                   txtuniversitycountry.Text = dr["UniversityCountry"].ToString();
                   txtinterest.Text = dr["Interest"].ToString();
                   txtremarks.Text = dr["Remarks"].ToString();
                    txtregisterdate.Text=dr["RegisterDateTime"].ToString();
                    ddlstream.Text = dr["Stream"].ToString();
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Id does not exist')</script>");
                    txtcustid.Focus();
                }
            }
            catch(Exception ex)
            {
                lblerror.Text = ex.Message;
            }
        }
        protected void btncancle_Click(object sender, EventArgs e)
        {
            txtfullname.Text = "";
            rmale.Checked = false;
            rfemale.Checked = false;
            rother.Checked = false;
            txtdbo.Text = "";
            txtaddress.Text = "";
            txtcountry.Text = "";
            txtcity.Text = "";
            txtstreet.Text = "";
            txtstat.Text = "";
            txtzipcode.Text = "";
            txtphone.Text = "";
            txtmobile.Text = "";
            txtemail.Text = "";
            txteducation.Text = "";
            txtuniversityname.Text = "";
            txtuniversitycountry.Text = "";
            txtinterest.Text = "";
            txtremarks.Text = "";
        }

      
    }
}














No comments:

Post a Comment

Advertisement Here