简体   繁体   中英

C# - Getting record from a row using DataRow

I'm trying to get record of a row using DataRow. Here's what I've done so far:

 uID = int.Parse(Request.QueryString["id"]);
 PhotoDataSetTableAdapters.MembersTableAdapter mem = new PhotoDataSetTableAdapters.MembersTableAdapter();
 PhotoDataSet.MembersDataTable memTable = mem.GetMemberByID(uID);
 DataRow[] dr = memTable.Select("userID = uID");
 string uName = dr["username"].ToString();

Then I got the error:

Cannot implicitly convert type 'string' to 'int'

The error points to "username" . I don't know what's wrong because I'm just trying to assign a string variable to a string value. Anyone figures out the reason of the error? Please help and thanks.

Change the following statement

DataRow[] dr = memTable.Select("userID = uID");

To

DataRow[] dr = memTable.Select("userID = "+ uID);

dr is a DataRow[] not a DataRow , therefor the compiler complains that you pass a String when he needs an int for the index.

You actually want the username of the the single DataRow in the DataTable , am i right?

String uName = memTable.AsEnumerable().Single().Field<String>("username");

Note that this throws an exception if there is more than one row in the DataTable . But since you pass an ID to the DataAdapter , i assume that it should return only one record.

Seems there are two problems. One is as Asif suggest that uID should be "userId = " + uID (should probably cast uID as string) and that dr is an array of datarows as Tim points out. You can access it by index too: dr[0]["userName"].ToString()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM