Why I get an error in Dapper with column string parsing?

.net c# dapper sql sql-server

Question

I have the table below.

create table tblWorkers
(
    Id int identity,
    WorkerId nvarchar(8),

    Username NVARCHAR(50) not null,
    Password NVARCHAR(12) not null,
    Token NVARCHAR(max) null,
    CONSTRAINT PK_WorkerId PRIMARY KEY (WorkerId)
)

I see the error message, "Error parsing column 0 (UserId=W01 - String)" when I attempt the following Dapper code with the parameters Username:W01 and Password:check123. Input string wasn't formatted correctly, throwing a FormatException.

``

        DynamicParameters parameters = new DynamicParameters();
        parameters.Add("@userName", loginModel.Username, DbType.String);
        parameters.Add("@password", loginModel.Password, DbType.String);
        using (IDbConnection con = _connectionManager.GetConnection())
        {
            con.Open();
            var result = con.Query<User>(StoredProcedures.uspAuthenticate, param: parameters, commandType: CommandType.StoredProcedure);
            return result.FirstOrDefault();

The following is the StoredProcedure.

CREATE PROCEDURE [dbo].[uspAuthenticate] 
    @userName nvarchar(8),
    @password nvarchar(12)
AS
BEGIN

    SET NOCOUNT ON;

    select WorkerId
    from tblWorkers
    where Username = @username and Password = @password
END

Please provide advice.

1
-1
10/28/2019 12:31:15 PM

Accepted Answer

Dapper anticipates that SQL and.NET will be similar. The issue specifies UserId, yet the code examples provide username as the parameter (not id).

When passing, you state, "Username: W01." Error parsing column 0 (UserId=W01 - String), according to the message.

0
11/4/2019 8:45:27 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow