0

I am programming an application using SQL Server Compact Edition in which my program inserts some data from somewhere to a local database. My database table has columns like: username, enterdate, exitdate... and so on.

Now I am trying to read a special row of my data and then add that row to a listview, but when I use the SqlCeDataReader and its Read(); method, it adds duplicate items to my listview. For example if my needed row is at the 9th index of the database, I mean when my desired row for example is in the 9th row of my database when I SELECT it using WHERE and then I read it, it adds 9 duplicate items with the same row data I need to my listview.

I am using Microsoft Visual Studio 2008 Express Edition.

Con.ConnectionString = "data source = |datadirectory|\\RoomDataBase.sdf";
Con.Open();
cmd.Connection = Con;
cmd.CommandText = "SELECT * FROM room WHERE username = '" + user + "'";

SqlCeDataReader reader1 = cmd.ExecuteReader();

if (reader1.Read() )
{
       dblogger(user + " Entered and WAS in list");
       //my problem happens here that the above line iterates
}

dblogger is a void that adds a string to a list view

In fact my problem is that the inside of if{....} iterates and iterates to number of the number of row in the database

I must tell you that when I SELECT data from database using WHERE I am sure that only one row matches the criteria, so to be sure about everything, the problem is from the Read() procedure.

And I must mention you that this problem either happens when I use SqlCeResultSet and then using it ReadFirst(); .

Special thanks to those who know the answer.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sarah
  • 1
  • 2

1 Answers1

1

Your're using the DataReader incorrectly. The read method is used for advancing the the DataReader to the next row. The HasRows property which will tell you if there were any results returned at all.

Try the following:

     if (reader1.HasRows)
     {
         dblogger(user + " Entered and WAS in list");
     }
Tylerflick
  • 101
  • 1
  • 3
  • thank you but when i try to use HasRows It says it must be Scrollabel, How Can i change its Option to scrollabe? – Sarah Mar 23 '14 at 19:53
  • And i tested HasRow Propery with SqlCeResultSet but still i have the same problem. – Sarah Mar 23 '14 at 20:42
  • I'm not sure why the IDE would be telling you that. You could try the DataReader's ExecuteScalar method, which would only return the first row of the result set. – Tylerflick Mar 25 '14 at 01:05