|
Forums » .NET » Sharepoint »
|
How to update the list item values in to the list? |
Posted Date: 30 Jun 2012 Posted By:: sridhar Member Level: Bronze Member Rank: 2610 Points: 2
Responses:
1
|
I want on scenario i have Emp list that have fields are Id ,name ,salary .i want update the items in the list programatically? please give me reply for the scenario ASAP...
|
Responses
|
#678254 Author: Devaraj T N Member Level: Gold Member Rank: 18 Date: 30/Jun/2012 Rating:  Points: 4 | Hi,
First create emp class as follows
public class emp { //declaring fields string _id; string _name; string _salary;
//declaring properties public string id { get { return _id; } set { _id = value; } }
public string name { get { return _name; } set { _name = value; } }
public string salary { get { return _salary; } set { _salary = value; } }
}
Add retrieveemplist() method inside the emp class
public List<emp> retrieveemplist() { string myconnectionstring = @"Data Source=servername;Initial Catalog=databasename; username=username; password=password;"; SqlConnection con = new SqlConnection(myconnectionstring); try { con.Open(); SqlCommand com = new SqlCommand("select * from emp", con); SqlDataReader dr = com.ExecuteReader(); List<emp> emplist = new List<emp>(); while (dr.Read()) { emp emp = new emp(); emp.id = dr["id"].ToString(); emp.name = dr["name"].ToString(); emp.salary = dr["salary"].ToString(); } com.Dispose(); dr.Dispose(); con.Dispose(); return emplist; } finally { con.Close(); } }
Your whole emp class as follows
public class emp { //declaring fields string _id; string _name; string _salary;
//declaring properties public string id { get { return _id; } set { _id = value; } }
public string name { get { return _name; } set { _name = value; } }
public string salary { get { return _salary; } set { _salary = value; } }
public List<emp> retrieveemplist() { string myconnectionstring = @"Data Source=servername;Initial Catalog=databasename; username=username; password=password;"; SqlConnection con = new SqlConnection(myconnectionstring); try { con.Open(); SqlCommand com = new SqlCommand("select * from emp", con); SqlDataReader dr = com.ExecuteReader(); List<emp> emplist = new List<emp>(); while (dr.Read()) { emp emp = new emp(); emp.id = dr["id"].ToString(); emp.name = dr["name"].ToString(); emp.salary = dr["salary"].ToString(); } com.Dispose(); dr.Dispose(); con.Dispose(); return emplist; } finally { con.Close(); } } }
Now you can update your emp list by calling retrieveemplist() method from emp class anywhere in the application by declaring as follows.
//Declaring emp class object name employee emp employee = new emp(); //fetching the emp list from the database List<emp> emplist = employee.retrieveemplist();
Hope it will be usefull,
Regards, Devaraj T.N Senior Software Engineer, Microsoft Certified Technology Specialist.
Regards, T.N. THEAAVARAAJ Senior Software Engineer, Microsoft Certified Technology Specialist. Email:devabe2005@gmail.com
|
|
| Post Reply |
|
|
|
 | | This thread is locked for new responses. Please post your comments and questions as a separate thread. If required, refer to the URL of this page in your new post. |
|
|
|
|
 Follow us on Twitter: https://twitter.com/dotnetspider
|
|