0

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.

0

2 Answers 2

1

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.

2
  • if you found my answer useful you may consider marking it as "best/useful answer" or either vote up any answer which was helpful to you - thx in advance Commented Aug 16, 2012 at 12:32
  • The "return nothing" is useful, the "byref" approach should be avoided. Upvoted to stop the community user bumping this ancient, relatively useless question
    – Caius Jard
    Commented Aug 27, 2020 at 13:32
0

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

1
  • actually i removed get table so it worked thank u anyway for ur help
    – charbel
    Commented Aug 16, 2012 at 12:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.