473,378 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

ExecuteScalar() not returning single field value

Why am I not able to retrieve the value of ID (which does exist) using
ExecuteScalar()? Do i simply have to use ExecuteReader() instead?

My problem snippet:

string mySQL = "SELECT ID FROM Table WHERE TableID = 1";
//TableID is a unique ID within Table, no dupiclates-risk

myCommand = new SqlCommand(mySQL, myConn);
myConn.Open();
int myID = (int)myCommand.ExecuteScalar();
myConn.Close();
return myID;

My result:
Object reference not set to an instance of an object.

Nov 22 '05 #1
3 4312
Is there a record with TableID=1 ?

Is the error being generated on the ExecuteScalar() invocation or a
different line?

You might try object obj = myCommand.ExecuteScalar();
and inspect the value of the object or convert it if it is not null.

-Andrew
<cm******@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Why am I not able to retrieve the value of ID (which does exist) using
ExecuteScalar()? Do i simply have to use ExecuteReader() instead?

My problem snippet:

string mySQL = "SELECT ID FROM Table WHERE TableID = 1";
//TableID is a unique ID within Table, no dupiclates-risk

myCommand = new SqlCommand(mySQL, myConn);
myConn.Open();
int myID = (int)myCommand.ExecuteScalar();
myConn.Close();
return myID;

My result:
Object reference not set to an instance of an object.

Nov 22 '05 #2
It's not executing the query that is doing it.

You should have the line number that this is failing on: so which line is
it?

<cm******@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Why am I not able to retrieve the value of ID (which does exist) using
ExecuteScalar()? Do i simply have to use ExecuteReader() instead?

My problem snippet:

string mySQL = "SELECT ID FROM Table WHERE TableID = 1";
//TableID is a unique ID within Table, no dupiclates-risk

myCommand = new SqlCommand(mySQL, myConn);
myConn.Open();
int myID = (int)myCommand.ExecuteScalar();
myConn.Close();
return myID;

My result:
Object reference not set to an instance of an object.

Nov 22 '05 #3
me = dumbA$$;

the variable I was passing in, unrelated to the simpler example posted
here, was the problem.

my apologies. :(.

Nov 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: sean | last post by:
HI, I would like to know if I can some set a variable to return a single value from a function using the set command. Is this possible? Thanks in advance for your answer Sean
3
by: cmo63126 | last post by:
Why am I not able to retrieve the value of ID (which does exist) using ExecuteScalar()? Do i simply have to use ExecuteReader() instead? My problem snippet: string mySQL = "SELECT ID FROM...
6
by: Max | last post by:
Anyone know why I'm always getting 0 returned? My stored procedure returns -1. Dim iErrorCode As Int32 iErrorCode = Convert.ToInt32(SqlHelper.ExecuteScalar(AppVars.strConn, _ "gpUpdateMember",...
7
by: Neven Klofutar | last post by:
Hi, I have a problem with SqlHelper.ExecuteScalar ... When I try to execute SqlHelper.ExecuteScalar I get this message: "System.InvalidCastException: Object must implement IConvertible.". ...
5
by: bienwell | last post by:
Hi all, I have a problem with using myCommand.ExecuteScalar(). My question is : If the Web setup is incorrect, does it make command ExecuteScalar() work improperly ?? In my program, I was...
8
by: Earl | last post by:
What's the best way to check for null on an ExecuteScalar? The following would fire the command twice: if (cmd.ExecuteScalar() != null) { intContactID = Convert.ToInt32(cmd.ExecuteScalar()); }
4
by: Morten Snedker | last post by:
On a web-form the user enters basic data (company name, phone, areacode etc). Many we have already registrered and some not. For those we have we wish to make the enter of data easier by looking...
2
by: Randy Smith | last post by:
Hi, I've got some weird behavior happening within one of the datamappers. It all has to do with inserting a new row, and returning the Id of the row being entered. Here is what the code...
2
by: Manikandan | last post by:
Hi, I have a table with following data Tablename:details No(varchar) Name(varchar) Updated(Datetime) 1 mm 10/10/2006 2 nn 02/12/2005 3 kk NULL I'm using executescalar to get the...
10
by: pinman | last post by:
hi i am trying to implemement forms authentication for my website but can't seem to get the stored procedure to output the correct value when checking a users credentials. the code is ALTER...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.