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?
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();
}
}
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With