CodeManiac
asked on
Function to Return DataSet
I want to write a function to return the results of a SQL query as a dataset. I'm calling it like this:
Dim dsResults As Data.DataSet = New Data.DataSet
dsResults = databaseFunctions.ExecuteQ uery("SELE CT * FROM tUsers")
But I keep getting this error on the second line:
System.NullReferenceExcept ion: Object reference not set to an instance of an object.
Function ExecuteQuery(ByVal txtQuery As String) As Data.DataSet
OpenDB()
cmd = New SqlClient.SqlCommand
adItem = New SqlClient.SqlDataAdapter
dsResults = New Data.DataSet
cmd = New SqlClient.SqlCommand(txtQu ery, cn)
adItem.SelectCommand = cmd
adItem.Fill(dsResults)
ExecuteQuery = dsResults
CloseDB()
End Function
Dim dsResults As Data.DataSet = New Data.DataSet
dsResults = databaseFunctions.ExecuteQ
But I keep getting this error on the second line:
System.NullReferenceExcept
Function ExecuteQuery(ByVal txtQuery As String) As Data.DataSet
OpenDB()
cmd = New SqlClient.SqlCommand
adItem = New SqlClient.SqlDataAdapter
dsResults = New Data.DataSet
cmd = New SqlClient.SqlCommand(txtQu
adItem.SelectCommand = cmd
adItem.Fill(dsResults)
ExecuteQuery = dsResults
CloseDB()
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
is option explicit turned off?
ASKER
Thanks Torrwin!
Can you give an example of how you call your function?
Do you declare a dataSet variable like I tried above?
Can you give an example of how you call your function?
Do you declare a dataSet variable like I tried above?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Paul is right, that's how I essentialy do it:
Dim ds as New Dataset
Dim sSQL as String
sSQL = "SELECT * FROM AUTHORS"
ds = SelectCommand(sSQL)
Dim ds as New Dataset
Dim sSQL as String
sSQL = "SELECT * FROM AUTHORS"
ds = SelectCommand(sSQL)
ASKER
I'm getting an "Object reference not set to an instance of an object." error when I call the function like this...
Dim ds As Data.DataSet = New Data.DataSet
ds = databaseFunctions.SelectCo
With ds.Tables(0)
For i = 0 To .Rows.Count - 1
Response.Write("test" & i)
Next
End With
Any ideas?
Post your answer at the following URL to get another 500 points...
https://www.experts-exchange.com/questions/21393393/Object-reference-not-set-to-an-instance-of-an-object.html
Thanks!