Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / DB Engine / SQL Server CE / March 2008

Tip: Looking for answers? Try searching our database.

String value in where clause

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dale Edmondson - 28 Feb 2008 04:34 GMT
SqlCeDataReader will not return any records when using a string value in the
Where clause.  Integers work fine.   Following is a example of the problem

// Database columns            
//------------------
// ID (int)
// SizeId (int)
// LookupCode nvarchar(16)            
// TestCode nchar(16)

// The following three records are in the database
//------------------------------------------------
// ID   SizeID  LookupCode     TestCode
//  1    100     123456              123456          
//  2    200     7890              7890            
//  3    300     333333333333 333333333333    

SqlCeConnection _conn = null;

string _dataSource = "Data Source = \\Program Files\\SQLtest\\TestDB.Sdf\\;";
_conn = new SqlCeConnection(_dataSource);
_conn.Open();
           
// Only the last Select statement works.  
// The first 3 does not return any records            
//
// string _sql = "Select * from UPC where LookupCode='7890'";
// string _sql = "Select * from UPC where LookupCode=N'7890'";
// string _sql = "Select * from UPC where TestCode=N'7890'";

string _sql = "Select * from UPC where SizeId=200";

SqlCeCommand _cmd = _conn.CreateCommand();
SqlCeDataReader _rdr;
_cmd.CommandText = _sql;
_cmd.CommandType = CommandType.Text;
_rdr = _cmd.ExecuteReader();
while (_rdr.Read())
{
   txt2.Text = _rdr["ID"].ToString() + " " + _rdr["SizeId"].ToString() + "
" + _rdr["TestCode"].ToString();
}
Ginny Caughey [MVP] - 28 Feb 2008 12:44 GMT
Dale,

I don't get this behavior in the Northwind database using SQL Server
Managment Studio. Do you get the same behavior with your data using SSMS?
Which version of SQLce are you using?

Signature

Ginny Caughey
Device Application Development MVP

www.wasteworks.com
Scalehouse and Billing Software for Waste Management

> SqlCeDataReader will not return any records when using a string value in
> the
[quoted text clipped - 40 lines]
> " + _rdr["TestCode"].ToString();
> }
Dale Edmondson - 28 Feb 2008 13:31 GMT
SSMS works fine for me too (all four queries from the example).  This only
occurs in code.  
SSMS is ver 9.00.1399.00
WM 6.0
VS 2005 8.0.50727.867
Vista Ultimate

> Dale,
>
[quoted text clipped - 46 lines]
> > " + _rdr["TestCode"].ToString();
> > }
Ginny Caughey [MVP] - 28 Feb 2008 14:20 GMT
Dale,

I can't reproduce this using the Northwind 3.0 database and WM6 emulator.
Can you?

Signature

Ginny Caughey
Device Application Development MVP

www.wasteworks.com
Scalehouse and Billing Software for Waste Management

> SSMS works fine for me too (all four queries from the example).  This only
> occurs in code.
[quoted text clipped - 56 lines]
>> > " + _rdr["TestCode"].ToString();
>> > }
Jin - 28 Feb 2008 16:39 GMT
On Feb 27, 11:34 pm, Dale Edmondson
<DaleEdmond...@discussions.microsoft.com> wrote:
> SqlCeDataReader will not return any records when using a string value in the
> Where clause.  Integers work fine.   Following is a example of the problem
[quoted text clipped - 39 lines]
>
> }

Just a thought.  Check to make sure the string column values don't
have trailing spaces.
Dale Edmondson - 01 Mar 2008 20:59 GMT
Here is an update: I am able to make the Northwind database work as well as
my own database.  Here's where the problem lies:

The field in question for my database is an ncharvar(16). IF the field
contains only numeric values, the record will not be selected. If the field
contains alpha characters the record WILL BE selected. I have reproduced
these results in both my database and the Northwind database.  I simply added
a new record to Northwind (customers) and used a numeric value ('0123') for
the Customer ID and the select failed.

Any suggestions?  

> On Feb 27, 11:34 pm, Dale Edmondson
> <DaleEdmond...@discussions.microsoft.com> wrote:
[quoted text clipped - 44 lines]
> Just a thought.  Check to make sure the string column values don't
> have trailing spaces.
Ginny Caughey [MVP] - 01 Mar 2008 22:36 GMT
Dale,

Does this only happen with a column that is nvarchar(16)? For example the
[customer id] column that is nvarchar(5) doesn't seem to have this behavior
for me.

Signature

Ginny Caughey
Device Application Development MVP

www.wasteworks.com
Scalehouse and Billing Software for Waste Management

> Here is an update: I am able to make the Northwind database work as well
> as
[quoted text clipped - 64 lines]
>> Just a thought.  Check to make sure the string column values don't
>> have trailing spaces.
Dale Edmondson - 01 Mar 2008 23:55 GMT
You added a numeric value for a Customer Id in Northwind?  (that starts with 0)

When I added a numeric customer Id ('0123') in Northwind, no records were
returned. Records that contain alpha characters work as expected.

> Dale,
>
[quoted text clipped - 70 lines]
> >> Just a thought.  Check to make sure the string column values don't
> >> have trailing spaces.
Ginny Caughey [MVP] - 02 Mar 2008 11:53 GMT
Dale,

Yes, here's the code, tested in the WM6 Pro emulator:

private void button1_Click(object sender, EventArgs e)
{
SqlCeConnection conn = new SqlCeConnection("Data source=northwind.sdf");
conn.Open();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "insert into customers ([customer id], [company name])
values ('0123', 'Something')";
cmd.ExecuteNonQuery();
cmd.CommandText = "select [customer id] from Customers where [customer id] =
'0123'";
SqlCeDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
MessageBox.Show(rdr.GetString(0));
}
rdr.Close();
conn.Close();

