Asp.Net Tutorials And Project Code


SQL Tutorial Class

Sunday, February 16, 2020

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;

            }

        }

    }

}

No comments:

Post a Comment

Advertisement Here