-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud.txt
81 lines (65 loc) · 2.58 KB
/
crud.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;// you have to add these 2
using System.Data.SqlClient;//
namespace crud2
{
public partial class WebForm1 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\learning.mdf;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
con.Open();
}
protected void Button1_Click(object sender, EventArgs e)//insert button code
{
String qry = "insert into task values ('" + id.Text + "','" + name.Text + "','" + contact.Text + "','" + address.Text + "')";
SqlCommand cmd = new SqlCommand(qry, con);
cmd.ExecuteNonQuery();
id.Text = "";
name.Text = "";
contact.Text = "";
address.Text = "";
Response.Write("data inserted");
}
protected void Button4_Click(object sender, EventArgs e)//view button code
{
String qry = "select * from task ";
SqlCommand cmd = new SqlCommand(qry, con);
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
Response.Write("displaying data");
}
protected void Button2_Click(object sender, EventArgs e)//update button code
{
String qry = "update task set name='"+name.Text+"',contact='"+contact.Text+"',address='"+address.Text+"'where id=('" + id.Text + "')";
SqlCommand cmd = new SqlCommand(qry, con);
cmd.ExecuteNonQuery();
id.Text = "";
name.Text = "";
contact.Text = "";
address.Text = "";
Response.Write("updating data");
}
protected void Button3_Click(object sender, EventArgs e)//delete button code
{
String qry = "delete from task where id= ('" + id.Text + "')";
SqlCommand cmd = new SqlCommand(qry, con);
cmd.ExecuteNonQuery();
id.Text = "";
Response.Write("data deleted");
}
}
}