So that's why I wondered if the problem was related to the size of the
nvarchar column.

Signature

Ginny Caughey
Device Application Development MVP

www.wasteworks.com
Scalehouse and Billing Software for Waste Management

> You added a numeric value for a Customer Id in Northwind?  (that starts
> with 0)
[quoted text clipped - 81 lines]
>> >> Just a thought.  Check to make sure the string column values don't
>> >> have trailing spaces.
Dale Edmondson - 03 Mar 2008 00:03 GMT
I really appreciate all the help.

After trying your test, it worked as long as the Insert and the Select was
performed together.  The same code was run again after commenting out the
insert statment and the select failed.  

I am really not sure where to go from here.  Can you compare your
SqlServerCe.Dll's to these versions?

Device:    3.0.3600.0
DeskTop: 9.0.242.0

> Dale,
>
[quoted text clipped - 106 lines]
> >> >> Just a thought.  Check to make sure the string column values don't
> >> >> have trailing spaces.
Ginny Caughey [MVP] - 03 Mar 2008 00:41 GMT
Dale,

I'm pretty sure I"m suing SQLce 3.5.

Signature

Ginny Caughey
Device Application Development MVP

www.wasteworks.com
Scalehouse and Billing Software for Waste Management

>I really appreciate all the help.
>
[quoted text clipped - 126 lines]
>> >> >> Just a thought.  Check to make sure the string column values don't
>> >> >> have trailing spaces.
Ginny Caughey [MVP] - 03 Mar 2008 00:45 GMT
I just checked and it's the same version as you. I am also able to comment
out the insert and run the app over and over. Do you have a problem on the
desktop too? I thought you didn't.

Signature

Ginny Caughey
Device Application Development MVP

www.wasteworks.com
Scalehouse and Billing Software for Waste Management

>I really appreciate all the help.
>
[quoted text clipped - 126 lines]
>> >> >> Just a thought.  Check to make sure the string column values don't
>> >> >> have trailing spaces.
Dale Edmondson - 03 Mar 2008 01:30 GMT
No problems with the desktop.  

> I just checked and it's the same version as you. I am also able to comment
> out the insert and run the app over and over. Do you have a problem on the
[quoted text clipped - 130 lines]
> >> >> >> Just a thought.  Check to make sure the string column values don't
> >> >> >> have trailing spaces.
Dale Edmondson - 02 Mar 2008 02:22 GMT
More Info:
I tried the same test using a desktop app and everything works as expected
regardless of data type.  All code the same except for the library Reference
and the connection string.

Desktop ----  WORKS AS EXPECTED
System.Data.SqlServerCe.dll (9.0.242.0)

Device ---- DOESN'T WORK
System.Data.SqlServerCe.Dll (3.0.3600.0

> Dale,
>
[quoted text clipped - 70 lines]
> >> Just a thought.  Check to make sure the string column values don't
> >> have trailing spaces.
Jin - 02 Mar 2008 19:22 GMT
On Mar 1, 9:22 pm, Dale Edmondson
<DaleEdmond...@discussions.microsoft.com> wrote:
> More Info:
> I tried the same test using a desktop app and everything works as expected
[quoted text clipped - 88 lines]
> > >> Just a thought.  Check to make sure the string column values don't
> > >> have trailing spaces.

Sounds like data conversion is taking place somewhere (from string to
numeric) so that the leading "0" is lost in translation.  You may want
to check all places where data conversion may come into play, whether
it's the insert statement, select statement, even the replication
routines if any are involved.  These issues usually end up being
something trivial that we tend to overlook.  It could be a bug, but
something as obvious as this would have been reported by others before.
Dale Edmondson - 04 Mar 2008 13:25 GMT
Just on last note.  The reader returns the correct record when using the
following parameter query.  

   cmd.CommandText = "select [customer id] from Customers where [customer
id] = @NameID";
   cmd.Parameters.Add("@NameID", SqlDbType.NVarChar, 15);
   cmd.Parameters["@NameID"].Value = "0123";
   SqlCeDataReader rdr = cmd.ExecuteReader();
   while (rdr.Read())
   {
       MessageBox.Show(rdr.GetString(0));
   }

> On Mar 1, 9:22 pm, Dale Edmondson
> <DaleEdmond...@discussions.microsoft.com> wrote:
[quoted text clipped - 98 lines]
> something trivial that we tend to overlook.  It could be a bug, but
> something as obvious as this would have been reported by others before.
Ginny Caughey [MVP] - 04 Mar 2008 14:17 GMT
Dale,

Thanks for this update. Sometimes values are translated incorrectly, and by
specifying the parameter type you avoid that potential issue. I've also had
good luck with Paraneters.AddWithValue compared with not specifying the
value type. I don't know why AddWithValue seems to do a better job of
inferring the data type even in the desktop, but in any case specifying the
type is the surest way to get it right.

Signature

Ginny Caughey
Device Application Development MVP

www.wasteworks.com
Scalehouse and Billing Software for Waste Management

> Just on last note.  The reader returns the correct record when using the
> following parameter query.
[quoted text clipped - 124 lines]
>> something trivial that we tend to overlook.  It could be a bug, but
>> something as obvious as this would have been reported by others before.
Dale Edmondson - 04 Mar 2008 14:49 GMT
Interesting--While reading your response I noticed the parameter for the Name
Id specifies a length of 15 and it should be 5.  In any case it worked.  
Thanks again for your help.

> Dale,
>
[quoted text clipped - 133 lines]
> >> something trivial that we tend to overlook.  It could be a bug, but
> >> something as obvious as this would have been reported by others before.
 
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



©2009 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.