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

.net c# dapper sql sql-server

Question

I have the following table

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)
)

When I try the following Dapper code, by passing Username:W01, Password=check123 I get an error, Error parsing column 0 (UserId=W01 - String)'.FormatException: Input string was not in a correct format.

``

        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();

`` StoredProcedure is as below.

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

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

Accepted Answer

Dapper expects SQL and .NET to be the same. The error mentions UserId, code examples show parameter as username (not id).

you say 'when passing: Username:W01' The error shows 'Error parsing column 0 (UserId=W01 - String)'

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