dimanche 10 mai 2015

Utilize returned value from Stored Procedure using Entity Framework in VB.Net

I hope the below stored procedure inserts a new record to the table and returns the same when executed. I tried to utilize this stored procedure from Entity Framework as shown far below. The new record is properly inserted. But I don't know why I couldn't get the value returned from the procedure. Could anyone help me with this?

Stored Procedure:

USE [Materials]
GO

CREATE PROCEDURE [dbo].[insertQuery1]
@Code NVARCHAR(50),
@Name NVARCHAR(100)
AS
BEGIN
INSERT INTO dbo.Materials
(Code, Name)

Values (@Code, @Name)

Select mat.ID, mat.Code, mat.Name from dbo.Materials mat where mat.ID = SCOPE_IDENTITY()

END

ASPX.VB Code:

Protected Sub testing_Click(sender As Object, e As EventArgs)
    Dim code As String
    Dim name As String
    Dim element As Object
    code = "New Code"
    name = "New Element"
    element = ent.insertQuery(code, name)
    Dim mat As Material = CType(element, Material)
End Sub

When I try this code, I get the following error

Unable to cast object of type 'System.Data.Objects.ObjectResult`1[Stored_Procedure_Test.insertQuery_Result]' to type 'Stored_Procedure_Test.Material'.

at the line "Dim mat As Material = CType(element, Material)"

Aucun commentaire:

Enregistrer un commentaire