对于学校,我们必须提出3级申请。使用数据库,Web服务和客户端部分。 现在我有以下存储过程,
ALTER PROCEDURE dbo.addMovie
(
@film_naam nvarchar(50),
@film_hoofdrol nvarchar(50)
)
AS
/* SET NOCOUNT ON */
INSERT INTO tbl_film
(film_naam, film_status, film_hoofdrol)
VALUES(@film_naam,0,@film_hoofdrol)
RETURN
当我执行此操作时,效果很好。但是当我在我的网络服务中尝试它时,我收到以下错误。
System.NullReferenceException:未将对象引用设置为对象的实例。 在MovieErent.Movie_rentDB.addMovie(String film_naam,String film_hoofdrol)中的D:\ School \ Programmeren VB.net \ Periode 2 \ Movie_rent \ Movie_rent \ App_code \ Movie_rentDB.vb:第105行 在MovieErent.Movie_web.addMovie(String film_naam,String film_hoofdrol)中的D:\ School \ Programmeren VB.net \ Periode 2 \ Movie_rent \ Movie_rent \ Movie_web.asmx.vb:第40行
我的网络服务部分
<WebMethod()> _
Public Function addMovie(ByVal film_naam As String, ByVal film_hoofdrol As String) As Boolean
Dim Movie_rent As New Movie_rentDB()
Return Movie_rent.addMovie(film_naam, film_hoofdrol)
End Function
我的Movie_rentDB类
Public Class Movie_rentDB
Private strApplicationName As String = "Movie rent"
Private strConnection As String
Private conMovie As SqlConnection
Private adapMovie As New SqlDataAdapter
Private CommandMovie As SqlCommand
Private DataSetMovie As New DataSet
Private sqlTransaction As SqlTransaction
Private objLoggingService As Logging.Logging = New Logging.Logging("d:\\WebService.log")
Public ReadOnly Property ConnectionString() As String
Get
Try
Return ConfigurationManager.ConnectionStrings("Movie_rent").ConnectionString
Catch ex As Exception
objLoggingService.WriteLine(strApplicationName, ex.Message)
Throw (ex)
End Try
Return ""
End Get
End Property
Public Sub CreateConnection()
Try
conMovie = New SqlConnection(ConnectionString)
Catch ex As Exception
objLoggingService.WriteLine(strApplicationName, ex.Message)
Throw (ex)
End Try
End Sub
Private Sub Prepare_StoredProcedureCall(ByVal strStoredProcedure As String)
Try
CommandMovie = New SqlCommand(strStoredProcedure, conMovie)
CommandMovie.CommandType = CommandType.StoredProcedure
adapMovie = New SqlDataAdapter(CommandMovie)
DataSetMovie = New DataSet()
conMovie.Open()
sqlTransaction = conMovie.BeginTransaction
CommandMovie.Transaction = sqlTransaction
Catch ex As Exception
objLoggingService.WriteLine(strApplicationName, ex.Message)
Throw (ex)
End Try
End Sub
Private Sub Finish_StoredProcedureCall()
Try
If (conMovie.State = ConnectionState.Open) Then
conMovie.Close()
End If
Catch ex As Exception
objLoggingService.WriteLine(strApplicationName, ex.Message)
Throw (ex)
End Try
End Sub
Public Function getAllMovies() As DataSet
Try
Prepare_StoredProcedureCall("getAllMovies")
adapMovie.Fill(DataSetMovie, "tbl_film")
sqlTransaction.Commit()
Catch ex As Exception
If Not (sqlTransaction Is Nothing) Then
sqlTransaction.Rollback()
End If
DataSetMovie = Nothing
objLoggingService.WriteLine(strApplicationName, ex.Message)
Throw (ex)
End Try
Try
Finish_StoredProcedureCall()
Catch ex As Exception
DataSetMovie = Nothing
Throw (ex)
End Try
Return DataSetMovie
End Function
Public Function addMovie(ByVal film_naam As String, ByVal film_hoofdrol As String) As Boolean
Dim bStatus As Boolean = False
Try
Prepare_StoredProcedureCall("addMovie")
With CommandMovie.Parameters
.AddWithValue("film_naam", film_naam)
.AddWithValue("film_status", 0)
.AddWithValue("film_hoofdrol", film_hoofdrol)
End With
CommandMovie.ExecuteNonQuery()
sqlTransaction.Commit()
bStatus = True
Catch ex As Exception
sqlTransaction.Rollback()
objLoggingService.WriteLine(strApplicationName, ex.Message)
Throw (ex)
End Try
Finish_StoredProcedureCall()
Return bStatus
End Function
希望有人能帮助我:)。
答案 0 :(得分:1)
在addMovie命令存储过程的代码中传入3个参数,但你的sproc只接受2个参数。
答案 1 :(得分:0)
根据我的判断,您的问题是您没有致电CreateConnection
并创建新连接。
在你的Prepare_StoredProcedureCall
sub中,你这样做:`CommandMovie = New SqlCommand(strStoredProcedure,conMovie)
CommandMovie.CommandType = CommandType.StoredProcedure
adapMovie = New SqlDataAdapter(CommandMovie)
DataSetMovie = New DataSet()
conMovie.Open()
` 问题是此时没有创建conMovie。你需要做这样的事情:
CreateConnection
CommandMovie = New SqlCommand(strStoredProcedure, conMovie)
CommandMovie.CommandType = CommandType.StoredProcedure
adapMovie = New SqlDataAdapter(CommandMovie)
DataSetMovie = New DataSet()
conMovie.Open()
编辑:
我注意到你的.vb文件中没有End Class
这个特定的类。这可能是问题吗?
P.S:如果你已经想到这一点,对不起,我来不及帮你:)