ASP.Net MVC Error: The data reader returned by the store data provider does not have enough columns for the query requested

Last Reply 5 months ago By nedash

Posted 5 months ago

hi 

according below article : Insert (Save) data (records) to database using Stored Procedure in ASP.Net MVC

I use EntityFrameWork and Stored procedure to inserting data into database:

StoreProcedure:

ALTER PROCEDURE [dbo].[InsertReseller]

        @Name nvarchar(100)
        ,@Address nvarchar(100)
        ,@OstanId int
AS
BEGIN
  INSERT INTO Resellers(Name,Address,OstanId) VALUES(@Name,@Address,@OstanId)
END

contoller:

public ActionResult AddReseller(Reseller reseller)
{
    if (ModelState.IsValid)
    {
        MvcShopDbEntities entities = new MvcShopDbEntities();
        int id = entities.Insert_reseller(reseller.Name, reseller.Address, reseller.OstanId).FirstOrDefault().Value;
    }

but when I click button to inserting data into database below error occured:

An exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

Additional information: The data reader returned by the store data provider does not have enough columns for the query requested.

this error occure in 

Model.Context in below code:

        public virtual ObjectResult<Nullable<int>> Insert_reseller(string name, string address, Nullable<int> ostanId)
        {
            var nameParameter = name != null ?
                new ObjectParameter("Name", name) :
                new ObjectParameter("Name", typeof(string));
    
            var addressParameter = address != null ?
                new ObjectParameter("Address", address) :
                new ObjectParameter("Address", typeof(string));
    
            var ostanIdParameter = ostanId.HasValue ?
                new ObjectParameter("OstanId", ostanId) :
                new ObjectParameter("OstanId", typeof(int));
    
            return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<Nullable<int>>("Insert_reseller", nameParameter, addressParameter, ostanIdParameter);
        }
    }

in this line:

return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<Nullable<int>>("Insert_reseller", nameParameter, addressParameter, ostanIdParameter);

best regards

Neda

You are viewing reply posted by: nedash 5 months ago.
Posted 5 months ago

hi

my problem was in Store procedure I should add below code:

SET @Id = SCOPE_IDENTITY()
SELECT @Id [Id]

best regards

neda