Programming for Everybody: ASP.NET C# insert update delete using SQL database and Load data in Gridview (WITH CODE)

ASP.NET C# insert update delete using SQL database and Load data in Gridview (WITH 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.Configuration;

public partial class _Default : System.Web.UI.Page

{

    public SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["webaspConnectionString"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)

    {

        GridView1.DataBind();

    }


    protected void Button1_Click(object sender, EventArgs e)

    {

        SqlCommand cmd = new SqlCommand("Insert Into Table1(firstname,lastname,marks)Values(@firstname,@lastname,@marks)", conn);

        cmd.Parameters.AddWithValue("firstname", ftxt.Text.Trim());

        cmd.Parameters.AddWithValue("lastname", ltxt.Text.Trim());

        cmd.Parameters.AddWithValue("marks", mtxt.Text.Trim());

        conn.Open();

        cmd.ExecuteNonQuery();

        conn.Close();

        result_lbl.Visible = true;

        result_lbl.ForeColor = System.Drawing.Color.Green;

        result_lbl.Text = "Data Inserted Successfully";

        GridView1.DataBind();

    }


    protected void Button2_Click(object sender, EventArgs e)

    {

        ftxt.Text = "";

        ltxt.Text = "";

        mtxt.Text = "";

        id_lbl.Text = "";

        result_lbl.Text = "";

    }


    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)

    {

        id_lbl.Text = GridView1.SelectedRow.Cells[0].Text.ToString();

        ftxt.Text = GridView1.SelectedRow.Cells[1].Text.ToString();

        ltxt.Text = GridView1.SelectedRow.Cells[2].Text.ToString();

        mtxt.Text = GridView1.SelectedRow.Cells[3].Text.ToString();

    }


    protected void Button3_Click(object sender, EventArgs e)

    {

        SqlCommand cmd = new SqlCommand("Update Table1 Set firstname=@firstname,lastname=@lastname,marks=@marks Where id=@id", conn);

        cmd.Parameters.AddWithValue("firstname", ftxt.Text.Trim());

        cmd.Parameters.AddWithValue("lastname", ltxt.Text.Trim());

        cmd.Parameters.AddWithValue("marks", mtxt.Text.Trim());

        cmd.Parameters.AddWithValue("id", id_lbl.Text.Trim());

        conn.Open();

        cmd.ExecuteNonQuery();

        conn.Close();

        result_lbl.Visible = true;

        result_lbl.ForeColor = System.Drawing.Color.Green;

        result_lbl.Text = "Data Updated Successfully";

        GridView1.DataBind();

    }


    protected void Button4_Click(object sender, EventArgs e)

    {

        SqlCommand cmd = new SqlCommand("Delete From Table1 Where id=@id", conn);

                cmd.Parameters.AddWithValue("id", id_lbl.Text.Trim());

        conn.Open();

        cmd.ExecuteNonQuery();

        conn.Close();

        result_lbl.Visible = true;

        result_lbl.ForeColor = System.Drawing.Color.Red;

        result_lbl.Text = "Data Deleted Successfully";

        GridView1.DataBind();

        ftxt.Text = "";

        ltxt.Text = "";

        mtxt.Text = "";

        id_lbl.Text = "";

        

    }


No comments:

Post a Comment