Click here to Skip to main content
15,897,226 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to select data from database but I am getting duplicate data.

The Scenario is :
I have 2 records in the database like this:

RoomId RoomTitle RoomName RoomPrice
1 Room1 Room1 200
2 Room2 Room2 250

And this is my code in C#:

public static List<RoomInfo> GetAvailRoom(string Checkin, string Checkout)
   {
       DateTime CheckinDate = DateTime.ParseExact(Checkin, "M/dd/yyyy", CultureInfo.InvariantCulture.DateTimeFormat);
       DateTime CheckoutDate = DateTime.ParseExact(Checkout, "M/dd/yyyy", CultureInfo.InvariantCulture.DateTimeFormat);

       List<RoomInfo> lstRooms = RoomInfo.GetRooms();
       List<RoomImage> lstRoomImages = new List<RoomImage>();
       List<RoomInfo> lstRoomsNew = new List<RoomInfo>();
       RoomInfo _RoomInfo = new RoomInfo();

       foreach (var roomitems in lstRooms)
       {
           _RoomInfo.Checkin = CheckinDate;
           _RoomInfo.Checkout = CheckoutDate;
           var roomAvailable = _RoomInfo.GetAvailableRoomByDate();

               foreach (var items in roomAvailable)
               {

                   _RoomInfo.RoomId = items.RoomId;
                   _RoomInfo.RoomTitle = items.RoomTitle;
                   _RoomInfo.RoomName = items.RoomName;
                   _RoomInfo.RoomPrice = items.RoomPrice;
                   _RoomInfo.RoomDescription = items.RoomDescription;
                   _RoomInfo.IsBookButton = items.IsBookButton;
                   if (_RoomInfo.RoomId > 0)
                   {
                    RoomImage _RoomImage = new RoomImage();
                   _RoomImage.RoomId = _RoomInfo.RoomId;
                   var LstOfImages = _RoomImage.GetRoomImagesByRoomId();

                   HttpContext.Current.Session["RoomId"] = _RoomInfo.RoomId;

                   foreach (var images in LstOfImages)
                   {

                       _RoomInfo.RoomImageId = images.RoomImageId;
                       _RoomInfo.RoomId = images.RoomId;
                       _RoomInfo.LSTImages = LstOfImages;
                   }

                  }
                  else
                  {

                      string lblMsgError = string.Empty;
                      lblMsgError += string.Format("<div id=\"ShowError\" class=\"NotificationError\">{0}</div>", _RoomInfo.RoomNotAvailable);

                  }
           }

           lstRoomsNew.Add(_RoomInfo); // Here is the problem I think.


       }

       return lstRoomsNew;
     }


No the problem is, I am getting the second record only in duplicate, means 2 times:
2 Room2 Room2 250
Now the thing is, just check on this line: lstRoomsNew.Add(_RoomInfo); // Here is the problem I think.

I think it is where there is a problem. On that line.

Please can someone help.
Posted
Comments
Sergey Alexandrovich Kryukov 15-Mar-15 9:08am    
To start with, a list, something you add elements to, is always an object (instance) of some type, not type, it cannot be generic. Only some types are generic. And such types cannot be instantiated as objects immediately, first, they need to be instantiated into complete types.
—SA

1 solution

The problem is simple: you only create one instance of a room - but then you change the data within it:
C#
RoomInfo _RoomInfo = new RoomInfo();

foreach (var roomitems in lstRooms)
{
    _RoomInfo.Checkin = CheckinDate;
    ...
    lstRoomsNew.Add(_RoomInfo); // Here is the problem I think.
}
So when you look at your collection, you see the same data twice.

Move the instance creation inside the loop:
C#
foreach (var roomitems in lstRooms)
{
    RoomInfo _RoomInfo = new RoomInfo();
    _RoomInfo.Checkin = CheckinDate;
    ...
    lstRoomsNew.Add(_RoomInfo); // Here is the problem I think.
}
And it'll do what you want - because now you create new rooms for each set of data.
 
Share this answer
 
Comments
El Dev 15-Mar-15 7:51am    
Hi OriginalGriff,
Still same problem is coming.
I just move the instance inside the loop and then add the items to the list but it is still giving the result of last records 2 times...
OriginalGriff 15-Mar-15 8:13am    
Are you using roomitem anywhere?
El Dev 15-Mar-15 8:30am    
No I am not using it anywhere.
OriginalGriff 15-Mar-15 8:40am    
Don't you think it would be a good idea? Given that it's the only thing that changes each time round the loop? :laugh:
El Dev 15-Mar-15 8:46am    
See what I did but now its giving the last record only, the 2 different records are showing up.

public static List<roominfo> GetAvailRoom(string Checkin, string Checkout)
{

DateTime CheckinDate = DateTime.ParseExact(Checkin, "M/dd/yyyy", CultureInfo.InvariantCulture.DateTimeFormat);
DateTime CheckoutDate = DateTime.ParseExact(Checkout, "M/dd/yyyy", CultureInfo.InvariantCulture.DateTimeFormat);

List<roominfo> lstRooms = RoomInfo.GetRooms();
List<roomimage> lstRoomImages = new List<roomimage>();
List<roominfo> lstRoomsNew = new List<roominfo>();
RoomInfo _RoomInfo = new RoomInfo();

_RoomInfo.Checkin = CheckinDate;
_RoomInfo.Checkout = CheckoutDate;
var roomAvailable = _RoomInfo.GetAvailableRoomByDate();

lstRoomsNew.Add(_RoomInfo);
foreach (var items in roomAvailable)
{

_RoomInfo.RoomId = items.RoomId;
_RoomInfo.RoomTitle = items.RoomTitle;
_RoomInfo.RoomName = items.RoomName;
_RoomInfo.RoomPrice = items.RoomPrice;
_RoomInfo.RoomDescription = items.RoomDescription;
_RoomInfo.IsBookButton = items.IsBookButton;
if (_RoomInfo.RoomId > 0)
{
RoomImage _RoomImage = new RoomImage();
_RoomImage.RoomId = _RoomInfo.RoomId;
var LstOfImages = _RoomImage.GetRoomImagesByRoomId();

HttpContext.Current.Session["RoomId"] = _RoomInfo.RoomId;

foreach (var images in LstOfImages)
{

_RoomInfo.RoomImageId = images.RoomImageId;
_RoomInfo.RoomId = images.RoomId;
_RoomInfo.LSTImages = LstOfImages;

}


}
else
{

string lblMsgError = string.Empty;
lblMsgError += string.Format("<div id=\"ShowError\" class=\"NotificationError\">{0}</div>", _RoomInfo.RoomNotAvailable);
}

}
return lstRoomsNew;
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900