Tutorials, Free Online Tutorials,It Challengers provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, core java, sql, php, c language etc. for beginners and professionals.

Breaking

ADT Program for Add ,Delete & Update records

 test.aspx file

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="onselectedDDL">
        </asp:DropDownList>
   
    </div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <br />
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    <br />
    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
    <br />
    <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
    <br />
    <asp:Button ID="Button1" runat="server" Text="Delete" OnClick="btnDelete_Click" />
    <asp:Button ID="Button2" runat="server" Text="Insert" OnClick="btnAdd_Click" />
    <asp:Button ID="Button3" runat="server" Text="Update" OnClick="btnEdit_Click" />
    <asp:Label ID="Label1" runat="server"></asp:Label>
    </form>
</body>
</html>


test.aspx.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Data
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        SqlConnection conn;
        string constr;
        protected void Page_Load(object sender, EventArgs e)
        {
            conn = new SqlConnection();
            conn.ConnectionString = ("Data Source=Anee;Initial Catalog=test;Integrated Security=True");
            constr = conn.ConnectionString;
            if(!this.IsPostBack)
            {
                this.populateDDL();
            }
        }
        private void populateDDL()
        {
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("select id,name from customer2"))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = con;
                    con.Open();
                    DropDownList1.DataSource = cmd.ExecuteReader();
                    DropDownList1.DataTextField="name";
                    DropDownList1.DataValueField = "id";
                    DropDownList1.DataBind();
                    con.Close();

                }
            }
            DropDownList1.Items.Insert(0, new ListItem("--select name--", "0"));
        }


        protected void onselectedDDL(object sender,EventArgs e)
        {
            if (this.DropDownList1.SelectedItem.Value != "0")
            {
                //string constr = ConfigurationManager.ConnectionStrings["DataDemoConn"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand("SELECT id,name,age,salary FROM customer2 WHERE id = @id", con))
                    {
                        using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                        {
                            cmd.Parameters.AddWithValue("@id", this.DropDownList1.SelectedItem.Value);
                            con.Open();
                            SqlDataReader dr = cmd.ExecuteReader();
                            //object address = cmd.ExecuteScalar();
                            while (dr.Read())
                            {
                                string id = dr["id"].ToString();
                                string name = dr["name"].ToString();
                                string ag = dr["age"].ToString();
                                string s = dr["salary"].ToString();
                                //string cat = dr["Category"].ToString();
                                TextBox1.Text = id;
                                TextBox2.Text = name;
                                TextBox3.Text = ag;
                                TextBox4.Text = s;
                                //TextBox5.Text=cat;
                            }
                            DropDownList1.DataBind();
                            con.Close();
                        }
                    }
                }
            }

            else
            {
                this.TextBox1.Text = "Please Select Name from List";
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            InsertProduct();
        }
        private void InsertProduct()
        {
            using (SqlConnection con = new SqlConnection(constr))
            {
                string id = TextBox1.Text.ToString();
                string name = TextBox2.Text.ToString();
                string ag = TextBox3.Text.ToString();
                string s = TextBox4.Text.ToString();
                //string cat = TextBox5.Text.ToString();
                SqlCommand cmd = new SqlCommand("INSERT INTO customer2 Values (" + id + ", '" + name + "'," + ag + ",'" + s + "')", con);

                con.Open();
                cmd.ExecuteNonQuery();
                DropDownList1.DataBind();
                con.Close();
                //Response.Write("<h4>Record Inserted Successfully...</h4>");
                Label1.Text = "Record Inserted Successfully...";
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection(constr))
            {
                SqlCommand cmd = new SqlCommand("delete from customer2 where id='" + DropDownList1.SelectedValue + "'", con);
                con.Open();
                //cmd.CommandText = "delete from cv_langues where id='" + DropDownList7.SelectedValue + "'";
                cmd.ExecuteNonQuery();
                DropDownList1.DataBind();
                con.Close();
                //Response.Write("<h4>Record Deleted Successfully...</h4>");
                Label1.Text = "Record Deleted Successfully...";
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection(constr))
            {
                string id = TextBox1.Text.ToString();
                string name = TextBox2.Text.ToString();
                string ag = TextBox3.Text.ToString();
                string s = TextBox4.Text.ToString();
               // string cat = TextBox5.Text.ToString();
                SqlCommand cmd = new SqlCommand("update customer2 set Name='" + name + "',age=" + ag + ",salary='" + s + "' where id=" + DropDownList1.SelectedValue, con);
                con.Open();
                cmd.ExecuteNonQuery();
                DropDownList1.DataBind();
                con.Close();
                //Response.Write("<h4>Record Updated Successfully...</h4>");
                Label1.Text = "Record Updated Successfully...";
            }
        }

    }
}

No comments:

Post a Comment