Instantiate objects

D

Daniel Walzenbach

Chris,

does your dataset contain a table(0) in Sub1? And is sub2 called directly
after sub1 fires? Do you have a postback between accessing sub1 and sub2? If
so your variables are lost. If you want to prevent varibales between
postbacks you have to store them in viewstate, sessionstate or cache or
whatever fits your need.

Daniel
 
S

Scott Allen

You'll want to post more code so we can see the problem. It sounds as
if you didn't add the DataTable object to the DataSet object's table
collection, but it's hard to tell without some code.
 
D

Daniel Walzenbach

Chris,

what error do you get? Place in a try...catch block and see what error you
get. It's usually gut to do sth. like the following if you access tables in
a dataset:

if myDataSet.Contains("myTable") then

' your code

end if

of course you should check if there are any rows in your datatable before
accessing them

if myDataSet.("myTable").rows.count>0 then

' your code

end if

and if your resultset is not predictable you can check if it contains
specific items as well

if myDataSet.("myTable").columns().contains("myItem") then

' your code

end if


But in your case it's important to first make sure if your dataset contains
a table with results. What you should do is to start sql profiler and check
the sql executed against your database. This will help you to understand
what happens behind the scenes and will help you to trace your problem

Daniel
 
D

Daniel Walzenbach

If you declare it you don't need to instantiate it if this is what you do
later in a sub. But regarding your question, if you instantiate objds in a
sub (and it is declared on a page leveö) you can access it an any other sub
as long as there is no postback in between.

Daniel
 
C

Chris Kennedy

I have a dataset, datatable and data row which I have declared public in my
class. I instantiate them in one sub routine and go to use them again and I
get an 'Cannot find table 0' wrror

Public objds as new dataset()
Public objDataTable As new DataTable()
Public objRow As DataRow

Sub 1
Create dataset and use sucessfully
end sub

Sub 2
objds.tables(0).rows(0)(XMLItem("fieldname"))
End sub
 
C

Chris Kennedy

No there is no post back.

I have a create dataset function


Dim objConnection As New System.Data.SqlClient.SqlConnection(StrConnection)
objDataAdapter = New System.Data.SqlClient.SqlDataAdapter(StrSQL,
objConnection)
objBuilder = New SqlClient.SqlCommandBuilder(objDataAdapter)
objDataAdapter.UpdateCommand = objBuilder.GetUpdateCommand()
objDataAdapter.InsertCommand = objBuilder.GetInsertCommand()
objDataAdapter.DeleteCommand = objBuilder.GetDeleteCommand()
objDataAdapter.Fill(objDataSet,0)
return objDataSet

end function

I call it in a sub (called from the render controls in a custom control)

Dim objds as new Dataset()
objds = CreateDataSet(strConnection,strSQL)

In the render sub
I try

objds.tables(0).rows(0)(fieldname)

I get the error. The only way around it is to recreste the dataset. Anyideas
where I am going wrong.
 
C

Chris Kennedy

If you declare a dataset

Public objds as new dataset()

as your dataset and then do:

dim objds as new dataset() within a sub does it lose it's public scope.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top