Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / ADO.NET / January 2006

Tip: Looking for answers? Try searching our database.

Where to use connection, dataset, datareader ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Doru Roman - 27 Jan 2006 19:01 GMT
Hi,

I have some difficulties in understanding how to use the connection,
dataset, datareader in a form.
Let's suppose I want to create a datareader and when I push a button to
bring another row from the dataset.
Supposedly I create under MyForm_Load event the connection, then a command
and the reader.

SqlConnection conn = new SqlConnection(...);
conn.open();
SqlCommand cmd = conn.CreateCommnad();
cmd.CommandType = ...
cmdCommnadText = ....
SqlReader dr = cmd.ExecuteReader();

If I try to use dr.Read() in side a ButtonRead_Click event, dr is not
recognized.
So, what is the approach here, where I create and use the components to make
them work?
Thanks,
Doru
Marina Levit [MVP] - 27 Jan 2006 19:09 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.