Click here to Skip to main content
15,312,535 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
   
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.
   
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;
}
OriginalGriff 15-Mar-15 8:57am
   
Look at your code.
Then look at the code I suggested you used.

See the difference?
El Dev 15-Mar-15 9:22am
   
Yeah there is a difference but it does not work too. I am getting the last record but many times.
OriginalGriff 15-Mar-15 9:30am
   
No, once you are getting the record many times, you have at least started - but now you need to start using different data.
Which means using the value in your foreach loop: roomitem
Because it's the only thing which changes between rooms! So if you don't use it, you will get the same data for all the rooms, won't you?

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