Tuesday, February 15, 2011

PersonPassport4a.asmx

Returning an ArrayList of Person class instead of Person[] :


[WebMethod]
public ArrayList GetPersonArrayList()
{
    SqlConnection dbConn = new SqlConnection(connStr);
    dbConn.Open();
    string sqlSelect = "select * from users ";
    SqlDataAdapter da = new SqlDataAdapter(sqlSelect, dbConn);
    DataTable dt = new DataTable();
    SqlCommand dbCommand = new SqlCommand(sqlSelect, dbConn);
    da.Fill(dt);
    dbConn.Close();
    ArrayList myList = new ArrayList();
    foreach (DataRow row in dt.Rows)
    {
        Person target = new Person();
        target.Name = row["Name"].ToString();
        target.UserName = row["UserName"].ToString();
        target.EMail = row["EMail"].ToString();
        target.Password = row["Password"].ToString();
        myList.Add(target);
    }
    return myList;





[WebMethod(Description = "Method to obtain All User Info")]
public Person[] ReturnArray()
{
    SqlConnection dbConn = new SqlConnection(connStr);
    dbConn.Open();
    string sqlSelect = "select * from users ";
    SqlDataAdapter da = new SqlDataAdapter(sqlSelect, dbConn);
    DataTable dt = new DataTable();
    SqlCommand dbCommand = new SqlCommand(sqlSelect, dbConn);
    da.Fill(dt);   
    dbConn.Close();
    List list = new List();
    foreach(DataRow row in dt.Rows) {
        // Person target = Activator.CreateInstance();
        Person target = new Person();
        target.Name = row["Name"].ToString();
        target.UserName = row["UserName"].ToString();
        target.EMail = row["EMail"].ToString();
        target.Password = row["Password"].ToString();
        // DataColumnAttribute.Bind(row,target);
        list.Add(target);
    }
    return list.ToArray();
}

No comments:

Post a Comment