Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unable to read a SQL UniqueIdentifier element in C#

Status
Not open for further replies.

SGLong

Programmer
Jun 6, 2000
405
0
0
US
I have a ASP.NET page in C# that calls a stored procedure and returns a small result set, and I'm unable to read the UniqueIdentifer element from that result set. What am I doing wrong?

Steve

Here is the final select statement of my Stored Procedure that contains the value I need the C# routine to work with. The 'UserId' field is defined as a uniqueidentifier in the SQL table.

Code:
SELECT PeopleId, UserId, FirstName, LastName, UserType FROM People 
	WHERE PeopleId = @UserNumber


This is the code from the ASP 'cs' file
Code:
// Get results from stored procedure
string cSysUserCode;
string cAHFUserId;
int nAHFUserId;
string cFullName;
UserReader = command.ExecuteReader();
if (UserReader.HasRows)
{
   while (UserReader.Read())
   {
      // This first value is not converting to string...
      [b]cSysUserCode = UserReader["UserId"].ToString().Trim();
[/b]      cAHFUserId = UserReader["PeopleId"].ToString();
      nAHFUserId = Convert.ToInt16(cAHFUserId);
      cFullName = UserReader["FirstName"].ToString().Trim()
          + " " + UserReader["LastName"].ToString().Trim();
   }
}


Here is the error message that I'm getting


UserId
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IndexOutOfRangeException: UserId

cSysUserCode = UserReader["UserId"].ToString().Trim();

 
Never Mind... I was able to figure it out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top