These are my tables:
CREATE TABLE [dbo].[tblDepartment]
(
[departmentId] [int] IDENTITY(1,1) NOT NULL,
[name] [nvarchar](50) NULL,
CONSTRAINT [PK_tblDepartment]
PRIMARY KEY CLUSTERED ([departmentId] ASC)
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[tblUser]
(
[userId] [int] IDENTITY(1,1) NOT NULL,
[name] [nvarchar](50) NULL,
[address] [nvarchar](500) NULL,
[state] [nvarchar](50) NULL,
[salary] [bigint] NULL,
[departmentId] [int] NULL,
CONSTRAINT [PK_tblUser]
PRIMARY KEY CLUSTERED ([userId] ASC)
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tblUser] WITH CHECK
ADD FOREIGN KEY([departmentId])
REFERENCES [dbo].[tblDepartment] ([departmentId])
GO
My stored procedure:
CREATE PROCEDURE [dbo].[tblDepartment_insertUpdate]
@userId int =0,
@name nvarchar(50) ,
@state nvarchar (50),
@address nvarchar(500),
@departmentId int ,
@result nvarchar(50) output
AS
BEGIN
IF (@userId = 0)
BEGIN
INSERT INTO tblUser (name, address, state, departmentId)
VALUES (@name, @address, @state, @departmentId)
IF (@@identity > 0)
SET @result = 'Record save successfully'
ELSE
SET @result = 'Record not saved!'
END
ELSE
BEGIN
UPDATE tblUser
SET name = @name,
address = @address,
state = @state,
departmentId = @departmentId
WHERE userId = @userId
SET @result = 'Record Updated !'
END
END
What I have tried:
public IHttpActionResult PostEmpDetails(string name, int departmentId, string address, int salary, string state, int userId) {
if (!ModelState.IsValid) {
return BadRequest(ModelState);
}
// by using own stored procedure
var spName = new SqlParameter("@name", name);
var spState = new SqlParameter("@state", state);
var spAddress = new SqlParameter("@address", address);
var spDepartmentId = new SqlParameter("@departmentId", departmentId);
var spUser = new SqlParameter("@userId", userId);
var spOut = new SqlParameter {
ParameterName = "@result",
SqlDbType = System.Data.SqlDbType.NVarChar,
Size = 100,
Direction = System.Data.ParameterDirection.Output
};
var user = eContext.Database.SqlQuery < tbluser > ("exec tblDepartment_insertUpdate @userId,@name,@state,@address,@departmentId, @result out ",
spUser, spName, spState, spAddress, spDepartmentId, spOut
).ToList < tbluser > ();
return Ok(user);
}
I want to use my stored procedure in ASP.NET MVC for crud operations in a WebAPI project, but in post method, I get an error. Please suggest a solution
This is the error I get:
The data reader is incompatible with the specified 'EntityTestModel.tblUser'. A member of the type, 'userId', does not have a corresponding column in the data reader with the same name.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.