Asp.Net Tutorials And Project Code


SQL Tutorial Class

Saturday, April 4, 2020

how to get data from sql server in asp.net

How to get data from sql server in asp.net


To retrieve data/record from SQL database in asp.net steps

1. Create a table and insert value in Sql Database
Create table ItemDetails
(
ItemId int NOT NULL,
Name Varchar(30)NOT NULL,
Quantity int NOT NULL,
Price float NOT NULL,
Category varchar(30)NOT NULL,
Country varchar(20)NOT NULL,
Remarks Varchar(250)NULL
)



Insert into ItemDetails Values('1','Dell Desktop Computer','10','540','Computer','Japan',NULL)
Insert into ItemDetails Values('2','Dell Server','5','500','Computer','USA',NULL)
insert into ItemDetails Values('3','Acer Desktop Computer','50','200','Computer','Korea',NULL)
Insert into ItemDetails Values('4','Acer Laptop','20','250','Computer','Korea',NULL)
Insert into ItemDetails Values('5','Dell Laptop','300','150','Computer','USA',NULL)
Insert into ItemDetails Values('6','Apple Mac Book','40','600','Computer','USA',NULL)
insert into ItemDetails Values('7','Samsung Laptop','20','200','Computer','USA',NULL)
Insert into ItemDetails Values('8','MSI Desktop Computer','40','200','Computer','China',NULL)
Insert into ItemDetails Values('9','MSI Laptop','15','250','Computer','China',NULL)
Insert into ItemDetails Values('10','Toshiba','5','200','Computer','USA',NULL)



2. Create ASP.Net From like bellow:

[Note:
ItemCode to txtItemCode
ItemName to txtName
Category to txtCategory
Quantity to txtQty Price to txtPrice
Country to txtCountry
Desc to txtDesc
]

get value form database in C#


3. Type following code inside the button event:

   

protected void btnFind_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con;
                con = new SqlConnection("Server=Kool-PC;Database=CenterDB;user=sa;password=a;trusted_connection=yes");
                con.Open();
                SqlCommand cmd;
                cmd = new SqlCommand("Select ItemId,Name,Quantity,Price,Category,Country, Remarks From ItemDetails Where ItemId=@ItemCode", con);
                cmd.Parameters.Add("@ItemCode", SqlDbType.VarChar, 20).Value = txtItemCode.Text.Trim();
                SqlDataReader dr = cmd.ExecuteReader();
                if(dr.HasRows)
                {
                    dr.Read();
                    txtName.Text = dr["Name"].ToString();
                    txtCategory.Text=dr["Category"].ToString();
                    txtQty.Text=dr["Quantity"].ToString();
                    txtPrice.Text=dr["Price"].ToString();
                    txtCountry.Text=dr["Country"].ToString();
                    txtDesc.Text = dr["Remarks"].ToString();                 
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('ItemDoes not exist, choose correct ItemCode')</script>");
                }
            }
            catch(Exception ex)
            {
                lblerror.Text = ex.Message;
            }
        }

Thursday, March 19, 2020

Insert into sql database c#

Insert into sql database c#


Save data/records from web application. 

1. Create Table In SQL Server Database

Create Table ItemDetails
(
ItemCode Varchar(10)NOT NULL,
ItemName Varchar(30)NOT NULL,
Category Varchar(10)NOT NULL,
Quantity Int NOT NULL,
Price Float NOT NULL,
Country VarChar(30)NOT NULL,
Description VarChar(200)NOT NULL
)




2. Create a C#/Asp.net form as below image.
 TextBox Name as below or any your choice.

[Note
  ItemCode to txtItemCode
  ItemName to txtName
  Category to txtCategory
  Quantity to txtQty Price to txtPrice
  Country to txtCountry
  Desc to txtDesc
]


Insert into sql database from asp Dot net

