Asp.Net Tutorials And Project Code


SQL Tutorial Class

Sunday, February 16, 2020

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

        
    }
}



































No comments:

Post a Comment

Advertisement Here