|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Where to use connection, dataset, datareader ?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 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. Show quote "Doru Roman" <doruro***@rogers.com> wrote in message news:O6bUfQ3IGHA.1832@TK2MSFTNGP11.phx.gbl... > 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 > > > 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. 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. Show quote "Doru Roman" <doruro***@rogers.com> wrote in message news:OrDNFc3IGHA.3904@TK2MSFTNGP10.phx.gbl... >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. > > 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 ---------------------------------------------------------------------------- Show quote "Marina Levit [MVP]" <someone@nospam.com> wrote in message news:OcSKwA4IGHA.964@tk2msftngp13.phx.gbl... >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. > > "Doru Roman" <doruro***@rogers.com> wrote in message > news:OrDNFc3IGHA.3904@TK2MSFTNGP10.phx.gbl... >>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. >> >> > > Oh, and yeah, same Marina :)
Show quote "Sahil Malik [MVP C#]" <contactmethrumyblog@nospam.com> wrote in message news:%23s0NN64IGHA.3752@TK2MSFTNGP11.phx.gbl... > 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 > ---------------------------------------------------------------------------- > > > > "Marina Levit [MVP]" <someone@nospam.com> wrote in message > news:OcSKwA4IGHA.964@tk2msftngp13.phx.gbl... >>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. >> >> "Doru Roman" <doruro***@rogers.com> wrote in message >> news:OrDNFc3IGHA.3904@TK2MSFTNGP10.phx.gbl... >>>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:
I noticed the same thing Sahil did. It's an honor to have you on board! Show quote "Marina Levit [MVP]" <someone@nospam.com> wrote in message news:OcSKwA4IGHA.964@tk2msftngp13.phx.gbl... >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. > > "Doru Roman" <doruro***@rogers.com> wrote in message > news:OrDNFc3IGHA.3904@TK2MSFTNGP10.phx.gbl... >>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. >> >> > > Thanks Sahil and William :)
Show quote "Marina Levit [MVP]" <someone@nospam.com> wrote in message news:OcSKwA4IGHA.964@tk2msftngp13.phx.gbl... >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. > > "Doru Roman" <doruro***@rogers.com> wrote in message > news:OrDNFc3IGHA.3904@TK2MSFTNGP10.phx.gbl... >>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. >> >> > > I declared the datareader as a variable of the Form class and it works.
Show quote "Marina Levit [MVP]" <someone@nospam.com> wrote in message news:%23tCO1U3IGHA.2900@TK2MSFTNGP14.phx.gbl... > 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. > > "Doru Roman" <doruro***@rogers.com> wrote in message > news:O6bUfQ3IGHA.1832@TK2MSFTNGP11.phx.gbl... >> 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,
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 Show quote "Marina Levit [MVP]" <someone@nospam.com> schreef in bericht news:%23tCO1U3IGHA.2900@TK2MSFTNGP14.phx.gbl... > 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. > > "Doru Roman" <doruro***@rogers.com> wrote in message > news:O6bUfQ3IGHA.1832@TK2MSFTNGP11.phx.gbl... >> 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 >> >> >> > > Thank you Cor :)
Show quote "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message news:uiz0a89IGHA.1728@TK2MSFTNGP14.phx.gbl... > 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 > > "Marina Levit [MVP]" <someone@nospam.com> schreef in bericht > news:%23tCO1U3IGHA.2900@TK2MSFTNGP14.phx.gbl... >> 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. >> >> "Doru Roman" <doruro***@rogers.com> wrote in message >> news:O6bUfQ3IGHA.1832@TK2MSFTNGP11.phx.gbl... >>> 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 >>> >>> >>> >> >> > > |
|||||||||||||||||||||||