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 / ASP.NET / General / December 2007

Tip: Looking for answers? Try searching our database.

What used to be simple is now simply confusing

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
James R. Davis - 26 Dec 2007 16:15 GMT
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.
Scott Roberts - 26 Dec 2007 16:27 GMT
> Yes, a newbie here.
>
[quoted text clipped - 23 lines]
>
> Thank you.

SqlConnection conn = new SqlConnection("YourConnectionString");
SqlCommand cmd = new SqlCommand("update MyTable set UserCounter =
UserCounter + 1", conn);
using (conn)
{
   conn.Open();
   cmd.ExecuteNonQuery();
}
James R. Davis - 26 Dec 2007 16:45 GMT
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 =
[quoted text clipped - 4 lines]
>     cmd.ExecuteNonQuery();
> }
Scott Roberts - 26 Dec 2007 17:08 GMT
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);

> Amazingly simple when you step away from the details.  That accomplished
> 90%
[quoted text clipped - 14 lines]
>>     cmd.ExecuteNonQuery();
>> }
Scott Roberts - 26 Dec 2007 17:13 GMT
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
[quoted text clipped - 26 lines]
>>>     cmd.ExecuteNonQuery();
>>> }
James R. Davis - 26 Dec 2007 17:16 GMT
Scott,

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

> 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
[quoted text clipped - 31 lines]
> >>>     cmd.ExecuteNonQuery();
> >>> }
Coskun SUNALI [MVP] - 26 Dec 2007 17:29 GMT
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();
}

Signature

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

> Scott,
>
[quoted text clipped - 42 lines]
>> >>>     cmd.ExecuteNonQuery();
>> >>> }
LVP - 27 Dec 2007 01:15 GMT
I am just wondering:
How can you guarantee that no one else updates the UserCounter in the table
before you read the UserCounter

LVP

> Hi,
>
[quoted text clipped - 62 lines]
>>> >>>     cmd.ExecuteNonQuery();
>>> >>> }
LVP - 27 Dec 2007 01:21 GMT
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.

> I am just wondering:
> How can you guarantee that no one else updates the UserCounter in the
[quoted text clipped - 70 lines]
>>>> >>>     cmd.ExecuteNonQuery();
>>>> >>> }
Coskun SUNALI [MVP] - 27 Dec 2007 01:45 GMT
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.

Signature

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

> What I mean is between your update and your read
>
[quoted text clipped - 78 lines]
>>>>> >>>     cmd.ExecuteNonQuery();
>>>>> >>> }
LVP - 27 Dec 2007 01:49 GMT
Okay,

I miss-understood the requirements.

LVP:

> Hi,
>
[quoted text clipped - 103 lines]
>>>>>> >>>     cmd.ExecuteNonQuery();
>>>>>> >>> }
Scott M. - 26 Dec 2007 16:41 GMT
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).

> Yes, a newbie here.
>
[quoted text clipped - 23 lines]
>
> Thank you.

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.