开发者

ASP.NET DATA TABLE Traverse Problem

I am getting the Data table a output from my DataAccess Layer.

In My Datatable I am getting users Name,Number,Qualification

I want to a开发者_高级运维ssign a users name,number,qualification to a textbox for a particular user id,

How can i do that.

Help


Suppose you got datatable dt from the DAL

Then

var row = from t in dt
          where t["userId"]='userid'
          select t;

since you got row related to a user now you can use it to assign to the textboxs

 txtName.Text = row["Name"]


assuming the datatable has 1 row:

DataRow row = table.Rows[0];
textbox1.text = row["Name"];
textbox2.text = row["Number"];

and so on

if there are multiple rows in the datatable, you need to select the row with that particular used id

DataRow row = table.Select("ID=" + UserID.ToString());


You have to pay attention to NULL values and number of rows.

if (table.Rows.Count == 1)
{
  DataRow row = table.Rows[0];

  NameTextBox.Text = row.IsNull("name") ? string.Empty : row["name"];
  NumberTextBox.Text = row.IsNull("number") ? string.Empty : row["number"];
}
else
{
// Deal with no rows from DL
}

Using ternary operator makes you sure, you delete content of TextBoxes in case you reload row within already filled up page.

Also you may consider to used typed dataset, and access to columns will be generated by xsd.exe.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