Ad Code

Responsive Advertisement

Ticker

6/recent/ticker-posts

ASP.NET CRUD Operation

         protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {    

                LoadData();

            }

        }

        


        protected void Button1_Click(object sender, EventArgs e)

        {            

            SqlConnection con = new SqlConnection("Data Source=ASUS-VIVOBOOK;Integrated Security=true;Initial Catalog=SurajDB");

            con.Open();

            SqlCommand cmd = new SqlCommand("insert into Student_Info values('"+tb_id.Text+ "','"+ tb_name.Text+ "','"+DropDownList1.SelectedValue+ "','"+tb_age.Text+ "','"+tb_contact.Text+"')",con);    

            cmd.ExecuteNonQuery();

            con.Close();

            //ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Insert Successfull');",true);

            Response.Write("<script>alert('Created');</script>");

            LoadData();


        }


        void LoadData()

        {

            SqlConnection con = new SqlConnection("Data Source=ASUS-VIVOBOOK;Integrated Security=true;Initial Catalog=SurajDB");

            con.Open();

            SqlCommand cmd = new SqlCommand("select * from Student_Info", con);

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);

            DataTable dataTable = new DataTable();

            adapter.Fill(dataTable);

            GridView1.DataSource = dataTable;

            GridView1.DataBind();

            

        }


        protected void UpdateRecord(object sender, EventArgs e)

        {

            SqlConnection con = new SqlConnection("Data Source=ASUS-VIVOBOOK;Integrated Security=true;Initial Catalog=SurajDB");

            con.Open();

            SqlCommand cmd = new SqlCommand("update Student_Info set Name = '"+tb_name.Text +"',Address = '"+DropDownList1.SelectedValue+"', Age = '"+tb_age.Text + "',Contact = '"+tb_contact.Text + "' where Id = '"+tb_id.Text+"'", con);

            cmd.ExecuteNonQuery();

            con.Close();

            Response.Write("<script>alert('Updated');</script>");

            LoadData();

        }


        protected void DeleteRecord(object sender, EventArgs e)

        {

            SqlConnection con = new SqlConnection("Data Source=ASUS-VIVOBOOK;Integrated Security=true;Initial Catalog=SurajDB");

            con.Open();

            SqlCommand cmd = new SqlCommand("delete Student_Info where Id = '" + tb_id.Text + "'", con);

            cmd.ExecuteNonQuery();

            con.Close();

            Response.Write("<script>alert('Deleted');</script>");

            LoadData();

        }

Post a Comment

0 Comments

Ad Code

Responsive Advertisement