I am trying to list all data in a table, but it only returns the first row, it doesn't loop the whole table. i need to return the data as strings, because I will use it in a ASMX web service.
And the xml schema only returns the first row
<String> data in row 1<String>
i want it to return somthing like this:
<String> data in row 1<String>
<String> data in row 2<String>
<String> data in row 3<String>
and row 1 to n rows....
I have tested the sql statment in VS2012 query builder and there it works fine. so i need to list out all the data in a way.
Here is my Code
public String finAllCompaniesForSpesficuserByUserId(String userid)
{
List<String> l = new List<String>();
try
{
String sql = "SELECT Companies.Name FROM UsersInCompanies INNER JOIN Companies ON UsersInCompanies.CompanyId = Companies.CompanyId WHERE UsersInCompanies.UserId ='" + userid + "'";
con = new SqlConnection(cs);
cmd = new SqlCommand(sql, con);
DataTable table = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(table);
con.Open();
dr = cmd.ExecuteReader();
dr.Read();
//while (dr.Read())
//{
// l.Add(dr["Name"].ToString());
//}
foreach (DataRow row in table.Rows)
{
return row["Name"].ToString();
}
}
finally
{
if (con != null)
con.Close();
}
/*
foreach (string p in l)
{
return p;
}
*/
return null;
}
Can someone point me in the right direction or give me an examples?