How to Insert ,Edit ,Update,Delete,Cancel and Print records in Data List control in asp.net
Step 2:- Now Add Database.mdf file in your website -->Create a employee table with three column as shown below:
Note:-You can create table separately in database(Sql ,MySQL etc.) and connect to this application .If you are facing any problem to add .mdf file on website then visit below links :- How add .mdf file on asp.net website. How to solve sql server problems. Step 3:- Now open your Default.aspx page -->Click Source button from below-->Write the following Data List layout code(html code) as given below:- Step 4:- Now click Design from the below of Default.aspx page-->you will see following layout of Data List control as shown below:-
Note:- I have given full explanation of the layout code in Repeater control application Step 5:- Now go properties of Data List control-->Click Events--> Now generate handler by double click on each as shown below:-
Step 6:- Now Open Default.aspx.cs page (or press F7 --> and Write the following c# codes as given below:-
Step 6:- Now Open Default.aspx.cs page (or press F7 --> and Write the following c# codes as given below:-
using System;
using System.
Web; using System.
Web.UI; using System.
Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
public partial class Default :
System.Web.UI.Page
{ protected void Page_Load(object sender, EventArgs e)
{ if (!IsPostBack) { GetData(); } } private void GetData() { SqlConnection con = new SqlConnection(@"Data Source=.;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;Max Pool Size=20; Connection Timeout=10;"); try { con.Open(); SqlCommand cmd = new SqlCommand("select * from employee", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); DataList1.DataSource = ds.Tables[0]; DataList1.DataBind(); } finally { con.Close(); } } protected void DataList1_EditCommand(object source, DataListCommandEventArgs e) { DataList1.EditItemIndex = e.Item.ItemIndex; GetData(); } protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e) { DataList1.EditItemIndex = -1; GetData(); } protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e) { String eid = ((Label)e.Item.FindControl("Label3")).Text; TextBox name = (TextBox)e.Item.FindControl("TextBox1"); TextBox salary = (TextBox)e.Item.FindControl("TextBox3"); SqlConnection con = new SqlConnection(@"Data Source=.;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;Max Pool Size=20; Connection Timeout=10;"); con.Open(); SqlCommand cmd = new SqlCommand("update employee set name='" + name.Text + "',salary='" + salary.Text + "'where eid ='" + eid + "'", con); int i = cmd.ExecuteNonQuery(); if (i > 0) { Label4.Text = "Recard Updated successfully...."; GetData(); } } protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e) { String eid = ((Label)e.Item.FindControl("Label1")).Text; SqlConnection con = new SqlConnection(@"Data Source=.;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;"); con.Open(); SqlCommand cmd = new SqlCommand("delete from employee where eid='" + eid + "'", con); int i = cmd.ExecuteNonQuery(); if (i > 0) { Label4.Text = "Recard Deleted successfully...."; GetData(); } } protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e) { if (e.CommandName.Equals("Insert")) { SqlConnection con = new SqlConnection(@"Data Source=.;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;Max Pool Size=20; Connection Timeout=10;"); con.Open(); TextBox ename = (TextBox)e.Item.FindControl("TextBox4"); TextBox salary = (TextBox)e.Item.FindControl("TextBox5"); SqlCommand cmd = new SqlCommand("insert into employee values('" + ename.Text + "','" + salary.Text + "')", con); int i = cmd.ExecuteNonQuery(); if (i > 0) { Label4.Text = "Recard inserted successfully...."; GetData(); } } } protected void LinkButton5_Click(object sender, EventArgs e) { Response.Redirect("default2.aspx"); } }
Step 7 :- Now Add another web Form (Default2.aspx)-->click Source button -->and write Data List layout (Html)codes and java script codes as given below:- Step 8 :- Now click design --> You will see following layout as shown below:-
Step 9:- Now open default.aspx.cs page(or press F7) --> and Write the following c# codes as given below:- using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data; using System.IO; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GetData(); } } private void GetData() { SqlConnection con = new SqlConnection(@"Data Source=.;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;Max Pool Size=20; Connection Timeout=10;"); try { con.Open(); SqlCommand cmd = new SqlCommand("select * from employee", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); DataList1.DataSource = ds.Tables[0]; DataList1.DataBind(); } finally { con.Close(); } } } Step 10:- Now Run the Application (press F5)-->Follow some points which are given below:- Write the Name and Salary fields values in below Text Box as shown below--> and Press Insert button.
Now Click Edit Button Emp Id (eid)=103 as shown below:-
Now Change the Emp Name value from mohan to Manmohan singh and salary value 40000 to 4500. -->click Update button as shown below:-
Now click Cancel Button for changing the Edit mode properties as shown below:-
Now Delete Emp Id = 105 as show below:-
Now click Take Preview Button -->You will see following preview as shown below:-
Now Click Take print out button -->You can take print out of the Existing page or save in pdf file (save --> save as pdf -->save) as shown below:-
Data List control is used to display the entire table data like Gridview and Repeater control in asp.net.It is a Web Form control which is used in asp.net
There are some points about Data List Control:-
There are some operations which are performed in this application which are given below:-
There are some points about Data List Control:-
- Data List control has not a default layout like Gridview control in asp.net.
- Data List control is converted to Html table tag on Browser side like Gridview in asp.net.
- Data List control has predefined event to add,delete,Edit,update and cancel the record.
There are some operations which are performed in this application which are given below:-
- Insert Data in Data List control
- Edit specific Data in Data List control
- Update specific Data in Data List control
- Cancel Data in Data List control
- Delete Data in Data List control
- Take Print out of Data in Data List control
In our previous tutorials i had made two application which can be used in any organizations. which are given below:-
There are some steps to make this application which are given below:-
Step 1:- First open your visual studio --> File-->New-->Website-->Select ASP.NET Empty website --> OK -->Open solution Explorer --> Add a Web Form (Default.aspx).
Step 1:- First open your visual studio --> File-->New-->Website-->Select ASP.NET Empty website --> OK -->Open solution Explorer --> Add a Web Form (Default.aspx).
Step 2:- Now Add Database.mdf file in your website -->Create a employee table with three column as shown below:
Note:-You can create table separately in database(Sql ,MySQL etc.) and connect to this application .If you are facing any problem to add .mdf file on website then visit below links :- How add .mdf file on asp.net website. How to solve sql server problems. Step 3:- Now open your Default.aspx page -->Click Source button from below-->Write the following Data List layout code(html code) as given below:- Step 4:- Now click Design from the below of Default.aspx page-->you will see following layout of Data List control as shown below:-
Note:- I have given full explanation of the layout code in Repeater control application Step 5:- Now go properties of Data List control-->Click Events--> Now generate handler by double click on each as shown below:-
Step 6:- Now Open Default.aspx.cs page (or press F7 --> and Write the following c# codes as given below:-
Step 6:- Now Open Default.aspx.cs page (or press F7 --> and Write the following c# codes as given below:-
using System;
using System.
Web; using System.
Web.UI; using System.
Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
public partial class Default :
System.Web.UI.Page
{ protected void Page_Load(object sender, EventArgs e)
{ if (!IsPostBack) { GetData(); } } private void GetData() { SqlConnection con = new SqlConnection(@"Data Source=.;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;Max Pool Size=20; Connection Timeout=10;"); try { con.Open(); SqlCommand cmd = new SqlCommand("select * from employee", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); DataList1.DataSource = ds.Tables[0]; DataList1.DataBind(); } finally { con.Close(); } } protected void DataList1_EditCommand(object source, DataListCommandEventArgs e) { DataList1.EditItemIndex = e.Item.ItemIndex; GetData(); } protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e) { DataList1.EditItemIndex = -1; GetData(); } protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e) { String eid = ((Label)e.Item.FindControl("Label3")).Text; TextBox name = (TextBox)e.Item.FindControl("TextBox1"); TextBox salary = (TextBox)e.Item.FindControl("TextBox3"); SqlConnection con = new SqlConnection(@"Data Source=.;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;Max Pool Size=20; Connection Timeout=10;"); con.Open(); SqlCommand cmd = new SqlCommand("update employee set name='" + name.Text + "',salary='" + salary.Text + "'where eid ='" + eid + "'", con); int i = cmd.ExecuteNonQuery(); if (i > 0) { Label4.Text = "Recard Updated successfully...."; GetData(); } } protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e) { String eid = ((Label)e.Item.FindControl("Label1")).Text; SqlConnection con = new SqlConnection(@"Data Source=.;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;"); con.Open(); SqlCommand cmd = new SqlCommand("delete from employee where eid='" + eid + "'", con); int i = cmd.ExecuteNonQuery(); if (i > 0) { Label4.Text = "Recard Deleted successfully...."; GetData(); } } protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e) { if (e.CommandName.Equals("Insert")) { SqlConnection con = new SqlConnection(@"Data Source=.;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;Max Pool Size=20; Connection Timeout=10;"); con.Open(); TextBox ename = (TextBox)e.Item.FindControl("TextBox4"); TextBox salary = (TextBox)e.Item.FindControl("TextBox5"); SqlCommand cmd = new SqlCommand("insert into employee values('" + ename.Text + "','" + salary.Text + "')", con); int i = cmd.ExecuteNonQuery(); if (i > 0) { Label4.Text = "Recard inserted successfully...."; GetData(); } } } protected void LinkButton5_Click(object sender, EventArgs e) { Response.Redirect("default2.aspx"); } }
Step 7 :- Now Add another web Form (Default2.aspx)-->click Source button -->and write Data List layout (Html)codes and java script codes as given below:- Step 8 :- Now click design --> You will see following layout as shown below:-
Step 9:- Now open default.aspx.cs page(or press F7) --> and Write the following c# codes as given below:- using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data; using System.IO; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GetData(); } } private void GetData() { SqlConnection con = new SqlConnection(@"Data Source=.;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;Max Pool Size=20; Connection Timeout=10;"); try { con.Open(); SqlCommand cmd = new SqlCommand("select * from employee", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); DataList1.DataSource = ds.Tables[0]; DataList1.DataBind(); } finally { con.Close(); } } } Step 10:- Now Run the Application (press F5)-->Follow some points which are given below:- Write the Name and Salary fields values in below Text Box as shown below--> and Press Insert button.
Now Click Edit Button Emp Id (eid)=103 as shown below:-
Now Change the Emp Name value from mohan to Manmohan singh and salary value 40000 to 4500. -->click Update button as shown below:-
Now click Cancel Button for changing the Edit mode properties as shown below:-
Now Delete Emp Id = 105 as show below:-
Now click Take Preview Button -->You will see following preview as shown below:-
Now Click Take print out button -->You can take print out of the Existing page or save in pdf file (save --> save as pdf -->save) as shown below:-
0 comments:
Post a Comment
Get Benifits