2011-02-27 19 views
0

對於學校我們必須製作3層應用。具有數據庫,Web服務和客戶端部分。 現在我有以下存儲過程,3層應用問題

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 

當我執行此,它的偉大工程。但是當我在我的web服務中嘗試它時,出現以下錯誤。

System.NullReferenceException:未將對象引用設置爲對象的實例。 at Movie_rent.Movie_web.addMovie(String film_naam)Movie_rent.Movie_web.addMovie(String film_naam,String film_hoofdrol)in D:\ School \ Programmeren VB.net \ Periode 2 \ Movie_rent \ Movie_rent \ App_code \ Movie_rentDB.vb:line 105 ,字符串film_hoofdrol)在d:\學校\ Programmeren VB.net \ Periode 2 \ Movie_rent \ Movie_rent \ Movie_web.asmx.vb:行40

我的web服務的一部分

<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

什麼是*** Logging.Logging ***? – Kiquenet 2017-10-31 15:12:37

回答

0

從我能弄清楚,你的問題是,你不要打電話CreateConnection並創建一個新的連接。 在你Prepare_StoredProcedureCall子,你這樣做:`CommandMovie =新的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() 

編輯: 我注意到你沒有End Class在.vb文件爲這個特定的類。這可能是問題嗎? P.S:如果你已經知道了這一點,對不起,我太遲了,以幫助你:)

+0

不,不,我已經正確地建立了連接。因爲我可以將已有的客戶添加到另一個表。所以這不是問題 – Steaphann 2011-02-28 07:18:11

1

在你的代碼當你addMovie命令存儲過程你傳遞3參數,但你的存儲過程只需要2參數。