You must Sign In to post a response.
  • Category: LINQ

    Search records with fromdate

    hi Everyone,

    List<DateTime> Dates = GetDate(txtfromdate.Text.Trim(), txttodate.Text.Trim());
    //list , enumerable,linq:
    for (int index = 0; index < Dates.Count(s => Dates.Contains(s)); index++)
    {
    for (int row = 0; row < dtKeenal.Rows.Count; row++)
    {
    if (dtKeenal.Rows[row]["Fromdate"].ToString() != string.Empty && dtKeenal.Rows[row]["Todate"].ToString() != string.Empty)
    {
    if (Convert.ToDateTime(Dates[index]) >= Convert.ToDateTime(dtKeenal.Rows[row]["Fromdate"].ToString()) && Convert.ToDateTime(Dates[index]) <= Convert.ToDateTime(dtKeenal.Rows[row]["Todate"].ToString()))
    {
    string SeatList = GetSeatNo(Convert.ToDateTime(dtKeenal.Rows[row]["Fromdate"].ToString()), Convert.ToDateTime(dtKeenal.Rows[row]["Todate"].ToString()));

    if (SeatNo == string.Empty)
    SeatNo = SeatList;


    else
    SeatNo = SeatNo + "," + SeatList;
    }
    }
    }
    }


    DataTable kennal = new DataTable();
    kennal.Columns.Add("Bookedkennel", typeof(string));
    kennal.Rows.Add(SeatNo);
    GenerateKennelAllocations(kennal);
    }

    private string GetSeatNo(DateTime FromDate, DateTime ToDate)
    {
    OleDbConnection con = Connection.DBconnection();
    // string date = System.DateTime.Now.ToString("yyyy-MM-dd");
    string get = "select * from tbl_Petprofile where Fromdate = '" + FromDate.ToString("yyyy-MM-dd") + "' and Todate = '" + ToDate.ToString("yyyy-MM-dd") + "'";
    OleDbDataAdapter sda = new OleDbDataAdapter(get, con);
    DataTable dtKeenal = new DataTable();
    sda.Fill(dtKeenal);
    con.Close();
    return dtKeenal.Rows[0]["Bookedkennel"].ToString();
    }

    private List<DateTime> GetDate(string FromDate, string ToDate)
    {
    var dates = new List<DateTime>();
    DateTime StartDate = Convert.ToDateTime(FromDate);
    DateTime EndDate = Convert.ToDateTime(ToDate);
    for (var dt = StartDate; dt <= EndDate; dt = dt.AddDays(1))
    dates.Add(dt);
    return dates;
    }

    Above am addes my code for get records with from and todate then i got output.
    But how am search records with only fromdate.if user select from date then click view available rooms.it will show the list of rooms only if is related with fromdate.

    if anyone know please tell me how i done it.
  • #761655
    1. If dtKeenal.Rows[row]["Todate"].ToString() == string.Empty, you can assign ToDate = FromDate

    2. You can create one overload method "GetSeatNo"
    having only FromDate

    3. In the query itself you can check whether the "Todate" is empty.

    By Nathan
    Direction is important than speed

  • #761659
    Hello Paul Raj,

    You can do it by using below query :

    FromDate.Text = "01/01/2015";
    ToDate.Text = DateTime.Today.Date.ToShortDateString();;

    SELECT * FROM RoomBooking WHERE (Booking_Date BETWEEN '" + FromDate.Text + "' AND '" + ToDate.Text + "')

    Hope this will help you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #761686

    Hai Paul,
    This is very common issue when you want to search records between dates or in date range.
    You need to have the same format of the date both sides of the comparison operator.
    Hope it will be helpful to you.


    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


Sign In to post your comments