Need to return username.

Sep 28, 2010 at 9:26 AM

Need to return username.

public static string GetUserNameByProfileID(int profileId)
    {
        string userName = String.Empty;
        if (profileId == 0)
            return null;
        
        Database db = OmegaLoveSqlDataHelper.CreateConnection(ConnnectionString);
        DbCommand dbCommand = db.GetStoredProcCommand("prc_Profile_SelectByPrimaryKey");
        db.AddInParameter(dbCommand, "ProfileID", DbType.Int32, profileId);
        db.AddOutParameter(dbCommand, "UserName", DbType.String, 0);
        if (db.ExecuteNonQuery(dbCommand) > 0)
        {
            userName = Convert.ToString(db.GetParameterValue(dbCommand, "@UserName"));
        }

        return userName;
    }

 

CREATE PROCEDURE [dbo].[prc_Profile_SelectByPrimaryKey]
	@ProfileID int,
	@UserName nvarchar(50) = NULL output
AS
SELECT UserName
FROM [dbo].[tbl_Profile]
WHERE
	([ProfileID] = @ProfileID)

 

Server Error in '/' Application.

String[1]: the Size property has an invalid size of 0.

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.InvalidOperationException: String[1]: the Size property has an invalid size of 0.

Source Error:

Line 89:         db.AddInParameter(dbCommand, "ProfileID", DbType.Int32, profileId);
Line 90:         db.AddOutParameter(dbCommand, "UserName", DbType.String, 0);
Line 91:         if (db.ExecuteNonQuery(dbCommand) > 0)
Line 92:         {
Line 93:             userName = Convert.ToString(db.GetParameterValue(dbCommand, "@UserName"));

Sep 28, 2010 at 9:43 AM

You should change the size parameter for the UserName output parameter.  For example, if your UserName column's datatype is nvarchar(50), your code should be:

db.AddOutParameter(dbCommand, "UserName", DbType.String, 50);

Unrelated to the error  you're getting, you should also modify your stored procedure's select statement so as to able to assign the result to the @UserName parameter:

SELECT @UserName = UserShortName
FROM [dbo].[UserView]
WHERE
 ([UserID] = @ProfileID)

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com