473,473 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ExecuteScalar problem for null values

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 updated values from the table
Sql query is "select updated from details where no='1' and name ='kk'
When execute the above query it is throwing error as "object reference
not set to an instance of an object "

Code as below

string date;
string Query="select updated from details where no='"+ no+"' and
name='"+ name+"'";
SqlCommand Command=new SqlCommand(Query,connection);
date=Command.ExecuteScalar().ToString();
Is it possible to cast a date value to string in execute scalar
method?.
Kindly help me to solve the issue
Manikandan

Apr 24 '07 #1
1 5698
ExecuteScalar returns type Object. So if you get back null, obviously you
cannot call the ToString() method on thin air. Check for null first, then
decide.

Also, you should be using parameterized SQL queries instead of inline
concatenation of a SQL string.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Manikandan" wrote:
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 updated values from the table
Sql query is "select updated from details where no='1' and name ='kk'
When execute the above query it is throwing error as "object reference
not set to an instance of an object "

Code as below

string date;
string Query="select updated from details where no='"+ no+"' and
name='"+ name+"'";
SqlCommand Command=new SqlCommand(Query,connection);
date=Command.ExecuteScalar().ToString();
Is it possible to cast a date value to string in execute scalar
method?.
Kindly help me to solve the issue
Manikandan

Apr 24 '07 #2

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

Similar topics

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...
1
by: Chad Dittmer via .NET 247 | last post by:
I'm having problems inserting into Sql 2000 using SqlHelper.ExecuteScalar. When it inserts, it only takes the first character from the string?? Any help would be appreciated. Here is the code and...
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.". ...
3
by: charliewest | last post by:
Hi - I need to detect when the ExecuteScalar() method of the cmd object returns NULL. I have tried the below code, however, it always returns false (this is to say, that ExecuteScalar never...
9
by: eagle | last post by:
How can I return the new id that is created when an insert command is used? for example: qry = "insert into tblClients (lname, fname) values ('smith', 'joe')" Dim xyz as string =...
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()); }
1
by: js | last post by:
I am using the following C# code and T-SQL to get result object from a SQL Server database. When my application runs, the ExecuteScalar returns "10/24/2006 2:00:00 PM" if inserting a duplicated...
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...
2
by: MDB | last post by:
Hello All, what is the correct / proper way to check for null values in the ExecuteScalar. System.DBNull.Value, Null or both?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.