Thursday 11 July 2013

How to create Web application with Basic Operations like SAVE , UPDATE , DELETE ?


Open Visual Studio and follow the below steps to open a web application.

The Steps are:-

1)      Go to Menu Bar and Click on “File” Menu.
2)      Click on “New”.
3)      Click on “Project”. Then a window will appear.
4)      Click on Visual C# and expand it.
5)      Select “web” option from Visual C#.
6)      Select ASP.NET Web Application.
7)      Give the name of the web application and also the directory to store the web application.
8)      Then a solution is created for that web application.

Agenda:

                   The main agenda of this application is to save, update, delete student name, class and whenever we saved the record that student name must present in a dropdown list. When we select student name in dropdown list, then student name, class must be displayed in the textboxes present.


using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

namespace StudentDetails
{
    public partial class _Default : System.Web.UI.Page
    {
        //below line is used to establish connection to database
        SqlConnection con = new SqlConnection("Data Source=*******; Initial Catalog=********; user id=********; password=******");
        protected void Page_Load(object sender, EventArgs e)
        {
            //we have to write !IsPostBack in page load so that when the page is loaded for first time,the method will be called.
         
            if (!IsPostBack)
            {
                //here the method dropdownddl will be called only ones,that is at the time of page loading
                dropdownddl();
            }
        }

        public void dropdownddl()
        {
            try
            {
                //dsdropdownddl is the object created for the DataSet
                DataSet dsdropdownddl = new DataSet();
                //Opening the Sql Server Connection
                con.Open();
                //ad is the object created for SqlDataAdapter to execute the sql command
                SqlDataAdapter ad = new SqlDataAdapter("select * from Studentdetails1tier", con);
                //here data is filled in the DataSet
                ad.Fill(dsdropdownddl);
                ddlSelectstudent.DataSource = dsdropdownddl;
                ddlSelectstudent.DataTextField = "StudentName";
                ddlSelectstudent.DataValueField = "S_ID";
                ddlSelectstudent.DataBind();
                ddlSelectstudent.Items.Insert(0, new ListItem("--Select--", "0"));
                //Closing the Sql Server Connection
                con.Close();
            }
            catch (Exception ex)
            {
            }
        }


        protected void ddlSelectstudent_SelectedIndexChanged(object sender, EventArgs e)
        {
            //we select the dropdown list value and convert it into integer and assign the value to S_ID
            int S_ID = Convert.ToInt32(ddlSelectstudent.SelectedValue);
            //Opening the Sql Server Connection
            con.Open();
            //creating the object for SqlCommand and writing the select query
            SqlCommand studentcmd = new SqlCommand("select StudentName,Class from Studentdetails1tier where S_ID=" + S_ID, con);
            //creating object for SqlDataAdapter and reading data from SqlCommand
            SqlDataAdapter studentda = new SqlDataAdapter(studentcmd);
            //creating an object for DataSet
            DataSet studentds = new DataSet();
            //Filling data in DataSet from dataAdapter
            studentda.Fill(studentds);
            //Closing the Sql Server Connection
            con.Close();
            //to show the student name and class for particular Student name
            txtStudentname.Text = studentds.Tables[0].Rows[0][0].ToString();
            txtClass.Text = studentds.Tables[0].Rows[0][1].ToString();
        }
        void Clear()
        {
            try
            {
                //dropdown method is called to bind the student name in the dropdown list
                dropdownddl();
                //After saving the data,to show the data in the textboxes as empty we place null in both the Textboxes
                txtStudentname.Text = null;
                txtClass.Text = null;
            }
            catch (Exception ex)
            {

            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //here the object is created for SqlCommand and Insert Query is written in the object to insert data in the Sql Server
                SqlCommand cmdsave = new SqlCommand("INSERT INTO Studentdetails1tier VALUES('" + txtStudentname.Text + "','" + txtClass.Text + "')", con);
                //Opening a connection for Sql Server
                con.Open();
                //Executing the Insert Query
                cmdsave.ExecuteNonQuery();
                //Closing the Sql Server Connection
                con.Close();
                //Clear method is called
                Clear();
            }
            catch (Exception ex)
            {
           
            }
        }
 

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                //creating object for SqlCommand and writing Update query
                SqlCommand cmdupdate = new SqlCommand("UPDATE Studentdetails1tier SET StudentName='" + txtStudentname.Text + "',Class='" + txtClass.Text + "' WHERE S_ID='" + Convert.ToInt32(ddlSelectstudent.SelectedValue) + "'", con);
                //Opening a connection for Sql Server
                con.Open();
                //Executing the Update query
                cmdupdate.ExecuteNonQuery();
                //Closing the Sql Server Connection
                con.Close();
                //calling clear method
                Clear();
            }
            catch (Exception ex)
            {

            }
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                //creating object for SqlCommand and writing Delete query
                SqlCommand cmddelete = new SqlCommand("DELETE FROM Studentdetails1tier WHERE S_ID='" + Convert.ToInt32(ddlSelectstudent.SelectedValue) + "'", con);
                //Opening a connection for Sql Server
                con.Open();
                //Executing the Delete query
                cmddelete.ExecuteNonQuery();
                //Closing the Sql Server Connection
                con.Close();
                //calling clear method
                Clear();
            }
            catch (Exception ex)
            {
                   
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            //calling clear method
            Clear();
        }   
    }
}



No comments:

Post a Comment