Sunday, 30 September 2012

Update , Delete , Display of the data using Database

take button and write the code in the button click events....
for Delete.
first write the connection string.
con =object of sqlconnection method

SqlCommand cmd=new SqlCommand("Delete from tablename where id='"+TextBox1.Text+"'",con);
con.Open();
cmd.ExecuteNonQuerry();
con.Close();
by this you can delete the particular data from the database...

for Update

SqlCommand cmd=new SqlCommand("Update tablename set name = '"+TextBox1.Text+"',add='"+TextBox2.Text+"' where id =''+TextBox3.Text+"',con);
con.open();

cmd.ExecuteNonQuerry();
con.Close();

for display

take datagridview control from the database and a button

first write the connection string..
in the button click event

SqlCommand cmd=new SqlCommand("Select * from tablename",con);
con.Open();
SqlDataReader dr=cmd.ExecuteReader();
DataTable dt=new DataTable();
dt.Load(dr);
dataGridView1.DataSource=dt;
con.close();


sqldatareader = used to read the data from the database and dr is the object
datatable = make the virtual table.
dt.load(dr)=load the data in the table.
dataGridView1.DataSource=dt; = to display the data in the web page....




No comments :

Post a Comment