[go: up one dir, main page]

0% found this document useful (0 votes)
7 views3 pages

Crud

The document contains code for an ASP.NET web page that manages records in a database. It includes methods for adding, updating, deleting, and displaying records from 'Table_1', as well as clearing input fields. The page uses a GridView to show the records and handles user interactions through button clicks.

Uploaded by

iiclriyagupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Crud

The document contains code for an ASP.NET web page that manages records in a database. It includes methods for adding, updating, deleting, and displaying records from 'Table_1', as well as clearing input fields. The page uses a GridView to show the records and handles user interactions through button clicks.

Uploaded by

iiclriyagupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

protected void Page_Load(object sender, EventArgs e)

if (!IsPostBack)

display();

protected void btnAdd_Click(object sender, EventArgs e)

SqlCommand cmd = new SqlCommand("INSERT INTO Table_1 VALUES(@name, @email)",


conn);

cmd.Parameters.AddWithValue("@name", TextBox1.Text);

cmd.Parameters.AddWithValue("@email", TextBox2.Text);

conn.Open();

cmd.ExecuteNonQuery();

conn.Close();

Label1.Text = "Record added.";

display();

cleartxt();

protected void btnUpdate_Click(object sender, EventArgs e)

SqlCommand cmd = new SqlCommand("UPDATE Table_1 SET email = @email WHERE name
= @name", conn);

cmd.Parameters.AddWithValue("@name", TextBox1.Text);

cmd.Parameters.AddWithValue("@email", TextBox2.Text);
conn.Open();

cmd.ExecuteNonQuery();

conn.Close();

Label1.Text = "Record updated.";

display();

cleartxt();

protected void btnDelete_Click(object sender, EventArgs e)

SqlCommand cmd = new SqlCommand("DELETE FROM Table_1 WHERE name = @name",


conn);

cmd.Parameters.AddWithValue("@name", TextBox1.Text);

conn.Open();

cmd.ExecuteNonQuery();

conn.Close();

Label1.Text = "Record deleted.";

display();

cleartxt();

public void display()

SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Table_1", conn);

DataTable dt = new DataTable();

da.Fill(dt);

GridView1.DataSource = dt;

GridView1.DataBind();
}

public void cleartxt()

TextBox1.Text = "";

TextBox2.Text = "";

protected void btnClear_Click(object sender, EventArgs e)

cleartxt();

Label1.Text = "";

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)

TextBox1.Text = GridView1.SelectedRow.Cells[1].Text;

TextBox2.Text = GridView1.SelectedRow.Cells[2].Text;

You might also like