2012-08-16 33 views
0

转换为system.data.datatable”,这是我的代码保持有错误‘字符串类型的值不能转化为system.data.datatable’值不能在vb.net

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 

部分,其中错误是gettable =“1”,下面 请我需要帮助尽快 问候

回答

0

你的功能GetTable返回DataTable

你不能转换"1"DataTable的消息表明,这是什么就行了发生GetTable = "1"

如果你想返回DataTable并设置一个标志,看到状态;改变你的函数定义为这样:

Function GetTable(byref result as Integer) As DataTable 

然后代替GetTable = "1"更改为result = 1。这样,您就可以检查结果值,并返回一个DataTable:

Dim res as Integer 
Dim dt as DataTable = GetTable(res) 
If res = 1 then 
    'It worked! 
End If 

侧面说明:打开选项严格在

+0

其实我删除拿到表,所以它的工作感谢ü反正UR帮助 – charbel 2012-08-16 12:22:58

0

GetTable = "1"表明您要设置你的函数的returnValue。由于你的函数定义为Function GetTable() As DataTable你的编译器显示错误!

下面几行有一个正确的回报(Return table),所以我不太确定你的目标是GetTable = "1"

我假设你想有一个额外的returnValue指示你的函数调用是否成功。但事实上,函数可能只有一个returnValue。

你可以选择你的表VAR设置为无,或使用参考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 

在解决方案2,你也可以选择说明你的呼叫布尔参数是成功还是失败。

+0

感谢ü它的工作 – charbel 2012-08-16 12:22:29

+0

,如果你发现我的回答有用的,你可以考虑将其标记为“最好的/有用的答案”或任一投票了这是有帮助的任何答案给你 - thx提前 – 2012-08-16 12:32:59