2013-09-04 3 views
1
Public Function Delete_GST_Entry(ByVal GE_ID As Integer) As DataTable 
    Try 
     Using con As New SqlConnection(DF_Class.GetConnectionString()) 
      Dim ds As DataTable = New DataTable 
      con.Open() 
      Dim command As SqlCommand = New SqlCommand("Delete_GSTEntry", con) 
      command.Parameters.AddWithValue("GE_ID", GE_ID) 
      command.CommandType = CommandType.StoredProcedure 
      Dim adapter As SqlDataAdapter = New SqlDataAdapter(command) 
      Dim table As DataTable = New DataTable 
      adapter.Fill(ds) 
      Return ds 
      con.Close() 
     End Using 
    Catch ex As SqlException 
     MessageBox.Show(ex.Message, "Data Input Error") 

    End Try 

End Function 

Я получаю предупреждение о том, что NULL reference could occur. Какая ошибка я делаю?Почему моя функция VB.NET показывает предупреждение

Предупреждение:

«. Функция„Delete_GST_Entry“не возвращает значение по всем путям коды может произойти Нулевая ссылка исключение во время выполнения, когда результат используется »

+0

Предупреждение «Функция« Delete_GST_Entry »не возвращает значение ко всем путям кода. Исключительная ошибка исключения может возникать во время выполнения, когда результат используется –

+0

Большое спасибо. –

+0

Вы можете добавить Return null как последний stat. поэтому весь код возвращает значение –

ответ

0

Все точки выхода (пути возврата) метода должны возвращать то, что указывает сигнатура метода.

Перепишите так:

Public Function Delete_GST_Entry(ByVal GE_ID As Integer) As DataTable 
    Dim dt As DataTable = New DataTable 

    Try 
     Using con As New SqlConnection(DF_Class.GetConnectionString()) 
      con.Open() 
      Dim command As SqlCommand = New SqlCommand("Delete_GSTEntry", con) 
      command.Parameters.AddWithValue("GE_ID", GE_ID) 
      command.CommandType = CommandType.StoredProcedure 
      Dim adapter As SqlDataAdapter = New SqlDataAdapter(command) 
      adapter.Fill(dt) 
      con.Close() 
     End Using 
    Catch ex As SqlException 
     MessageBox.Show(ex.Message, "Data Input Error") 
    End Try 

    Return dt ' Note will be empty if stored proc call fails... 
End Function 

В идеале, вы должны обернуть соединение в использовании заявлении.

Смежные вопросы