By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,727 Members | 1,263 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,727 IT Pros & Developers. It's quick & easy.

What used to be simple is now simply confusing

P: n/a
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.
Dec 26 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a


"James R. Davis" <Ji*@msgroup.orgwrote in message
news:uC**************@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();
}

Dec 26 '07 #2

P: n/a
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" <Ji*@msgroup.orgwrote in message
news:uC**************@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.


Dec 26 '07 #3

P: n/a
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();
}

Dec 26 '07 #4

P: n/a
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" <Ji*@msgroup.orgwrote in message
news:uy**************@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();
}

Dec 26 '07 #5

P: n/a
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" <Ji*@msgroup.orgwrote in message
news:uy**************@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();
}

Dec 26 '07 #6

P: n/a
Scott,

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

"Scott Roberts" <sr******@no.spam.here-webworks-software.comwrote in
message news:Of***************@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" <Ji*@msgroup.orgwrote in message
news:uy**************@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();
}

Dec 26 '07 #7

P: n/a
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" <Ji*@msgroup.orgwrote in message
news:O2****************@TK2MSFTNGP04.phx.gbl...
Scott,

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

"Scott Roberts" <sr******@no.spam.here-webworks-software.comwrote in
message news:Of***************@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" <Ji*@msgroup.orgwrote in message
news:uy**************@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();
}


Dec 26 '07 #8

P: n/a
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]" <Co****@SUNALI.comwrote in message
news:%2******************@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" <Ji*@msgroup.orgwrote in message
news:O2****************@TK2MSFTNGP04.phx.gbl...
>Scott,

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

"Scott Roberts" <sr******@no.spam.here-webworks-software.comwrote in
message news:Of***************@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" <Ji*@msgroup.orgwrote in message
news:uy**************@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();
}




Dec 27 '07 #9

P: n/a
LVP

What I mean is between your update and your read

Update ctr = ctr + 1 in table
<<<some one else updates it again>>>
you read it.
"LVP" <lv**********@hotmail.comwrote in message
news:uE**************@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]" <Co****@SUNALI.comwrote in message
news:%2******************@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" <Ji*@msgroup.orgwrote in message
news:O2****************@TK2MSFTNGP04.phx.gbl...
>>Scott,

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

"Scott Roberts" <sr******@no.spam.here-webworks-software.comwrote in
message news:Of***************@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" <Ji*@msgroup.orgwrote in message
news:uy**************@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();
}
>



Dec 27 '07 #10

P: n/a
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" <lv**********@hotmail.comwrote in message
news:em**************@TK2MSFTNGP05.phx.gbl...
>
What I mean is between your update and your read

Update ctr = ctr + 1 in table
<<<some one else updates it again>>>
you read it.
"LVP" <lv**********@hotmail.comwrote in message
news:uE**************@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]" <Co****@SUNALI.comwrote in message
news:%2******************@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" <Ji*@msgroup.orgwrote in message
news:O2****************@TK2MSFTNGP04.phx.gbl.. .
Scott,

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

"Scott Roberts" <sr******@no.spam.here-webworks-software.comwrote in
message news:Of***************@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" <Ji*@msgroup.orgwrote in message
news:uy**************@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();
>}
>>
>
>

>


Dec 27 '07 #11

P: n/a
LVP
Okay,

I miss-understood the requirements.

LVP:
"Coskun SUNALI [MVP]" <Co****@SUNALI.comwrote in message
news:eq**************@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" <lv**********@hotmail.comwrote in message
news:em**************@TK2MSFTNGP05.phx.gbl...
>>
What I mean is between your update and your read

Update ctr = ctr + 1 in table
<<<some one else updates it again>>>
you read it.
"LVP" <lv**********@hotmail.comwrote in message
news:uE**************@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]" <Co****@SUNALI.comwrote in message
news:%2******************@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" <Ji*@msgroup.orgwrote in message
news:O2****************@TK2MSFTNGP04.phx.gbl. ..
Scott,
>
You are amazingly generous with your help. Again, thank you!
>
"Scott Roberts" <sr******@no.spam.here-webworks-software.comwrote in
message news:Of***************@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" <Ji*@msgroup.orgwrote in message
news:uy**************@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();
>>}
>>>
>>
>>
>
>>
>
>


Dec 27 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.