Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7020917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:22:53+00:00 2026-05-27T23:22:53+00:00

I am developing a windows mobile app. Right now I am just testing that

  • 0

I am developing a windows mobile app. Right now I am just testing that it can correctly query the local SQL Server CE database. It works fine until I put a WHERE statement in.

Here is my code:

private void buttonStart_Click(object sender, EventArgs e)
{
   System.Data.SqlServerCe.SqlCeConnection conn = new System.Data.SqlServerCe.SqlCeConnection(
   ("Data Source=" + (System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), "ElectricReading.sdf") + ";Max Database Size=2047")));

   try
   {
       // Connect to the local database
       conn.Open();
       System.Data.SqlServerCe.SqlCeCommand cmd = conn.CreateCommand();

       SqlCeParameter param = new SqlCeParameter();
       param.ParameterName = "@Barcode";
       param.Value = "%" + textBarcode.Text.Trim() + "%";

       // Insert a row
       cmd.CommandText = "SELECT * FROM Main2 WHERE Reading LIKE @Barcode";
       cmd.Parameters.Add(param);

       cmd.ExecuteNonQuery();

       DataTable data = new DataTable();

       using (SqlCeDataReader reader = cmd.ExecuteReader())
       {
           if (reader.Read())
           {
              data.Load(reader);
           }
       }

       if (data != null)
       {
           this.dataGrid1.DataSource = data;
       }
   }
   finally
   {
       conn.Close();
   }

The database contains this data:

db

Okay so you can see I changed the WHERE statement to use the Reading column just for testing purposes. When I enter “111” into the textbox and run –> it returns only the row where reading ="1111" and not the row that contains “111”.

If I enter “1111” it does not return any data.

If I enter “1” it will return both the “1111” row and the “111” row which is the correct behavior.

However if I enter “11” it once again only returns the “1111” row.

Any other data entry of 2’s or 9’s attempting to return those rows does not work.

I’m not sure what is going on? This does not make any sense. It is not behaving like I would expect in any way shape or form. I know this must be a little confusing to read. I hope it makes enough sense to get some answers. Please help!

NOTE: I added the “%” before and after the text in an attempt to get better results. This is not desired.

EDIT <<<———————–I did have Reading = @Barcode, I just accidently typed Location for this question, that is not the problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:22:54+00:00Added an answer on May 27, 2026 at 11:22 pm

    Firstly, some things to note:

    1) As other commentators have noted, use the Reading column, not the Location column. I know you have mentioned you are testing, but swapping around column names and then changing code isn’t the easiest way to troubleshoot these things. Try to only change one thing at a time.

    2) If Reading is numeric, you are going to have to convert the column value first.

    So your query becomes:

    "SELECT * FROM Main2 WHERE CONVERT(varchar, Reading) LIKE @Barcode";
    

    Also see How to use parameter with LIKE in Sql Server Compact Edition for more help with working with parameters in SqlServerCE.

    3) Set a parameter type on your SqlCEParameter. I’ve linked to the appropriate page in the code example below.

    4) You are using ExecuteNonQuery for no reason. Just get rid of it in this context. It’s for when you want to make a change to the database (like an insert, update, delete) or execute something (like a stored proc that can also insert, update, delete etc) that returns no rows. You’ve probably cut and paste this code from another place in your app 🙂

    5) Use using on disposable objects (see example below). This will make managing your connection lifecycle much simpler. It’s also more readable (IMO) and will take care of issues when exceptions occur.

    6) Use the using statement to import the BCL (Base Class Libraries) into your current namespace:

    Add the following using statements to the top of your class (.cs). This will make using all of the .Net classes a lot simpler (and is much easier to read and less wear on your keyboard 😉

    using System.Data.SqlServerCe;
    using System.IO;
    using System.Reflection;
    

    A more complete example would look like the following

    private void buttonStart_Click(object sender, EventArgs e) 
    { 
       using(SqlCeConnection conn = new SqlCeConnection( 
       ("Data Source=" + (Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase), "ElectricReading.sdf") + ";Max Database Size=2047"))))
       {
    
           // Connect to the local database 
           conn.Open(); 
           using(SqlCeCommand cmd = conn.CreateCommand())
           {
           SqlCeParameter param = new SqlCeParameter(); 
           param.ParameterName = "@Barcode"; 
           param.DBType = DBType.String; //Intellisense is your friend here but See http://msdn.microsoft.com/en-US/library/system.data.sqlserverce.sqlceparameter.dbtype(v=VS.80).aspx for supported types
           param.Value = "%" + textBarcode.Text.Trim() + "%"; 
    
           // SELECT rows
           cmd.CommandText = "SELECT * FROM Main2 WHERE CONVERT(varchar, Reading) LIKE @Barcode";
           cmd.Parameters.Add(param); 
    
           //cmd.ExecuteNonQuery();  //You don't need this line
    
           DataTable data = new DataTable(); 
    
           using (SqlCeDataReader reader = cmd.ExecuteReader()) 
           { 
              data.Load(reader); //SqlCeDataReader does not support the HasRows property.
              if(data.Rows.Count > 0)
              {
                  this.dataGrid1.DataSource = data;
              } 
           } 
    
           }
       }
    
    
    } 
    

    Intellisense should be able to clean up any errors with the above but feel free to ask for more help.

    Finally, you also might be able to set the data source of the grid directly to a datareader, try it!

           using (SqlCeDataReader reader = cmd.ExecuteReader()) 
           { 
              dataGrid1.DataSource = reader; 
           } 
    

    You can then get rid of the DataTable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a Windows Mobile app that my company is providing the devices
i'm developing bluetooth app on VisualStudio2008 windows mobile 6 sdk.I searched that i will
I'm developing an app for windows mobile . I have my own control that
I am developing an app for Windows Mobile 6 and there is a CameraCaptureDialog
I am developing a forms app (not web) for Windows Mobile, using .NET CF
I am developing a mobile application for Windows Mobile. I would like that the
Can we start developing apps for the upcoming windows mobile 7. Are developer tools
I'm developing a Windows Mobile WinForm app with C# and .Net Compact Framework 2.0
I'm developing an app for Windows Mobile 5.0 and above, with C# and .NET
I'm developing a WinForm app for Windows Mobile 6.0 with C#, .NET Compact Framework

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.