|
-
Nov 18th, 2002, 09:43 AM
#1
Thread Starter
Evil Genius
Return Dataset From A Function ...
Hi everyone, the following code is meant to grab some data from my sql server database and return it in a dataset from the function, at the moment though, it fails highlightint the fill line with the following:
vb(37): Value of type '1-dimensional array of System.Data.DataSet' cannot be converted to 'String'.
VB Code:
Public Function ViewAllFlightDetails() As Data.DataSet()
Dim strSQL As String
Dim objDAFlightInfo As SqlClient.SqlDataAdapter
strSQL = "SELECT * FROM aTable"
objDAFlightInfo = New SqlClient.SqlDataAdapter(strSQL, gstrDEVSQLSERVERCONN)
If Not (objDAFlightInfo Is Nothing) Then
objDAFlightInfo.Fill(New Data.DataSet(ViewAllFlightDetails))
End If
End Function
Can anyone shed some light on what could be behind this problem? Thanks!
-
Nov 18th, 2002, 09:52 AM
#2
Member
try without () after ViewAllFlightDetails
Public Function ViewAllFlightDetails As Data.DataSet()
...
End Function
-
Nov 18th, 2002, 09:56 AM
#3
Thread Starter
Evil Genius
Thanks for the suggestion, but I get a new error as below, god knows where the string is picked up/why vb is trying to guess at the datatype as I've declared all the objects properly...
"Value of type 'System.Data.DataSet' cannot be converted to 'String'."
-
Nov 18th, 2002, 10:02 AM
#4
Thread Starter
Evil Genius
The following seems to work (not fully tested in runtime yet, but the build now succeeds for my project & the squiggly lines have all gone...
VB Code:
Dim strSQL As String
Dim objDAFlightInfo As SqlClient.SqlDataAdapter
Dim objDSTemp As Data.DataSet = New Data.DataSet()
strSQL = "SELECT * FROM aTable"
objDAFlightInfo = New SqlClient.SqlDataAdapter(strSQL, gstrDEVSQLSERVERCONN)
If Not (objDAFlightInfo Is Nothing) Then
objDAFlightInfo.Fill(objDSTemp)
Return objDSTemp
End If
If any gurus could help to explain why the first piece of code failed though, It'd be much appreciated!
-
Nov 18th, 2002, 10:18 AM
#5
this line makes no sense from your old function
objDAFlightInfo.Fill(New Data.DataSet(ViewAllFlightDetails))
you were never setting the function to return any value and you keep calling itself from itself. Code makes no sense.
-
Nov 18th, 2002, 10:39 AM
#6
Thread Starter
Evil Genius
I thought this was the equivalent of ...
set ViewAllFlightDetails = new dataset
objDAFlightInfo.Fill(ViewAllFlightDetails)
Where I'm initialising & populating the returned dataset. Do you have to use the return keyword in vb.net now, i.e. would this still work:
VB Code:
Private function setstringval() as string
setstringval = "HelloWorld"
end function
-
Nov 18th, 2002, 10:42 AM
#7
Hyperactive Member
this error:
Code:
Value of type 'System.Data.DataSet' cannot be converted to 'String'.
is being generated because of this line:
Code:
objDAFlightInfo.Fill(New Data.DataSet(ViewAllFlightDetails))
The dataset has two constructors, one with no params and the other with a string param which is the name of the dataset you're creating. ViewAllFlightDetails is the name of your function which is of type dataset (not string) hence the error msg. As Cander said that doesn't make any sense even if you could pass in a dataset as a param. Probably be stuck in a loop until you get a stack overflow or something. Here's how i'd do it:
VB Code:
Public Function ViewAllAuthors() As DataSet
Dim connString As String = "user id=sa;password=sa;database=pubs;server=DeathAngel;"
Dim sql As String = "Select * From Authors"
Dim da As SqlDataAdapter
da = New SqlDataAdapter(sql, connString)
ViewAllAuthors = New DataSet("ViewAllAuthors")
da.Fill(ViewAllAuthors)
End Function
-
Nov 18th, 2002, 11:00 AM
#8
Thread Starter
Evil Genius
Right-O, that's explained it now, thanks to both of you!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|