.NETGURU
ExecuteScalar
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngdata' list.


Brad Kingsley
Where is a good - complete - code sample for use of ExecuteScalar? I've
found a few articles online but none that show complete (cut & paste
workable) code. I know Charles had a code sample once but I can't find it
anymore.

Thanks,

~Brad Kingsley
Microsoft MVP - ASP
Windows 2000 MCSE

http://www.orcsweb.com/
Powerful Web Hosting Solutions
#1 in Service and Support

---
[This E-mail scanned for viruses by Declude Virus]

Reply to this message...
 
    
gordon
VHJ5IHRoaXMgbGluay4gaHR0cDovL2RuLnl5eXoubmV0L0RuWXpBcnRpY2xlU2hvdy5hc3B4P1dB
PTMwDQogDQpUaGUgY29kZSAoQyMpIHJldHVybnMgYW4gb2JqZWN0IHdoaWNoIGlzIHRoZW4gY29u
dmVydGVkIHRvIHRoZSBkZXNpcmVkIHR5cGUuIFdlIGFsc28gaGF2ZSBhbm90aGVyIGFydGljbGUg
KHNhbWUgbGlua3MpIHdoaWNoIHVzZXMgZXhlY3V0ZXNjYWxhciBmb3IgcmVjb3JkIGNvdW50aW5n
LiBMZXQgbWUga25vdyBpZiB5b3UgaGF2ZSBhbnkgcXVlc3Rpb25zLg0KIA0KR2Z3DQpHZndAeXl5
ei5uZXQNCmh0dHA6Ly95eXl6Lm5ldA0KIA0KDQoJLS0tLS1PcmlnaW5hbCBNZXNzYWdlLS0tLS0g
DQoJRnJvbTogQnJhZCBLaW5nc2xleSBbbWFpbHRvOmJrTGlzdHNAb3Jjc3dlYi5jb21dIA0KCVNl
bnQ6IEZyaSA2LzIxLzIwMDIgOTowMiBBTSANCglUbzogYXNwbmdkYXRhIA0KCUNjOiANCglTdWJq
ZWN0OiBbYXNwbmdkYXRhXSBFeGVjdXRlU2NhbGFyDQoJDQoJDQoNCglXaGVyZSBpcyBhIGdvb2Qg
LSBjb21wbGV0ZSAtIGNvZGUgc2FtcGxlIGZvciB1c2Ugb2YgRXhlY3V0ZVNjYWxhcj8gSSd2ZQ0K
CWZvdW5kIGEgZmV3IGFydGljbGVzIG9ubGluZSBidXQgbm9uZSB0aGF0IHNob3cgY29tcGxldGUg
KGN1dCAmIHBhc3RlDQoJd29ya2FibGUpIGNvZGUuIEkga25vdyBDaGFybGVzIGhhZCBhIGNvZGUg
c2FtcGxlIG9uY2UgYnV0IEkgY2FuJ3QgZmluZCBpdA0KCWFueW1vcmUuDQoJDQoJVGhhbmtzLA0K
CQ0KCX5CcmFkIEtpbmdzbGV5DQoJTWljcm9zb2Z0IE1WUCAtIEFTUA0KCVdpbmRvd3MgMjAwMCBN
Q1NFDQoJDQoJaHR0cDovL3d3dy5vcmNzd2ViLmNvbS8NCglQb3dlcmZ1bCBXZWIgSG9zdGluZyBT
b2x1dGlvbnMNCgkjMSBpbiBTZXJ2aWNlIGFuZCBTdXBwb3J0DQoJDQoJLS0tDQoJW1RoaXMgRS1t
YWlsIHNjYW5uZWQgZm9yIHZpcnVzZXMgYnkgRGVjbHVkZSBWaXJ1c10NCgkNCgkNCgl8IFthc3Bu
Z2RhdGFdIG1lbWJlciBnZndAeXl5ei5uZXQgPSBZT1VSIElEDQoJfCBodHRwOi8vd3d3LmFzcGxp
c3RzLmNvbS9hc3BsaXN0cy9hc3BuZ2RhdGEuYXNwID0gSk9JTi9RVUlUDQoJfCBodHRwOi8vd3d3
LmFzcGxpc3RzLmNvbS9zZWFyY2ggPSBTRUFSQ0ggQXJjaGl2ZXMNCgkNCgkNCg0K
Reply to this message...
 
    
Scott Watermasysk
Brad,

Here you go! Just remember to cast the returned value (stirng, int, bool):
public object ExecuteScalar(string sql, string ConnectString)
{
    object obj;
    SqlCommand cmd = new SqlCommand(sql, new SqlConnection(ConnectString));
    cmd.CommandType = CommandType.StoredProcedure;
    try
    {
        cmd.Connection.Open();
        obj = cmd.ExecuteScalar();
    }
    catch (SqlException se)
    {
        throw;
    }
    catch (Exception e)
    {
        throw;
    }
    finally
    {
        if(cmd.Connection.State == ConnectionState.Open)
        {
            cmd.Connection.Close();
        }
    }
    return obj;
}

HTH,
Scott
---------- Original Message ----------------------------------
From: "Brad Kingsley" <Click here to reveal e-mail address>
Reply-To: "aspngdata" <Click here to reveal e-mail address>
Date: Fri, 21 Jun 2002 10:02:23 -0400

[Original message clipped]

Reply to this message...
 
    
Ahmed, Salman
http://www.dotnetnotes.com/index.aspx?&catID=4&pageID=0#101

-----Original Message-----
From: Brad Kingsley [mailto:Click here to reveal e-mail address]
Sent: Friday, June 21, 2002 10:02 AM
To: aspngdata
Subject: [aspngdata] ExecuteScalar

Where is a good - complete - code sample for use of ExecuteScalar? I've
found a few articles online but none that show complete (cut & paste
workable) code. I know Charles had a code sample once but I can't find it
anymore.

Thanks,

~Brad Kingsley
Microsoft MVP - ASP
Windows 2000 MCSE

http://www.orcsweb.com/
Powerful Web Hosting Solutions
#1 in Service and Support

---
[This E-mail scanned for viruses by Declude Virus]

| [aspngdata] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngdata.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

Reply to this message...
 
    
Brad Kingsley
Thanks all that answered this. It took me a while to figure out what object
I was trying to return until I realized that the "object" is really nothing
more than the value, so the object equals the data type that is returned.
For some reason I had a hard time grasping that concept, but I got it all
working quite nicely (and fast) now.

Thanks again.

~Brad Kingsley
Microsoft MVP - ASP
Windows 2000 MCSE

http://www.orcsweb.com/
Powerful Web Hosting Solutions
#1 in Service and Support

---
[This E-mail scanned for viruses by Declude Virus]

Reply to this message...
 
 
System.Data.CommandType
System.Data.ConnectionState
System.Data.SqlClient.SqlCommand
System.Data.SqlClient.SqlConnection
System.Data.SqlClient.SqlException




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification