2012-08-16 27 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