Search code examples
c#rowrecorddatarow

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.


Solution

  • 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.