8

How to extract the values from data table having single row and assign to asp labels.

private void GetUser(string userId)
{
    dbr.SelectString = "select name, gender,  address, contactno from userInfo where id = = '" + userId + "' --"; // return single row
    DataTable dt = dbr.GetTable();
    //DataRow row = dt.Rows[0];
    // how to retrieve the fields from the data table.
    //lbl_name = name.ToString();
    //lbl_gender = gender.ToString();
    //lbl_contact = contactno.ToString();
}

I thought of using foreach loop but the datatable contains only single row. How can I pass empty string in the case of NULL cells. Also, can I extract the values from datatable via list?

ASh
  • 34,632
  • 9
  • 60
  • 82
user4221591
  • 2,084
  • 7
  • 34
  • 68

4 Answers4

24
private void GetUser(string userId)
{
    dbr.SelectString = "select name, gender,  address, contactno from userInfo where id = = '" + userId + "' --"; // return single row
    DataTable dt = dbr.GetTable();
    if (dt.Rows.Count > 0) 
    {
        DataRow row = dt.Rows[0];

        lbl_name = row["name"].ToString();
        lbl_gender = row["gender"].ToString();
        lbl_contact = row["contactno"].ToString();
    }
}
ASh
  • 34,632
  • 9
  • 60
  • 82
  • thanks for the prompt reply. can I convert this data table to list and retrieve values. Which will be more preferably? Please can you explain. – user4221591 Mar 14 '15 at 08:03
  • @user4221591, which kind of list? dataTable contains 1 row, create a single object of custom type – ASh Mar 14 '15 at 19:24
3

Simply in one line

dbr.SelectString = "select name, gender,  address, contactno from userInfo where id = = '" + userId + "' --"; 
DataTable DataTable_Values= dbr.GetTable(); 
lbl_name =  DataTable_Values.Rows[0].Field<string>("name");
lbl_gender =  DataTable_Values.Rows[0].Field<int>("gender");
lbl_contact =  DataTable_Values.Rows[0].Field<int>("contact")
AhmadMM
  • 371
  • 4
  • 16
0

To get an list of all Rows, you need a Class

  class Lbl()
            {
                public string Name {get; set;}
                public string Gender {get; set;}
                public string Contact {get; set;}
            }

Fill the class:

 List<Lbl> list = new List<Lbl>();
            foreach(DataRow row in dt.Rows)
            {
                Lbl lbl = new Lbl();
                lbl.Name = row["name"].ToString();
                lbl.Gender = row["gender"].ToString();
                lbl.Contact = row["contactno"].ToString();

                list.add(lbl);
            }
Thomas
  • 113
  • 1
  • 11
0

its quite late but I think what you are looking for is the below code, this Datatable is the result of a query. That DT is then being parsed with all its rows and columns as a string.

DataTable dt = Execute_Query(Connection, query);
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    J = dt.Rows.Count;

                    StringBuilder sb = new StringBuilder();

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        for (int j = 0; j < dt.Columns.Count; j++) 
                        {
                            sb.Append(dt.Rows[i][j] + ",");
                        }
                        sb.Append("\n");
                    }

                    Logger.Info(sb.ToString());
                }
            }
Faisal Ansari
  • 352
  • 2
  • 5