472,364 Members | 2,153 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Executescalar pblm 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
2 3258
Manikandan,

The problem here is that calling ExecuteScalar is returning null (an
object reference of null, not the db value of null) because your query
returns no rows. Because of this, you are calling ToString on your null
reference, which gives you the NullReferenceException.

You need to do this:

// Get the result.
object date = Command.ExecuteScalar();

// If there is a value, continue.
if (date != null)
{
// Work with date here.
}

Also, don't create the query string the way you are, you are opening
yourself up to a SQL injection attack. Rather, use the Parameters
collection on the command to have a parameterized query.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Manikandan" <pl**********@gmail.comwrote in message
news:11*********************@o40g2000prh.googlegro ups.com...
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
Aside from Nicholas' recommendations, if field "No" is a number, then it
generally should be an int or bigint, not a varchar -- usually resulting
in better performance and less consumption of disk space. Not to
mention avoiding collation issues such as "10" coming before "2". At
the very least, if you must use varchar to store numeric data, left-pad
numbers in a varchar field with blanks or zeroes so they will order
properly.

--Bob

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 #3

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...
1
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?
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

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.