This is an issue of scope. Variables declared locally in a function cannot
be referenced outside that function. If something needs to be accessed in
multiple functions, it should be declared as a member variable of the class.
In the case of a data reader, I would not recommend having it be a class
level variable. For it to be of any use it would need to maintain an open
database connection, and that is not advisable. I would say just open the
connection in your click event as needed, run your query, get your data,
close the connection - all within the click event.
The problem you are describing is not actually related to ADO.NET in any
way, but in not understanding variable scoping. I recommend you pick up a
book on C# and read up on its structure and some coding techniques.
> Hi,
>
[quoted text clipped - 18 lines]
> Thanks,
> Doru
Doru Roman - 27 Jan 2006 19:22 GMT
I realize it is a matter of scope, that is why I asked for a solution in
case I need to navigate through the records at a push of a button. I gather
that this is not possible.
Marina Levit [MVP] - 27 Jan 2006 20:28 GMT
I told you how you can do it - declare the variables not inside a particular
function, but at the class level. So it's not impossible. I just warned
against having an open data reader just sitting around on a form, that is a
bad database access practice.
>I realize it is a matter of scope, that is why I asked for a solution in
>case I need to navigate through the records at a push of a button. I gather
>that this is not possible.
Sahil Malik [MVP C#] - 27 Jan 2006 22:10 GMT
Hey Marina,
Are you the Marina we've been seeing in this NG for so long now? If so, I'm
happy you got MVP :). Congrats and well deserved indeed.
- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
----------------------------------------------------------------------------
>I told you how you can do it - declare the variables not inside a
>particular function, but at the class level. So it's not impossible. I
[quoted text clipped - 4 lines]
>>case I need to navigate through the records at a push of a button. I
>>gather that this is not possible.
Marina Levit [MVP] - 30 Jan 2006 20:59 GMT
Oh, and yeah, same Marina :)
> Hey Marina,
>
[quoted text clipped - 14 lines]
>>>case I need to navigate through the records at a push of a button. I
>>>gather that this is not possible.
W.G. Ryan eMVP - 28 Jan 2006 05:40 GMT
Marina:
I noticed the same thing Sahil did. It's an honor to have you on board!
>I told you how you can do it - declare the variables not inside a
>particular function, but at the class level. So it's not impossible. I
[quoted text clipped - 4 lines]
>>case I need to navigate through the records at a push of a button. I
>>gather that this is not possible.
Marina Levit [MVP] - 30 Jan 2006 13:52 GMT
Thanks Sahil and William :)
>I told you how you can do it - declare the variables not inside a
>particular function, but at the class level. So it's not impossible. I
[quoted text clipped - 4 lines]
>>case I need to navigate through the records at a push of a button. I
>>gather that this is not possible.
Doru Roman - 27 Jan 2006 21:18 GMT
I declared the datareader as a variable of the Form class and it works.
> This is an issue of scope. Variables declared locally in a function cannot
> be referenced outside that function. If something needs to be accessed in
[quoted text clipped - 33 lines]
>> Thanks,
>> Doru
Cor Ligthert [MVP] - 28 Jan 2006 07:48 GMT
Marina,
I am happy for the recognition of your work.
Although that you was for me of course already onboard of these newsgroups
is it always nice to see as others as well award you for that.
My congratulations,
Cor
> This is an issue of scope. Variables declared locally in a function cannot
> be referenced outside that function. If something needs to be accessed in
[quoted text clipped - 33 lines]
>> Thanks,
>> Doru
Marina Levit [MVP] - 30 Jan 2006 13:52 GMT
Thank you Cor :)
> Marina,
>
[quoted text clipped - 44 lines]
>>> Thanks,
>>> Doru