Cómo agregar elementos de la tabla de datos a la lista en asp.net

Estoy creando un web service method, where i have to add items in list from data-table. How to do this? Here i am pasting my code. I am getting error in items.Add(row).

[System.Web.Services.WebMethod]
public static List<SelectUsers> GetUsers()
{
    List<SelectUsers> items = new List<SelectUsers>();

    DataTable DetailsTbl = new DataTable();
    int gimId = 0;
    if (DetailsTbl.Rows[0]["RwId"] != "")
    {
        gimId = Convert.ToInt32(DetailsTbl.Rows[0]["RwId"]);

    }  
    DataTable dtAssignTo = SLAFacadeBLL.GetGIMIncidentUsers(gimId);
    if (dtAssignTo != null && dtAssignTo.Rows.Count > 0)
    {
        foreach (DataRow row in dtAssignTo.Rows)
        {
            items.Add(row);
        }
        return items;
    }                
}

SelectUsers.cs:

public class SelectUsers
{
    public string Value { get; set; }
    public string Text { get; set; }
}

Can anyone give any suggestion?

preguntado el 12 de febrero de 14 a las 06:02

puedes agregar el codigo de SelectUsers ¿clase? -

This is a class file only get and set method -

I know, but I need to know the property names of SelectUsers class, you can't add a DataRow a una List<SelectUsers> -

I have added now..just have look on my code. -

2 Respuestas

No puedes agregar un DataRow a una List<SelectUsers>, you have to assign the values from row a una SelectUsers. As per comment below, Value es tomado de row["Id"] y Text es tomado de row["Name"], por lo que este código debería funcionar

if (dtAssignTo != null && dtAssignTo.Rows.Count > 0)
{
    foreach (DataRow row in dtAssignTo.Rows)
    {
        items.Add(new SelectUsers { Value = row["Id"].ToString(), Text = row["Name"].ToString() });
    }
}
return items;

and you should move return items; to outside of if (dtAssignTo != null && dtAssignTo.Rows.Count > 0) bloquear para evitar not all code paths return a value error.

Respondido 12 Feb 14, 07:02

Thnaks...but again it is showing error in GetUsers() method....the error is GetUser() method -not all code paths return a value. - JOJO

What's the error? Can you tell me the column names of row and which columns will be taken for SelectUsers? I'm only assuming the column names are Value y Text. - ekad

Okie..in palce of "Value" and "Text".we have to put the column name.Thanks - JOJO

Yes exactly, if you can tell me the column names I will change my answer. - ekad

public static List GetUsers() { List items = new List();

    DataTable DetailsTbl = new DataTable();
    int gimId = 0;
    if (DetailsTbl.Rows[0]["RwId"] != "")
    {
        gimId = Convert.ToInt32(DetailsTbl.Rows[0]["RwId"]);

    }
    DataTable dtAssignTo = SLAFacadeBLL.GetGIMIncidentUsers(gimId);
    if (dtAssignTo != null && dtAssignTo.Rows.Count > 0)
    {

        foreach (DataRow row in dtAssignTo.Rows)
        {
            SelectUsers selectuser = new SelectUsers();
            selectuser.Value = row["Value"].ToString(); // row["column name in the datatable"].ToString();
            selectuser.Text = row["Text "].ToString();  // row["column name in the datatable"].ToString();
            items.Add(selectuser);
        }
    }
}

Gracias.

Respondido 12 Feb 14, 07:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.