|
Greetings!
How can I create a public function that returns a dataset?
So far this is what I have:
----------------------------------------------------------------- Public Function RetrieveDataSet(ByVal strSQL As String, ByVal strTableName As String) As DataTable Try Dim objConn As New SqlConnection(ConnectionString) Dim sdaProductData As DataSet Dim sdaProduct As New SqlDataAdapter(strSQL, objConn) sdaProduct.Fill(sdaProductData, strTableName) Return sdaProductData.Tables(strTableName) Catch ex As Exception Throw ex End Try End Function -----------------------------------------------------------------
Then in code I was trying to execute function this way...
'Create instance of database object Dim dsConn As pyiDatabase.Database = New pyiDatabase.Database
'Bind Data to DataGrid dgProducts.DataSource = dsConn.RetrieveDataSet(sql, "Products")
As you can see, I was trying to create a function that would return a dataTable. I tried returning the dataset but it didnt seem to work. Then I tried returning a dataTable by creating the dataset by creating it with a TableName and then returning the results of that table.
So in short, create a dataset with a tablename "Products" then return the datatable results. However, I get an error message here too. The error message is as followed:
Value cannot be Null. Parameter name: dataSet
I am trying to keep all database code in a class file called database.vb. Any suggestions on how to return a dataset or dataTable like above would be greatly appreciated.
Thanks, Gregg | | MigrationUser 1 Saturday, December 27, 2003 1:45 PM | Hi Gregg,
There is nothing magical or tricky about returning either a DataTable or a DataSet from a function. But you need to successfully create it first, then return it like any other object.
I realize that this code is a work in progress, but you need to focus first on getting the table or data set built right. Among other things, you aren't opening the connection.
Anyway, here is what you are doing in this code: * Creating a new connection object, presumably with a valid connection string * Creating a new dataset object variable * Creating a data adapter object passing SQL and the as yet unopened connection object <sort of okay, so far> * Using a data adapter with an unopened connection to fill a dataset that has no structure with some data that isn't mapped to anything. What are you trying to do here???? * Return a DataTable object from a DataSet that doesn't have anything in it
Do you want to return a data set or table? Let me know which you really want and I'll help with straightening out this code.
Don | | MigrationUser 1 Sunday, December 28, 2003 9:17 PM | Hey Don,
Thanks for the reply. I would like to try and return the dataSet. If you could help to get me jump started I would appreciate it. And out of curiosity, how different would it be to return the dataTable?
Thanks again!
Gregg | | MigrationUser 1 Monday, December 29, 2003 9:29 AM | Hi Gregg,
Okay, here is roughly what you should use. Note that I didn't test this so there may be syntax stuff, but this should give you the general idea.
Public Function RetrieveDataSet(ByVal strSQL As String, ByVal strTableName As String) _ As DataSet Try Dim objConn As New SqlConnection(ConnectionString) objConn.Open Dim dsProductData As DataSet Dim sdaProduct As New SqlDataAdapter(strSQL, objConn)
sdaProduct.Fill(dsProductData)
Return dsProductData Finally objConn.Close End Try End Function
Note also that I changed the Try block. There is no point in catching an exception if you're just going to throw it again. The only thing that gets you is slower code. I also opened the connection and closed it in the Finally block, so you never leave the connection open.
Changing it to a DataTable is pretty simple. Just change the As clause to a DataTable and create a DataTable instead of a DataSet. There isn't any reason to create a DataSet just to get at a DataTable. So the code would look something like this:
Public Function RetrieveDataSet(ByVal strSQL As String, ByVal strTableName As String) _ As <b>DataTable</b> Try Dim objConn As New SqlConnection(ConnectionString) objConn.Open <b>Dim dtProductData As DataTable</b> Dim sdaProduct As New SqlDataAdapter(strSQL, objConn)
sdaProduct.Fill(<b>dtProductData</b>)
Return <b>dtProductData</b> Finally objConn.Close End Try End FunctionDon | | MigrationUser 1 Monday, December 29, 2003 1:27 PM | Hey Don,
Thanks for the help. I appreciate you explaining my errors. I didnt realize I wasnt actually opening the connection. :-0
The "Try - Finally" modification was a good point as well. I saw that in a book and copied what they had.
Thanks again!
Gregg | | MigrationUser 1 Monday, December 29, 2003 1:46 PM |
|