3. Type the following code inside btnSave event

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 ItemEntry : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con;
                con = new SqlConnection("Server=Kool-PC;Database=CenterDB;user=sa;password=a;trusted_connection=yes");
                con.Open();
                SqlCommand cmd;
                cmd = new SqlCommand("Insert Into ItemDetails Values(@ItemCode,@ItemName,@Category,@Quantity,@Price,@Country,@Desc)", con);
                cmd.Parameters.Add("@ItemCode", SqlDbType.VarChar, 10).Value = txtItemCode.Text.Trim();
                cmd.Parameters.Add("@ItemName", SqlDbType.VarChar, 50).Value = txtName.Text.Trim();
                cmd.Parameters.Add("@Category", SqlDbType.VarChar, 20).Value = txtCategory.Text.Trim();
                cmd.Parameters.Add("@Quantity", SqlDbType.Int).Value = txtQty.Text.Trim();
                cmd.Parameters.Add("@Price", SqlDbType.Float).Value = txtPrice.Text.Trim();
                cmd.Parameters.Add("@Country", SqlDbType.VarChar, 30).Value = txtCountry.Text.Trim();
                cmd.Parameters.Add("@Desc", SqlDbType.VarChar, 200).Value = txtDesc.Text.Trim();
                cmd.ExecuteNonQuery();
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language="javascript">alert('Saved')</script>");

            }
            catch(Exception ex)
            {
                lblerror.Text = ex.Message;
               // Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language="javascript">alert('" + ex.Message + "')</script>");

            }
              
        }

    }
}





Insert into Sql database form asp.net in Video

Saturday, March 7, 2020

How to create login page in asp.net with database

How to create login page in asp.net with database.

1. Create a login form in asp.net. (See image.)
2. Create table in SQL.
3. Type Asp.Net C# Code in Button Click. (See code)


1. Asp.net Login Page

[Note: LoginId TextBox is: txtlogin.]
[Note: Password TextBox name is: txtpassword.] and
[Note: Login button name is: btnlogin]
How to create login page in asp.net with database


2. Create Login Table in SQL server

Create Table UserTable
(
UserId varchar(10),
Password Varchar(20)
)

Insert into UserTable Values('SMITH','12345')
Insert into UserTable values('ANU','5432')


3. Type or copy and paste below.
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 Login
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

    
        protected void btnlogin_Click(object sender, EventArgs e)
        {
            if(txtlogin.Text.Equals(""))
            {
                lblmsg.Text = "Enter Login Id";
                txtlogin.Focus();
                lblmsg.BackColor = System.Drawing.Color.Red;
            }
            else if(txtpassword.Text.Equals(""))
            {
                lblmsg.Text = "";
                lblmsg.Text = "Enter Password";
                txtpassword.Focus();
                lblmsg.BackColor = System.Drawing.Color.Red;
            }
            else
            {
                lblmsg.Text = "";
                lblmsg.BackColor = System.Drawing.Color.Wheat;
                checkLogin();
            }

        }
        protected void checkLogin()
        {
            SqlConnection con;
            con = new SqlConnection("Server=KOOL-PC;Database=CenterDB;user=sa;password=a;trusted_connection=yes");
            SqlCommand cmd = new SqlCommand("Select UserId From UserTable Where UserId=@UserId", con);
            cmd.Parameters.Add("@UserId", SqlDbType.VarChar, 10).Value = txtlogin.Text.Trim();
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if(dr.HasRows)
            {
                CheckUserAndPass();   
            }
            else
            {
                
                lblmsg.Text = "";
                lblmsg.Text = "User Id does not exist.";
                lblmsg.BackColor = System.Drawing.Color.Red;
                dr.Dispose();
                cmd.Dispose();
                con.Close();
            }

        }
      protected void CheckUserAndPass()
        {
            SqlConnection con;
            con = new SqlConnection("Server=KOOL-PC;Database=CenterDB;user=sa;password=a;trusted_connection=yes");
            SqlCommand cmd = new SqlCommand("Select UserId,Password From UserTable Where UserId=@UserId And Password=@Password",con);
            cmd.Parameters.Add("@UserId", SqlDbType.VarChar, 10).Value = txtlogin.Text.Trim();
            cmd.Parameters.Add("@Password", SqlDbType.VarChar, 20).Value = txtpassword.Text.Trim();
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
          if(dr.HasRows)
          {
              lblmsg.Text = "Login Success...";
              lblmsg.BackColor = System.Drawing.Color.White;
          }
          else
          {
              lblmsg.Text = "Login unsuccess";
              lblmsg.BackColor = System.Drawing.Color.Red;
          }
        }
    }
}


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 = "";
        }

      
    }
}














