Retrieve data from database using ado.net?

Posted by Mandlaa under ADO.NET on 10/13/2014 | Points: 10 | Views : 1467 | Status : [Member] | Replies : 3
ID  Name Job          DeptName 

100 g Mgr p1

100 g pur d1

200 f Sal p2

200 f order d2

This is my table with data,i want to retrieve data using data reader using below code,

I want to loop execute two times,One is retrieve data ID with Two(rows) records data,and again retive data loadId alogwith two rows data

Finally total data stored in alist


I am writing above code not getting perfect way of data,can you give me perfect code,once see my code once





Responses

Posted by: Mandlaa on: 10/13/2014 [Member] Starter | Points: 25

Up
0
Down
Give me rply

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Loganathanav on: 11/12/2014 [Member] Starter | Points: 25

Up
0
Down
Can you please attach the code which is written by you?
I'll sort out this problem.

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mandlaa on: 11/12/2014 [Member] Starter | Points: 25

Up
0
Down
[DataContract]
public class Employee
{
[DataMember]
public string ID {get;set;}
[DataMember]
public List<Dept> Depts;
}
public class Dept
{

[DataMember]
public string Job { get; set; }
[DataMember]
public string DeptName { get; set; }
}

public List<Empoyee> AllEmp()
{
List<Empoyee> lists = new List<Empoyee>();
Empoyee dto = new Empoyee();

SqlConnection con = new SqlConnection(@"data");
con.Open();
SqlCommand cmd = new SqlCommand("selectEmpoyee", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
adp.Fill(dt);
List<Dept> uplist = new List<dept>();
Dept up;
intId = 0;
dto.ID = Convert.ToInt32(dt.Rows[0]["ID"]);
foreach (DataRow stoup in dt.Rows)
{
if(Id!=0)
loadId = Convert.ToInt32(stoup["ID"]);

if (Id != dto.ID || (Id == 0))
{
dto = new Status();
dto.ID = Convert.ToInt32(stoup["ID"]);
dto.Name = stoup["Name"].ToString();
lists.Add(dto);
}

if ((Id == dto.ID) || (Id == 0))
{
up = new Dept();
up.Job = stoup["Job"].ToString();
up.DeptName = stoup["DeptName"].ToString();

uplist.Add(up);

Id = Convert.ToInt32(stoup["ID"]);
}
dto.Depts = uplist;
}
return lists;
}


Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response