Results 1 to 8 of 8

Thread: Return Dataset From A Function ...

  1. #1

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    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:
    1. Public Function ViewAllFlightDetails() As Data.DataSet()
    2.  
    3.     Dim strSQL As String
    4.     Dim objDAFlightInfo As SqlClient.SqlDataAdapter
    5.  
    6.     strSQL = "SELECT * FROM aTable"
    7.     objDAFlightInfo = New SqlClient.SqlDataAdapter(strSQL, gstrDEVSQLSERVERCONN)
    8.  
    9.     If Not (objDAFlightInfo Is Nothing) Then
    10.         objDAFlightInfo.Fill(New Data.DataSet(ViewAllFlightDetails))
    11.     End If
    12.  
    13. End Function

    Can anyone shed some light on what could be behind this problem? Thanks!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  2. #2
    Member
    Join Date
    Nov 2002
    Location
    Austria
    Posts
    52
    try without () after ViewAllFlightDetails

    Public Function ViewAllFlightDetails As Data.DataSet()
    ...
    End Function

  3. #3

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    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'."

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  4. #4

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    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:
    1. Dim strSQL As String
    2. Dim objDAFlightInfo As SqlClient.SqlDataAdapter
    3. Dim objDSTemp As Data.DataSet = New Data.DataSet()
    4.  
    5. strSQL = "SELECT * FROM aTable"
    6. objDAFlightInfo = New SqlClient.SqlDataAdapter(strSQL, gstrDEVSQLSERVERCONN)
    7.  
    8. If Not (objDAFlightInfo Is Nothing) Then
    9.     objDAFlightInfo.Fill(objDSTemp)
    10.     Return objDSTemp
    11. End If

    If any gurus could help to explain why the first piece of code failed though, It'd be much appreciated!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  5. #5
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    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.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  6. #6

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    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:
    1. Private function setstringval() as string
    2.     setstringval = "HelloWorld"
    3. end function

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  7. #7
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    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:
    1. Public Function ViewAllAuthors() As DataSet
    2.     Dim connString As String = "user id=sa;password=sa;database=pubs;server=DeathAngel;"
    3.     Dim sql As String = "Select * From Authors"
    4.     Dim da As SqlDataAdapter
    5.     da = New SqlDataAdapter(sql, connString)
    6.     ViewAllAuthors = New DataSet("ViewAllAuthors")
    7.     da.Fill(ViewAllAuthors)
    8. End Function

  8. #8

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Right-O, that's explained it now, thanks to both of you!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width