Update record in sql server using c#

Update record in SQL Server using C#:

Asp.Net Form Design:
update record in sql server using c#




Asp.Net Form Design Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ModifyCustomerDetails.aspx.cs" Inherits="MyProject.ModifyCustomerDetails" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Button ID="btnsave" runat="server" Enabled="False" style="z-index: 1; left: 733px; top: 202px; position: absolute; height: 29px; width: 55px;" Text="Save" OnClick="btnsave_Click" />
    
        <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" GroupName="gender" />
        <asp:RadioButton ID="rfemale" runat="server" style="z-index: 1; left: 222px; top: 79px; position: absolute" Text="Female" GroupName="gender" />
        <asp:RadioButton ID="rmale" runat="server" style="z-index: 1; left: 157px; top: 77px; position: absolute" Text="Male" GroupName="gender" />
        <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;" OnSelectedIndexChanged="ddledutype_SelectedIndexChanged">
            <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" OnSelectedIndexChanged="ddlstream_SelectedIndexChanged">
            <asp:ListItem></asp:ListItem>
            <asp:ListItem>Science</asp:ListItem>
            <asp:ListItem>Engineering</asp:ListItem>
            <asp:ListItem>Education</asp:ListItem>
            <asp:ListItem>Humanities</asp:ListItem>
            <asp:ListItem>Education</asp:ListItem>
        </asp:DropDownList>
        <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:Label ID="lblerror" runat="server" style="z-index: 1; left: 306px; top: 22px; position: absolute"></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>





