What used to be simple is now simply confusing

What used to be simple is now simply confusing

am 26.12.2007 17:15:00 von Jim

Yes, a newbie here.

Though I am making progress, slowly, I am also getting more and more
confused.

With ASP, when I wanted to do something as trivial as updating a visitor
counter, I connected to a database, executed a SQL command to read the
current value of a field into a recordset, updated the value by adding 1 and
writing the field back to the table, closed and got rid of the connection
and recordset. I had no concerns about how to get or use the information
read, or manipulate it.

Now I am confronted with issues such as whether or not to use a connected or
disconnected database access. Do I really need to build a dataadapter and
in-memory table for such a trivial function? Do I really need to be
concerned about data binding? Do I really need to use a databound control
at all?

Will someone please post the minimum ASP.NET instructions to perform this
trivial function for me? I suspect that under ASP.NET it is just as trivial
as it was under ASP, but as I said earlier, what used to be simple is now
simply confusing.

Thank you.

Re: What used to be simple is now simply confusing

am 26.12.2007 17:27:02 von Scott Roberts

"James R. Davis" wrote in message
news:uCoAXr9RIHA.4684@TK2MSFTNGP06.phx.gbl...
> Yes, a newbie here.
>
> Though I am making progress, slowly, I am also getting more and more
> confused.
>
> With ASP, when I wanted to do something as trivial as updating a visitor
> counter, I connected to a database, executed a SQL command to read the
> current value of a field into a recordset, updated the value by adding 1
> and
> writing the field back to the table, closed and got rid of the connection
> and recordset. I had no concerns about how to get or use the information
> read, or manipulate it.
>
> Now I am confronted with issues such as whether or not to use a connected
> or
> disconnected database access. Do I really need to build a dataadapter and
> in-memory table for such a trivial function? Do I really need to be
> concerned about data binding? Do I really need to use a databound control
> at all?
>
> Will someone please post the minimum ASP.NET instructions to perform this
> trivial function for me? I suspect that under ASP.NET it is just as
> trivial
> as it was under ASP, but as I said earlier, what used to be simple is now
> simply confusing.
>
> Thank you.
>
>

