Reputation: 1
This is my code. I keep having an error "value of type string cannot be converted to system.data.datatable"
Function GetTable() As DataTable
Dim SQLConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("Zeinchatconnection").ToString())
Dim CommSQL As New SqlClient.SqlCommand
Dim ChatDataAdapter As SqlDataAdapter
Dim paramSQL As SqlClient.SqlParameter
Dim DStable As DataSet
Dim table As New DataTable
Dim szName As String = ""
Dim szNumber As String = ""
Try
If SQLConnection.State = ConnectionState.Closed Then
SQLConnection.Open()
End If
CommSQL.Connection = SQLConnection
CommSQL.CommandType = CommandType.StoredProcedure
CommSQL.CommandText = "spc_newselect"
CommSQL.ExecuteNonQuery()
ChatDataAdapter = New SqlDataAdapter(CommSQL)
ChatDataAdapter.Fill(DSTable)
table.Rows.Clear()
table.Clear()
table = DStable.Tables(0)
Dim i As Integer = 0
For i = 0 To table.Rows.Count - 1
szName = szName & " " & table.Rows(i).Item(0) & table.Rows(i).Item(1)
szNumber = szNumber & " " & table.Rows(i).Item(0) & table.Rows(i).Item(1)
Next
GetTable = "1"
Catch ex As System.Data.SqlClient.SqlException
GetTable = "0"
Catch ex As Exception
GetTable = "0"
If (IsNothing(ChatDataAdapter) = False) Then ChatDataAdapter.Dispose()
If (IsNothing(CommSQL) = False) Then CommSQL.Dispose()
SQLConnection.Close()
End Try
Return table
End Function
The part where the errors are is gettable= "1" and below.
Upvotes: 0
Views: 9815
Reputation: 8359
GetTable = "1"
indicates that you want to set the returnValue of your function. As your function is defined as Function GetTable() As DataTable
your compiler shows an error!
A few lines below there is a correct return stmt (Return table
) so I am not quite sure what your goal is with GetTable = "1"
?
I would assume that you would like to have an additional returnValue indicating whether your function call was successful or not. But as a matter of fact functions may only have one returnValue.
You may choose to set your table var to nothing, or use a ref param ...
' possible solution 1 - using nothing value
Function GetTable() As DataTable
Try
' your code goes here
' remove GetTable = "1"
Catch ex as Exception
' change GetTable = "0" to
table = nothing
End Try
' other code ...
End Function
' possible solution 2 - ref param
Function GetTable(ByRef status as integer) as DataTable
Try
' your code goes here
' remove GetTable = "1"
status = 1
Catch ex as Exception
' change GetTable = "0" to
status = 0
End Try
' other code ...
End Function
At solution 2 you may also choose a boolean parameter indicating your call was successful or not.
Upvotes: 1
Reputation: 27322
Your function GetTable
returns a DataTable
you can't convert "1"
to a DataTable
as the message suggests which is what is happening on the line GetTable = "1"
If you want to return a DataTable
and Set a flag to see the status; change your function definition as so:
Function GetTable(byref result as Integer) As DataTable
Then instead of GetTable = "1"
change this to result = 1
. This way you can inspect the result value and return a DataTable:
Dim res as Integer
Dim dt as DataTable = GetTable(res)
If res = 1 then
'It worked!
End If
Side Note: Turn Option Strict On
Upvotes: 0