1

I am trying to create a new record and keep getting an error

System.Data.Entity.Infrastructure.DbRawSqlQuery`1[System.Int32]' to type 'System.IConvertible

I have this piece of code..

// Contact
c.Name = IsItNull(contact.Name);
int newContactID = AddContact(c);

The AddContact method is

public int AddContact(Contact contact)
    {
        return Convert.ToInt32(AWJE.Database.SqlQuery<int>
            ("usp_InsertContact @Name", contact.Name));
    }

the storedproc is

    CREATE PROCEDURE [dbo].[usp_InsertContact]
    @ContactID int output,
    @Name nvarchar(50)
AS
SET NOCOUNT OFF
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

DECLARE @ERROR_SEVERITY int,
        @MESSAGE varchar(1000),
        @ERROR_NUMBER int,
        @ERROR_PROCEDURE nvarchar(200),
        @ERROR_LINE int,
        @ERROR_MESSAGE nvarchar(4000);

begin try
    insert into [Contact]
    (Name) values (@Name)
    set @ContactID = @@IDENTITY
end try

BEGIN CATCH
    SET @ERROR_SEVERITY = ISNULL(ERROR_SEVERITY(),'');
    SET @ERROR_NUMBER = ISNULL(ERROR_NUMBER(),'');
    SET @ERROR_PROCEDURE = ISNULL(ERROR_PROCEDURE(),''); 
    SET @ERROR_LINE = ISNULL(ERROR_LINE(),'');
    SET @ERROR_MESSAGE = ISNULL(ERROR_MESSAGE(),'');

    -- Test if the transaction is uncommittable.
    IF (XACT_STATE()) = -1
        BEGIN
            --PRINT N'The transaction is in an uncommittable state. Rolling back transaction.'
            ROLLBACK TRANSACTION;
        END;

    -- Test if the transaction is active and valid.
    IF (XACT_STATE()) = 1
        BEGIN
            --PRINT N'The transaction is committable. Committing transaction.'
            COMMIT TRANSACTION;   
        END;

    SET @MESSAGE = 'Error Occured in Stored Procedure ' + cast(@ERROR_PROCEDURE as varchar(200)) + 
                    '; Line Number ' + cast(@ERROR_LINE as varchar) + 
                    '; Message: [' + cast(@ERROR_NUMBER as varchar) + '] - '
                    + cast(@ERROR_MESSAGE as varchar(255))

    RAISERROR(@MESSAGE, @ERROR_SEVERITY, 1);
END CATCH;

GO

The error gets thrown at

int newContactID = AddContact(c);

What exactly am I doing wrong? I have looked at other SO posts, that is somewhat similar to this error but they were about Linq and nothing like how I am doing it. Any idea's?

1 Answer 1

2

I found the issue and the issue was in the AddContact method

public int AddContact(Contact contact)
{
    return Convert.ToInt32(AWJE.Database.SqlQuery<int>
        ("usp_InsertContact @Name", contact.Name));
}

Instead of using SqlQuery, I used ExecuteSqlCommand in its place and that fixed the error. So now the method looks like this..

return AWJE.Database.ExecuteSqlCommand
    ("usp_InsertContact @Name", contact.Name);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.