Asp.net ModifyCustomerDetails.aspx.cs Code
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 ModifyCustomerDetails : 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();
                    if(dr["Gender"].ToString()=="Male")
                    {
                        rmale.Checked = true;
                        rfemale.Checked = false;
                        rother.Checked = false;
                    }
                    else if(dr["Gender"].ToString()=="Female")
                    {
                        rmale.Checked = false;
                        rfemale.Checked = true;
                        rother.Checked = false;
                    }
                    else if (dr["Gender"].ToString() == "Other")
                    {
                        rmale.Checked = false;
                        rfemale.Checked = false;
                        rother.Checked = true;
                    }
                    else
                    {
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Undefined gender.')</script>");
                        return;
                    }
                   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();
                   
                    btnsave.Enabled = true;
                    
                }
                else
                {
                    btnsave.Enabled = false;
                    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 = "";
        }

        protected void btnsave_Click(object sender, EventArgs e)
        {
            if (txtfullname.Text.Trim().Equals(""))
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter Full Name.')</script>");
                txtfullname.Focus();
            }
            else if ((rmale.Checked = false) && (rfemale.Checked = false) && (rother.Checked = false))
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Select Gender.')</script>");
            }
            else if (txtdbo.Text.Trim().Equals(""))
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter Date Of Birth.')</script>");
                txtdbo.Focus();
            }
            else if (txtaddress.Text.Trim().Equals(""))
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter Address.')</script>");
                txtaddress.Focus();
            }
            else if (txtcountry.Text.Trim().Equals(""))
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter Country Name.')</script>");
                txtcountry.Focus();
            }
            else if (txtcity.Text.Trim().Equals(""))
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter City.')</script>");
                txtcity.Focus();
            }
            else if (txtstreet.Text.Trim().Equals(""))
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter Street.')</script>");
                txtstreet.Focus();
            }
            else if (txtstat.Text.Trim().Equals(""))
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter Stat.')</script>");
                txtstat.Focus();
            }
            else if (txtzipcode.Text.Trim().Equals(""))
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter ZipCode.')</script>");
                txtzipcode.Focus();
            }
            else if (txtmobile.Text.Trim().Equals(""))
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter mobile no.')</script>");
                txtmobile.Focus();
            }
            else if (txtemail.Text.Trim().Equals(""))
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter Email Address.')</script>");
                txtemail.Focus();
            }
            else if (txtinterest.Text.Trim().Equals(""))
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter Interest.')</script>");
                txtinterest.Focus();
            }
            else if ((txtremarks.Text.Trim().Equals("")) || (txtremarks.Text.Trim().Length <= 10))
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter Remarks.')</script>");
                txtremarks.Focus();
            }
            else if (ddledutype.SelectedIndex == 0)
            {
                if (txteducation.Text.Trim().Equals(""))
                {
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter Education.')</script>");
                    txteducation.Focus();
                }
                else if (ddlstream.SelectedIndex == -1)
                {
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Select Stream.')</script>");
                    ddlstream.Focus();
                }
                else if (txtuniversityname.Text.Trim().Equals(""))
                {
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter University Name.')</script>");
                    txtuniversityname.Focus();
                }
                else if (txtuniversitycountry.Text.Trim().Equals(""))
                {
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter University Country Name.')</script>");
                    txtuniversitycountry.Focus();
                }
                else
                {
                    updateRecord();
                }
            }

            else
            {
                //Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Calling to insert.')</script>");

                updateRecord();
            }
        }
        protected void updateRecord()
        {

            try
            {
                DBConnection();
                con.Open();
                SqlCommand cmd;
                cmd = new SqlCommand("Update CustomerDetails SET CustomerName=@fullname,Gender=@gender,DateOfBirth=@dob,Address=@address,Country=@country,City=@city,Street=@street,Stat=@stat,ZipCode=@zipcode,Phone=@phone,Mobile=@mobile,Email=@email,EducationType=@ddlacademytype,Education=@education,Stream=@ddlstream,UniversityName=@universityname,UniversityCountry=@universitycountry,Interest=@interest,Remarks=@remarks Where CustomerId=@CustomerId", con);
                cmd.Parameters.Add("@CustomerId", SqlDbType.Int).Value = txtcustid.Text.Trim();
                cmd.Parameters.Add("@fullname", SqlDbType.VarChar, 50).Value = txtfullname.Text.Trim();
                if((rfemale.Checked==false)&&(rother.Checked==false)&&(rmale.Checked==true))
                {
                    cmd.Parameters.Add("@gender", SqlDbType.VarChar, 10).Value = rmale.Text.Trim();
                    lblerror.Text = rmale.Text;
                }
                else if ((rmale.Checked == false) && (rfemale.Checked == true) && (rother.Checked == false))
                {
                    cmd.Parameters.Add("@gender", SqlDbType.VarChar, 10).Value = rfemale.Text.Trim();
                    lblerror.Text = rfemale.Text;
                }
                else if ((rmale.Checked == false) && (rfemale.Checked == false) && (rother.Checked == true))
                {
                    cmd.Parameters.Add("@gender", SqlDbType.VarChar, 10).Value = rother.Text.Trim();
                    lblerror.Text = rother.Text;
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Unspecified Gender.')</script>");
                    return;
                }
                cmd.Parameters.Add("@dob", SqlDbType.Date).Value = txtdbo.Text.Trim();
                cmd.Parameters.Add("@address", SqlDbType.VarChar, 100).Value = txtaddress.Text.Trim();
                cmd.Parameters.Add("@country", SqlDbType.VarChar, 50).Value = txtcountry.Text.Trim();
                cmd.Parameters.Add("@city", SqlDbType.VarChar, 50).Value = txtcity.Text.Trim();
                cmd.Parameters.Add("@street", SqlDbType.VarChar, 50).Value = txtstreet.Text.Trim();
                cmd.Parameters.Add("@stat", SqlDbType.VarChar, 50).Value = txtstat.Text.Trim();
                cmd.Parameters.Add("@zipcode", SqlDbType.VarChar, 50).Value = txtzipcode.Text.Trim();
                cmd.Parameters.Add("@phone", SqlDbType.VarChar, 50).Value = txtphone.Text.Trim();
                cmd.Parameters.Add("@mobile", SqlDbType.VarChar, 50).Value = txtmobile.Text.Trim();
                cmd.Parameters.Add("@email", SqlDbType.VarChar, 50).Value = txtemail.Text.Trim();

                if (ddledutype.SelectedIndex == 0)
                {
                    cmd.Parameters.Add("@ddlacademytype", SqlDbType.VarChar, 50).Value = ddledutype.Text.Trim();
                    cmd.Parameters.Add("@education", SqlDbType.VarChar, 50).Value = txteducation.Text.Trim();
                    cmd.Parameters.Add("@ddlstream", SqlDbType.VarChar, 50).Value = ddlstream.Text.Trim();
                    cmd.Parameters.Add("@universityname", SqlDbType.VarChar, 50).Value = txtuniversityname.Text.Trim();
                    cmd.Parameters.Add("@universitycountry", SqlDbType.VarChar, 50).Value = txtuniversitycountry.Text.Trim();
                }
                else if (ddledutype.SelectedIndex == 1)
                {
                    cmd.Parameters.Add("@ddlacademytype", SqlDbType.VarChar, 50).Value = ddledutype.Text.Trim();
                    cmd.Parameters.Add("@education", SqlDbType.VarChar, 50).Value = DBNull.Value;
                    cmd.Parameters.Add("@ddlstream", SqlDbType.VarChar, 50).Value = DBNull.Value;
                    cmd.Parameters.Add("@universityname", SqlDbType.VarChar, 50).Value = DBNull.Value;
                    cmd.Parameters.Add("@universitycountry", SqlDbType.VarChar, 50).Value = DBNull.Value;
                }
                else
                {

                }
                cmd.Parameters.Add("@interest", SqlDbType.VarChar, 100).Value = txtinterest.Text.Trim();
                cmd.Parameters.Add("@remarks", SqlDbType.VarChar, 200).Value = txtremarks.Text.Trim();            
                int count = 0;
                if ((count = cmd.ExecuteNonQuery()) == 1)
                {
                     Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Successfuly modified.')</script>");                
                }

            }
            catch (Exception ex)
            {
                txtfullname.Text = ex.Message;
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('" + ex.Message + "')</script>");
            }
        }

        protected void ddlstream_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddledutype_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddledutype.SelectedIndex == 1)
            {
                txteducation.Enabled = false;
                ddlstream.Enabled = false;
                txtuniversityname.Enabled = false;
                txtuniversitycountry.Enabled = false;
            }
            else
            {
                txteducation.Enabled = true;
                ddlstream.Enabled = true;
                txtuniversityname.Enabled = true;
                txtuniversitycountry.Enabled = true;
            }
        }

        
    }
}



































