[英]Stored Procedure from SQL Server is not filling ASP.Net dropdown using VB.Net coding
[英]Utilize returned value from Stored Procedure using Entity Framework in VB.Net
我希望下面的存儲過程在表中插入一條新記錄,並在執行時返回相同的記錄。 我試圖利用實體框架中的該存儲過程,如下所示。 新記錄已正確插入。 但是我不知道為什么我無法從過程中返回值。
存儲過程:
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代碼:
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
當我嘗試此代碼時,出現以下錯誤
Unable to cast object of type 'System.Data.Objects.ObjectResult`1[Stored_Procedure_Test.insertQuery_Result]' to type 'Stored_Procedure_Test.Material'.
在線Dim mat As Material = CType(element, Material)
從異常非常清楚,您正在將Stored_Procedure_Test.insertQuery_Result
的對象Stored_Procedure_Test.insertQuery_Result
轉換為Material
對象類型。 您的SP返回類型為Stored_Procedure_Test.insertQuery_Result
的對象的Stored_Procedure_Test.insertQuery_Result
。 因此,您可以做的就是從SP這樣返回的對象中獲取價值。
Stored_Procedure_Test.insertQuery_Result element = ent.insertQuery(code, name).FirstOrDefault();
//And you can access properties of this object like this
string name=element.Name;
因此,您的完整代碼應如下所示
Protected Sub testing_Click(sender As Object, e As EventArgs)
Dim code As String
Dim name As String
code = "New Code"
name = "New Element"
Dim element As Stored_Procedure_Test.insertQuery_Result = ent.insertQuery(code, name).FirstOrDefault();
//Now you can directly use the `element` to access values returned from SP.
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.