SqlConnection conn = new SqlConnection("YourConnectionString");
SqlCommand cmd = new SqlCommand("update MyTable set UserCounter =
UserCounter + 1", conn);
using (conn)
{
conn.Open();
cmd.ExecuteNonQuery();
}

Re: What used to be simple is now simply confusing

am 26.12.2007 17:41:04 von smar

The process doesn't need to be that much different than what you are doing
now albeit that some of the object have changed.

You still need a connection to the database and you can then just execute an
update statement to modify the db value.

This is not a job for DataAdapters and DataSets (disconnected data).



"James R. Davis" wrote in message
news:uCoAXr9RIHA.4684@TK2MSFTNGP06.phx.gbl...
> Yes, a newbie here.
>
> Though I am making progress, slowly, I am also getting more and more
> confused.
>
> With ASP, when I wanted to do something as trivial as updating a visitor
> counter, I connected to a database, executed a SQL command to read the
> current value of a field into a recordset, updated the value by adding 1
> and
> writing the field back to the table, closed and got rid of the connection
> and recordset. I had no concerns about how to get or use the information
> read, or manipulate it.
>
> Now I am confronted with issues such as whether or not to use a connected
> or
> disconnected database access. Do I really need to build a dataadapter and
> in-memory table for such a trivial function? Do I really need to be
> concerned about data binding? Do I really need to use a databound control
> at all?
>
> Will someone please post the minimum ASP.NET instructions to perform this
> trivial function for me? I suspect that under ASP.NET it is just as
> trivial
> as it was under ASP, but as I said earlier, what used to be simple is now
> simply confusing.
>
> Thank you.
>
>

Re: What used to be simple is now simply confusing

am 26.12.2007 17:45:19 von Jim

Amazingly simple when you step away from the details. That accomplished 90%
of what I need. Thank you!

I assume you still must close or dispose of the connection. All that I need
now is to know how to gain access to the new value to set the text value of
a label. Can I get that value without having to re-read the field?

>
> SqlConnection conn = new SqlConnection("YourConnectionString");
> SqlCommand cmd = new SqlCommand("update MyTable set UserCounter =
> UserCounter + 1", conn);
> using (conn)
> {
> conn.Open();
> cmd.ExecuteNonQuery();
> }
>

Re: What used to be simple is now simply confusing

am 26.12.2007 18:08:17 von Scott Roberts

The "using" statement automatically disposes of the connection (which also
closes it). You can explicitly call "conn.Close();" if it makes you feel
better. :)

You'll need another command and a DataReader to get the new value.

// Goes right after "ExecuteNonQuery".
SqlCommand scmd = new SqlCommand("select UserCounter from MyTable", conn);
SqlDataReader dr = scmd.ExecuteReader();
if (dr.Read())
Label2.Text = dr.GetString(0);


"James R. Davis" wrote in message
news:uymoU89RIHA.4740@TK2MSFTNGP02.phx.gbl...
> Amazingly simple when you step away from the details. That accomplished
> 90%
> of what I need. Thank you!
>
> I assume you still must close or dispose of the connection. All that I
> need
> now is to know how to gain access to the new value to set the text value
> of
> a label. Can I get that value without having to re-read the field?
>
>>
>> SqlConnection conn = new SqlConnection("YourConnectionString");
>> SqlCommand cmd = new SqlCommand("update MyTable set UserCounter =
>> UserCounter + 1", conn);
>> using (conn)
>> {
>> conn.Open();
>> cmd.ExecuteNonQuery();
>> }
>>
>
>

Re: What used to be simple is now simply confusing

am 26.12.2007 18:13:31 von Scott Roberts

Note also that the "old" way you were also executing 2 statements - 1 to
read then 1 to update. Now we're still executing 2 statements, but we're
doing the update first then the read. This is "safer" because the DB handles
concurrency issues for us.

> The "using" statement automatically disposes of the connection (which also
> closes it). You can explicitly call "conn.Close();" if it makes you feel
> better. :)
>
> You'll need another command and a DataReader to get the new value.
>
> // Goes right after "ExecuteNonQuery".
> SqlCommand scmd = new SqlCommand("select UserCounter from MyTable", conn);
> SqlDataReader dr = scmd.ExecuteReader();
> if (dr.Read())
> Label2.Text = dr.GetString(0);
>
>
> "James R. Davis" wrote in message
> news:uymoU89RIHA.4740@TK2MSFTNGP02.phx.gbl...
>> Amazingly simple when you step away from the details. That accomplished
>> 90%
>> of what I need. Thank you!
>>
>> I assume you still must close or dispose of the connection. All that I
>> need
>> now is to know how to gain access to the new value to set the text value
>> of
>> a label. Can I get that value without having to re-read the field?
>>
>>>
>>> SqlConnection conn = new SqlConnection("YourConnectionString");
>>> SqlCommand cmd = new SqlCommand("update MyTable set UserCounter =
>>> UserCounter + 1", conn);
>>> using (conn)
>>> {
>>> conn.Open();
>>> cmd.ExecuteNonQuery();
>>> }
>>>
>>
>>
>

Re: What used to be simple is now simply confusing

am 26.12.2007 18:16:51 von Jim

Scott,

You are amazingly generous with your help. Again, thank you!

"Scott Roberts" wrote in
message news:Ofk8iK%23RIHA.748@TK2MSFTNGP04.phx.gbl...
> Note also that the "old" way you were also executing 2 statements - 1 to
> read then 1 to update. Now we're still executing 2 statements, but we're
> doing the update first then the read. This is "safer" because the DB
handles
> concurrency issues for us.
>
> > The "using" statement automatically disposes of the connection (which
also
> > closes it). You can explicitly call "conn.Close();" if it makes you feel
> > better. :)
> >
> > You'll need another command and a DataReader to get the new value.
> >
> > // Goes right after "ExecuteNonQuery".
> > SqlCommand scmd = new SqlCommand("select UserCounter from MyTable",
conn);
> > SqlDataReader dr = scmd.ExecuteReader();
> > if (dr.Read())
> > Label2.Text = dr.GetString(0);
> >
> >
> > "James R. Davis" wrote in message
> > news:uymoU89RIHA.4740@TK2MSFTNGP02.phx.gbl...
> >> Amazingly simple when you step away from the details. That
accomplished
> >> 90%
> >> of what I need. Thank you!
> >>
> >> I assume you still must close or dispose of the connection. All that I
> >> need
> >> now is to know how to gain access to the new value to set the text
value
> >> of
> >> a label. Can I get that value without having to re-read the field?
> >>
> >>>
> >>> SqlConnection conn = new SqlConnection("YourConnectionString");
> >>> SqlCommand cmd = new SqlCommand("update MyTable set UserCounter =
> >>> UserCounter + 1", conn);
> >>> using (conn)
> >>> {
> >>> conn.Open();
> >>> cmd.ExecuteNonQuery();
> >>> }
> >>>
> >>
> >>
> >
>

Re: What used to be simple is now simply confusing

am 26.12.2007 18:29:44 von Coskun

Hi,

To make some additions...

If you still insist on using only one command, you can execute both queries
at once and you can get the returning value using "ExecuteScalar" method of
the SqlCommand class. Just simple changes to what Scott wrote. I am just now
sure about if you need "+1" in second SQL statement, I haven't executed the
code but you still may need to save or remove it.

int currentCounter = 0;
SqlConnection conn = new SqlConnection("YourConnectionString");
SqlCommand cmd = new SqlCommand("update MyTable set UserCounter =
UserCounter + 1; Select UserCounter + 1 from MyTable;", conn);
using (conn)
{
conn.Open();
currentCounter = cmd.ExecuteScalar();
}


--
All the best,
Coskun SUNALI
MVP ASP/ASP.NET
http://sunali.com


"James R. Davis" wrote in message
news:O2HG8N%23RIHA.4196@TK2MSFTNGP04.phx.gbl...
> Scott,
>
> You are amazingly generous with your help. Again, thank you!
>
> "Scott Roberts" wrote in
> message news:Ofk8iK%23RIHA.748@TK2MSFTNGP04.phx.gbl...
>> Note also that the "old" way you were also executing 2 statements - 1 to
>> read then 1 to update. Now we're still executing 2 statements, but we're
>> doing the update first then the read. This is "safer" because the DB
> handles
>> concurrency issues for us.
>>
>> > The "using" statement automatically disposes of the connection (which
> also
>> > closes it). You can explicitly call "conn.Close();" if it makes you
>> > feel
>> > better. :)
>> >
>> > You'll need another command and a DataReader to get the new value.
>> >
>> > // Goes right after "ExecuteNonQuery".
>> > SqlCommand scmd = new SqlCommand("select UserCounter from MyTable",
> conn);
>> > SqlDataReader dr = scmd.ExecuteReader();
>> > if (dr.Read())
>> > Label2.Text = dr.GetString(0);
>> >
>> >
>> > "James R. Davis" wrote in message
>> > news:uymoU89RIHA.4740@TK2MSFTNGP02.phx.gbl...
>> >> Amazingly simple when you step away from the details. That
> accomplished
>> >> 90%
>> >> of what I need. Thank you!
>> >>
>> >> I assume you still must close or dispose of the connection. All that
>> >> I
>> >> need
>> >> now is to know how to gain access to the new value to set the text
> value
>> >> of
>> >> a label. Can I get that value without having to re-read the field?
>> >>
>> >>>
>> >>> SqlConnection conn = new SqlConnection("YourConnectionString");
>> >>> SqlCommand cmd = new SqlCommand("update MyTable set UserCounter =
>> >>> UserCounter + 1", conn);
>> >>> using (conn)
>> >>> {
>> >>> conn.Open();
>> >>> cmd.ExecuteNonQuery();
>> >>> }
>> >>>
>> >>
>> >>
>> >
>>
>
>

Re: What used to be simple is now simply confusing

am 27.12.2007 02:15:37 von LVP

I am just wondering:
How can you guarantee that no one else updates the UserCounter in the table
before you read the UserCounter

LVP



"Coskun SUNALI [MVP]" wrote in message
news:%23Mo7pT%23RIHA.4684@TK2MSFTNGP06.phx.gbl...
> Hi,
>
> To make some additions...
>
> If you still insist on using only one command, you can execute both
> queries at once and you can get the returning value using "ExecuteScalar"
> method of the SqlCommand class. Just simple changes to what Scott wrote. I
> am just now sure about if you need "+1" in second SQL statement, I haven't
> executed the code but you still may need to save or remove it.
>
> int currentCounter = 0;
> SqlConnection conn = new SqlConnection("YourConnectionString");
> SqlCommand cmd = new SqlCommand("update MyTable set UserCounter =
> UserCounter + 1; Select UserCounter + 1 from MyTable;", conn);
> using (conn)
> {
> conn.Open();
> currentCounter = cmd.ExecuteScalar();
> }
>
>
> --
> All the best,
> Coskun SUNALI
> MVP ASP/ASP.NET
> http://sunali.com
>
>
> "James R. Davis" wrote in message
> news:O2HG8N%23RIHA.4196@TK2MSFTNGP04.phx.gbl...
>> Scott,
>>
>> You are amazingly generous with your help. Again, thank you!
>>
>> "Scott Roberts" wrote in
>> message news:Ofk8iK%23RIHA.748@TK2MSFTNGP04.phx.gbl...
>>> Note also that the "old" way you were also executing 2 statements - 1 to
>>> read then 1 to update. Now we're still executing 2 statements, but we're
>>> doing the update first then the read. This is "safer" because the DB
>> handles
>>> concurrency issues for us.
>>>
>>> > The "using" statement automatically disposes of the connection (which
>> also
>>> > closes it). You can explicitly call "conn.Close();" if it makes you
>>> > feel
>>> > better. :)
>>> >
>>> > You'll need another command and a DataReader to get the new value.
>>> >
>>> > // Goes right after "ExecuteNonQuery".
>>> > SqlCommand scmd = new SqlCommand("select UserCounter from MyTable",
>> conn);
>>> > SqlDataReader dr = scmd.ExecuteReader();
>>> > if (dr.Read())
>>> > Label2.Text = dr.GetString(0);
>>> >
>>> >
>>> > "James R. Davis" wrote in message
>>> > news:uymoU89RIHA.4740@TK2MSFTNGP02.phx.gbl...
>>> >> Amazingly simple when you step away from the details. That
>> accomplished
>>> >> 90%
>>> >> of what I need. Thank you!
>>> >>
>>> >> I assume you still must close or dispose of the connection. All that
>>> >> I
>>> >> need
>>> >> now is to know how to gain access to the new value to set the text
>> value
>>> >> of
>>> >> a label. Can I get that value without having to re-read the field?
>>> >>
>>> >>>
>>> >>> SqlConnection conn = new SqlConnection("YourConnectionString");
>>> >>> SqlCommand cmd = new SqlCommand("update MyTable set UserCounter =
>>> >>> UserCounter + 1", conn);
>>> >>> using (conn)
>>> >>> {
>>> >>> conn.Open();
>>> >>> cmd.ExecuteNonQuery();
>>> >>> }
>>> >>>
>>> >>
>>> >>
>>> >
>>>
>>
>>
>

Re: What used to be simple is now simply confusing

am 27.12.2007 02:21:38 von LVP

What I mean is between your update and your read

Update ctr = ctr + 1 in table
<<>>
you read it.


"LVP" wrote in message
news:uEH19XCSIHA.5404@TK2MSFTNGP03.phx.gbl...
>
> I am just wondering:
> How can you guarantee that no one else updates the UserCounter in the
> table before you read the UserCounter
>
> LVP
>
>
>
> "Coskun SUNALI [MVP]" wrote in message
> news:%23Mo7pT%23RIHA.4684@TK2MSFTNGP06.phx.gbl...
>> Hi,
>>
>> To make some additions...
>>
>> If you still insist on using only one command, you can execute both
>> queries at once and you can get the returning value using "ExecuteScalar"
>> method of the SqlCommand class. Just simple changes to what Scott wrote.
>> I am just now sure about if you need "+1" in second SQL statement, I
>> haven't executed the code but you still may need to save or remove it.
>>
>> int currentCounter = 0;
>> SqlConnection conn = new SqlConnection("YourConnectionString");
>> SqlCommand cmd = new SqlCommand("update MyTable set UserCounter =
>> UserCounter + 1; Select UserCounter + 1 from MyTable;", conn);
>> using (conn)
>> {
>> conn.Open();
>> currentCounter = cmd.ExecuteScalar();
>> }
>>
>>
>> --
>> All the best,
>> Coskun SUNALI
>> MVP ASP/ASP.NET
>> http://sunali.com
>>
>>
>> "James R. Davis" wrote in message
>> news:O2HG8N%23RIHA.4196@TK2MSFTNGP04.phx.gbl...
>>> Scott,
>>>
>>> You are amazingly generous with your help. Again, thank you!
>>>
>>> "Scott Roberts" wrote in
>>> message news:Ofk8iK%23RIHA.748@TK2MSFTNGP04.phx.gbl...
>>>> Note also that the "old" way you were also executing 2 statements - 1
>>>> to
>>>> read then 1 to update. Now we're still executing 2 statements, but
>>>> we're
>>>> doing the update first then the read. This is "safer" because the DB
>>> handles
>>>> concurrency issues for us.
>>>>
>>>> > The "using" statement automatically disposes of the connection (which
>>> also
>>>> > closes it). You can explicitly call "conn.Close();" if it makes you
>>>> > feel
>>>> > better. :)
>>>> >
>>>> > You'll need another command and a DataReader to get the new value.
>>>> >
>>>> > // Goes right after "ExecuteNonQuery".
>>>> > SqlCommand scmd = new SqlCommand("select UserCounter from MyTable",
>>> conn);
>>>> > SqlDataReader dr = scmd.ExecuteReader();
>>>> > if (dr.Read())
>>>> > Label2.Text = dr.GetString(0);
>>>> >
>>>> >
>>>> > "James R. Davis" wrote in message
>>>> > news:uymoU89RIHA.4740@TK2MSFTNGP02.phx.gbl...
>>>> >> Amazingly simple when you step away from the details. That
>>> accomplished
>>>> >> 90%
>>>> >> of what I need. Thank you!
>>>> >>
>>>> >> I assume you still must close or dispose of the connection. All
>>>> >> that I
>>>> >> need
>>>> >> now is to know how to gain access to the new value to set the text
>>> value
>>>> >> of
>>>> >> a label. Can I get that value without having to re-read the field?
>>>> >>
>>>> >>>
>>>> >>> SqlConnection conn = new SqlConnection("YourConnectionString");
>>>> >>> SqlCommand cmd = new SqlCommand("update MyTable set UserCounter =
>>>> >>> UserCounter + 1", conn);
>>>> >>> using (conn)
>>>> >>> {
>>>> >>> conn.Open();
>>>> >>> cmd.ExecuteNonQuery();
>>>> >>> }
>>>> >>>
>>>> >>
>>>> >>
>>>> >
>>>>
>>>
>>>
>>
>
>

Re: What used to be simple is now simply confusing

am 27.12.2007 02:45:35 von Coskun

Hi,

I am sorry but I don't understand the problem that I will have if someone
else updates the table before my read.

Current Value = 2
I visited web site
{
Counter incremented by 1;
Someone else visited web site
{
Counter incremented by 1;
}
}
I got the response: 4;
Someone else also got the response 4;

So actually the result is even better because I would always like to be sure
that I have the most correct value which is 4 here in this case, instead of
3 because someone else also visited the page at the given time.

And just because I want to remind you; what is being discused within this
topic is not the best practice to have an user counter but having an user
counter as much as simple.

--
All the best,
Coskun SUNALI
MVP ASP/ASP.NET
http://sunali.com



"LVP" wrote in message
news:emtWVbCSIHA.5524@TK2MSFTNGP05.phx.gbl...
>
> What I mean is between your update and your read
>
> Update ctr = ctr + 1 in table
> <<>>
> you read it.
>
>
> "LVP" wrote in message
> news:uEH19XCSIHA.5404@TK2MSFTNGP03.phx.gbl...
>>
>> I am just wondering:
>> How can you guarantee that no one else updates the UserCounter in the
>> table before you read the UserCounter
>>
>> LVP
>>
>>
>>
>> "Coskun SUNALI [MVP]" wrote in message
>> news:%23Mo7pT%23RIHA.4684@TK2MSFTNGP06.phx.gbl...
>>> Hi,
>>>
>>> To make some additions...
>>>
>>> If you still insist on using only one command, you can execute both
>>> queries at once and you can get the returning value using
>>> "ExecuteScalar" method of the SqlCommand class. Just simple changes to
>>> what Scott wrote. I am just now sure about if you need "+1" in second
>>> SQL statement, I haven't executed the code but you still may need to
>>> save or remove it.
>>>
>>> int currentCounter = 0;
>>> SqlConnection conn = new SqlConnection("YourConnectionString");
>>> SqlCommand cmd = new SqlCommand("update MyTable set UserCounter =
>>> UserCounter + 1; Select UserCounter + 1 from MyTable;", conn);
>>> using (conn)
>>> {
>>> conn.Open();
>>> currentCounter = cmd.ExecuteScalar();
>>> }
>>>
>>>
>>> --
>>> All the best,
>>> Coskun SUNALI
>>> MVP ASP/ASP.NET
>>> http://sunali.com
>>>
>>>
>>> "James R. Davis" wrote in message
>>> news:O2HG8N%23RIHA.4196@TK2MSFTNGP04.phx.gbl...
>>>> Scott,
>>>>
>>>> You are amazingly generous with your help. Again, thank you!
>>>>
>>>> "Scott Roberts" wrote in
>>>> message news:Ofk8iK%23RIHA.748@TK2MSFTNGP04.phx.gbl...
>>>>> Note also that the "old" way you were also executing 2 statements - 1
>>>>> to
>>>>> read then 1 to update. Now we're still executing 2 statements, but
>>>>> we're
>>>>> doing the update first then the read. This is "safer" because the DB
>>>> handles
>>>>> concurrency issues for us.
>>>>>
>>>>> > The "using" statement automatically disposes of the connection
>>>>> > (which
>>>> also
>>>>> > closes it). You can explicitly call "conn.Close();" if it makes you
>>>>> > feel
>>>>> > better. :)
>>>>> >
>>>>> > You'll need another command and a DataReader to get the new value.
>>>>> >
>>>>> > // Goes right after "ExecuteNonQuery".
>>>>> > SqlCommand scmd = new SqlCommand("select UserCounter from MyTable",
>>>> conn);
>>>>> > SqlDataReader dr = scmd.ExecuteReader();
>>>>> > if (dr.Read())
>>>>> > Label2.Text = dr.GetString(0);
>>>>> >
>>>>> >
>>>>> > "James R. Davis" wrote in message
>>>>> > news:uymoU89RIHA.4740@TK2MSFTNGP02.phx.gbl...
>>>>> >> Amazingly simple when you step away from the details. That
>>>> accomplished
>>>>> >> 90%
>>>>> >> of what I need. Thank you!
>>>>> >>
>>>>> >> I assume you still must close or dispose of the connection. All
>>>>> >> that I
>>>>> >> need
>>>>> >> now is to know how to gain access to the new value to set the text
>>>> value
>>>>> >> of
>>>>> >> a label. Can I get that value without having to re-read the field?
>>>>> >>
>>>>> >>>
>>>>> >>> SqlConnection conn = new SqlConnection("YourConnectionString");
>>>>> >>> SqlCommand cmd = new SqlCommand("update MyTable set UserCounter =
>>>>> >>> UserCounter + 1", conn);
>>>>> >>> using (conn)
>>>>> >>> {
>>>>> >>> conn.Open();
>>>>> >>> cmd.ExecuteNonQuery();
>>>>> >>> }
>>>>> >>>
>>>>> >>
>>>>> >>
>>>>> >
>>>>>
>>>>
>>>>
>>>
>>
>>
>
>

Re: What used to be simple is now simply confusing

am 27.12.2007 02:49:29 von LVP

Okay,

I miss-understood the requirements.

LVP:


"Coskun SUNALI [MVP]" wrote in message
news:eqMhvoCSIHA.3676@TK2MSFTNGP06.phx.gbl...
> Hi,
>
> I am sorry but I don't understand the problem that I will have if someone
> else updates the table before my read.
>
> Current Value = 2
> I visited web site
> {
> Counter incremented by 1;
> Someone else visited web site
> {
> Counter incremented by 1;
> }
> }
> I got the response: 4;
> Someone else also got the response 4;
>
> So actually the result is even better because I would always like to be
> sure that I have the most correct value which is 4 here in this case,
> instead of 3 because someone else also visited the page at the given time.
>
> And just because I want to remind you; what is being discused within this
> topic is not the best practice to have an user counter but having an user
> counter as much as simple.
>
> --
> All the best,
> Coskun SUNALI
> MVP ASP/ASP.NET
> http://sunali.com
>
>
>
> "LVP" wrote in message
> news:emtWVbCSIHA.5524@TK2MSFTNGP05.phx.gbl...
>>
>> What I mean is between your update and your read
>>
>> Update ctr = ctr + 1 in table
>> <<>>
>> you read it.
>>
>>
>> "LVP" wrote in message
>> news:uEH19XCSIHA.5404@TK2MSFTNGP03.phx.gbl...
>>>
>>> I am just wondering:
>>> How can you guarantee that no one else updates the UserCounter in the
>>> table before you read the UserCounter
>>>
>>> LVP
>>>
>>>
>>>
>>> "Coskun SUNALI [MVP]" wrote in message
>>> news:%23Mo7pT%23RIHA.4684@TK2MSFTNGP06.phx.gbl...
>>>> Hi,
>>>>
>>>> To make some additions...
>>>>
>>>> If you still insist on using only one command, you can execute both
>>>> queries at once and you can get the returning value using
>>>> "ExecuteScalar" method of the SqlCommand class. Just simple changes to
>>>> what Scott wrote. I am just now sure about if you need "+1" in second
>>>> SQL statement, I haven't executed the code but you still may need to
>>>> save or remove it.
>>>>
>>>> int currentCounter = 0;
>>>> SqlConnection conn = new SqlConnection("YourConnectionString");
>>>> SqlCommand cmd = new SqlCommand("update MyTable set UserCounter =
>>>> UserCounter + 1; Select UserCounter + 1 from MyTable;", conn);
>>>> using (conn)
>>>> {
>>>> conn.Open();
>>>> currentCounter = cmd.ExecuteScalar();
>>>> }
>>>>
>>>>
>>>> --
>>>> All the best,
>>>> Coskun SUNALI
>>>> MVP ASP/ASP.NET
>>>> http://sunali.com
>>>>
>>>>
>>>> "James R. Davis" wrote in message
>>>> news:O2HG8N%23RIHA.4196@TK2MSFTNGP04.phx.gbl...
>>>>> Scott,
>>>>>
>>>>> You are amazingly generous with your help. Again, thank you!
>>>>>
>>>>> "Scott Roberts" wrote in
>>>>> message news:Ofk8iK%23RIHA.748@TK2MSFTNGP04.phx.gbl...
>>>>>> Note also that the "old" way you were also executing 2 statements - 1
>>>>>> to
>>>>>> read then 1 to update. Now we're still executing 2 statements, but
>>>>>> we're
>>>>>> doing the update first then the read. This is "safer" because the DB
>>>>> handles
>>>>>> concurrency issues for us.
>>>>>>
>>>>>> > The "using" statement automatically disposes of the connection
>>>>>> > (which
>>>>> also
>>>>>> > closes it). You can explicitly call "conn.Close();" if it makes you
>>>>>> > feel
>>>>>> > better. :)
>>>>>> >
>>>>>> > You'll need another command and a DataReader to get the new value.
>>>>>> >
>>>>>> > // Goes right after "ExecuteNonQuery".
>>>>>> > SqlCommand scmd = new SqlCommand("select UserCounter from MyTable",
>>>>> conn);
>>>>>> > SqlDataReader dr = scmd.ExecuteReader();
>>>>>> > if (dr.Read())
>>>>>> > Label2.Text = dr.GetString(0);
>>>>>> >
>>>>>> >
>>>>>> > "James R. Davis" wrote in message
>>>>>> > news:uymoU89RIHA.4740@TK2MSFTNGP02.phx.gbl...
>>>>>> >> Amazingly simple when you step away from the details. That
>>>>> accomplished
>>>>>> >> 90%
>>>>>> >> of what I need. Thank you!
>>>>>> >>
>>>>>> >> I assume you still must close or dispose of the connection. All
>>>>>> >> that I
>>>>>> >> need
>>>>>> >> now is to know how to gain access to the new value to set the text
>>>>> value
>>>>>> >> of
>>>>>> >> a label. Can I get that value without having to re-read the field?
>>>>>> >>
>>>>>> >>>
>>>>>> >>> SqlConnection conn = new SqlConnection("YourConnectionString");
>>>>>> >>> SqlCommand cmd = new SqlCommand("update MyTable set UserCounter =
>>>>>> >>> UserCounter + 1", conn);
>>>>>> >>> using (conn)
>>>>>> >>> {
>>>>>> >>> conn.Open();
>>>>>> >>> cmd.ExecuteNonQuery();
>>>>>> >>> }
>>>>>> >>>
>>>>>> >>
>>>>>> >>
>>>>>> >
>>>>>>
>>>>>
>>>>>
>>>>
>>>
>>>
>>
>>
>