how to insert record in sql database using asp.net c#

Insert into SQL Server database from asp.net:

1. Create form. (Like picture)
2. Connect to SQL Server Database.

Asp.net Insert form.

Insert, Update, Delete, Display in Asp.net


Insert Design Code:


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



<!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="lblerror" runat="server"></asp:Label>

   

        <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="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" AutoPostBack="True" OnSelectedIndexChanged="ddledutype_SelectedIndexChanged">

            <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:ListItem>Science</asp:ListItem>

            <asp:ListItem>Engineering</asp:ListItem>

            <asp:ListItem>Education</asp:ListItem>

            <asp:ListItem>Humanities</asp:ListItem>

            <asp:ListItem>Education</asp:ListItem>

        </asp:DropDownList>

        <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" OnClick="btncancle_Click" style="z-index: 1; left: 744px; top: 195px; position: absolute" Text="Cancle" />

        <asp:Button ID="btnsend" runat="server" OnClick="btnsend_Click" style="z-index: 1; left: 681px; top: 194px; position: absolute" Text="Send" />

   

    </div>

    </form>

</body>

</html>







CustomerDetailsForm.aspx

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 CustomerDetailsForm : System.Web.UI.Page

    {

        SqlConnection con;

        public void DBConnection()

        {

            try

            {

                con = new SqlConnection("Server=Kool-PC;Database=CenterDB;user=sa;password=a;trusted_connection=yes");

              

            }

            catch(Exception ex)

            {

                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('"+ex.Message+"')</script>");

            }

        }

        protected void Page_Load(object sender, EventArgs e)

        {



        }



        protected void btnsend_Click(object sender, EventArgs e)

        {

            if(txtfullname.Text.Trim().Equals(""))

            {

                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter Full Name.')</script>");

                txtfullname.Focus();

            }

            else if ((rmale.Checked=false) && (rfemale.Checked=false)&&(rother.Checked=false))

            {

                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Select Gender.')</script>");

            }

            else if (txtdbo.Text.Trim().Equals(""))

            {

                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter Date Of Birth.')</script>");

                txtdbo.Focus();

            }

            else if(txtaddress.Text.Trim().Equals(""))

            {

                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter Address.')</script>");

                txtaddress.Focus();

            }

            else if(txtcountry.Text.Trim().Equals(""))

            {

                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter Country Name.')</script>");

                txtcountry.Focus();

            }

            else if(txtcity.Text.Trim().Equals(""))

            {

                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter City.')</script>");

                txtcity.Focus();

            }

            else if(txtstreet.Text.Trim().Equals(""))

            {

                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter Street.')</script>");

                txtstreet.Focus();

            }

            else if (txtstat.Text.Trim().Equals(""))

            {

                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter Stat.')</script>");

                txtstat.Focus();

            }

            else if(txtzipcode.Text.Trim().Equals(""))

            {

                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter ZipCode.')</script>");

                txtzipcode.Focus();

            }

            else if (txtmobile.Text.Trim().Equals(""))

            {

                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter mobile no.')</script>");

                txtmobile.Focus();

            }

            else if(txtemail.Text.Trim().Equals(""))

            {

                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter Email Address.')</script>");

                txtemail.Focus();

            }

            else if(txtinterest.Text.Trim().Equals(""))

            {

                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter Interest.')</script>");

                txtinterest.Focus();

            }

            else if((txtremarks.Text.Trim().Equals(""))||(txtremarks.Text.Trim().Length<=10))

            {

                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter Remarks.')</script>");

                txtremarks.Focus();

            }

            else if(ddledutype.SelectedIndex==0)

            {            

                if(txteducation.Text.Trim().Equals(""))

                {

                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter Education.')</script>");

                    txteducation.Focus();

                }

                else if(ddlstream.SelectedIndex==-1)

                {

                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Select Stream.')</script>");

                    ddlstream.Focus();

                }

                else if(txtuniversityname.Text.Trim().Equals(""))

                {

                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter University Name.')</script>");

                    txtuniversityname.Focus();

                }

                else if(txtuniversitycountry.Text.Trim().Equals(""))

                {

                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Enter University Country Name.')</script>");

                    txtuniversitycountry.Focus();

                }

                else

                {

                    insertRecord();

                }

            }

           

            else

            {

                //Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Calling to insert.')</script>");



                insertRecord();

            }

        }

        protected void insertRecord()

        {

           

            try

            {

                DBConnection();

                con.Open();

                SqlCommand cmd;

                cmd = new SqlCommand("Insert Into CustomerDetails Values(@fullname,@gender,@dob,@address,@country,@city,@street,@stat,@zipcode,@phone,@mobile,@email,@ddlacademytype,@education,@ddlstream,@universityname,@universitycountry,@interest,@remarks,@registerdate,@registerdatetime)", con);

                cmd.Parameters.Add("@fullname", SqlDbType.VarChar, 50).Value = txtfullname.Text.Trim();

                cmd.Parameters.Add("@gender", SqlDbType.VarChar, 10).Value = rmale.Text.Trim();

                cmd.Parameters.Add("@dob", SqlDbType.Date).Value =txtdbo.Text.Trim();

                cmd.Parameters.Add("@address", SqlDbType.VarChar, 100).Value =txtaddress.Text.Trim();

                cmd.Parameters.Add("@country", SqlDbType.VarChar, 50).Value = txtcountry.Text.Trim();

                cmd.Parameters.Add("@city", SqlDbType.VarChar, 50).Value = txtcity.Text.Trim();

                cmd.Parameters.Add("@street", SqlDbType.VarChar, 50).Value = txtstreet.Text.Trim();

                cmd.Parameters.Add("@stat", SqlDbType.VarChar, 50).Value = txtstat.Text.Trim();

                cmd.Parameters.Add("@zipcode", SqlDbType.VarChar, 50).Value = txtzipcode.Text.Trim();

                cmd.Parameters.Add("@phone", SqlDbType.VarChar, 50).Value = txtphone.Text.Trim();

                cmd.Parameters.Add("@mobile", SqlDbType.VarChar, 50).Value = txtmobile.Text.Trim();

                cmd.Parameters.Add("@email", SqlDbType.VarChar, 50).Value = txtemail.Text.Trim();



                if(ddledutype.SelectedIndex==0)

                {

                    cmd.Parameters.Add("@ddlacademytype", SqlDbType.VarChar, 50).Value = ddledutype.Text.Trim();

                    cmd.Parameters.Add("@education", SqlDbType.VarChar, 50).Value = txteducation.Text.Trim();

                    cmd.Parameters.Add("@ddlstream", SqlDbType.VarChar, 50).Value = ddlstream.Text.Trim();

                    cmd.Parameters.Add("@universityname", SqlDbType.VarChar, 50).Value = txtuniversityname.Text.Trim();

                    cmd.Parameters.Add("@universitycountry", SqlDbType.VarChar, 50).Value = txtuniversitycountry.Text.Trim();

                }

                else if (ddledutype.SelectedIndex==1)

                {

                    cmd.Parameters.Add("@ddlacademytype", SqlDbType.VarChar, 50).Value = ddledutype.Text.Trim();

                    cmd.Parameters.Add("@education", SqlDbType.VarChar, 50).Value = DBNull.Value;

                    cmd.Parameters.Add("@ddlstream", SqlDbType.VarChar, 50).Value = DBNull.Value;

                    cmd.Parameters.Add("@universityname", SqlDbType.VarChar, 50).Value = DBNull.Value;

                    cmd.Parameters.Add("@universitycountry", SqlDbType.VarChar, 50).Value = DBNull.Value;

                }

                else

                {



                }

                cmd.Parameters.Add("@interest", SqlDbType.VarChar, 100).Value =txtinterest.Text.Trim();

                cmd.Parameters.Add("@remarks", SqlDbType.VarChar, 200).Value = txtremarks.Text.Trim();

                cmd.Parameters.Add("@registerdate", SqlDbType.Date).Value =DateTime.Now;

                cmd.Parameters.Add("@registerdatetime", SqlDbType.DateTime).Value = DateTime.Now;

                int count=0;

                if ((count = cmd.ExecuteNonQuery())==1)

                    {

                       // Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Successfuly Send.')</script>");

                        getCustomerId();

                    }

                  

            }

            catch(Exception ex)

            {

                txtfullname.Text = ex.Message;

                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('"+ex.Message+"')</script>");

            }

        }

        private void getCustomerId()

        {

            DBConnection();

            con.Open();

            try

            {

                SqlCommand cmd;

                cmd = new SqlCommand("Select Max(CustomerId)As CustomerId From CustomerDetails", con);

                SqlDataReader dr = cmd.ExecuteReader();

                if(dr.HasRows)

                {

                   dr.Read();

                   string strcustomerid=string.Empty;

                   strcustomerid = dr["CustomerId"].ToString();

                   Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('Successfully Sent, Your Id Is: " + strcustomerid.ToString() + "')</script>");

                   lblerror.Text = strcustomerid.ToString();

                   //Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('New Customer Id is: " + strcustomerid + "')</script>");

                }

            }

            catch(Exception ex)

            {

                lblerror.Text = ex.Message;

                //Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language = 'javascript'>alert('"+ex.Message+"')</script>");

            }

        }

        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 = "";

        }



        protected void ddledutype_SelectedIndexChanged(object sender, EventArgs e)

        {

            if (ddledutype.SelectedIndex == 1)

            {

                txteducation.Enabled = false;

                ddlstream.Enabled = false;

                txtuniversityname.Enabled = false;

                txtuniversitycountry.Enabled = false;

            }

            else

            {

                txteducation.Enabled = true;

                ddlstream.Enabled = true;

                txtuniversityname.Enabled = true;

                txtuniversitycountry.Enabled = true;

            }

        }

    }

}

Wednesday, January 29, 2020

Asp net

Asp.Net is a server side web-application framework. It designed for a web application. It allows to create dynamic web page. Asp.Net
Advertisement Here