protected void BookTicket()
    {
        try
        {
            int sourceStationId = 0; int destinationStationId = 0; string journeyDate = "";
            long serviceId = 0; int serviceTransId = 0; int noOfSeats = 0; string address = ""; string contactNo = ""; string emailID = "";
            long blockedTicketId = 0; string apiname = ""; string boradingpointid = ""; int k = 0;
            string status = ""; string api = "";
            DataSet dsbookresult = null;
            if (Session["manabusrefno"] != null)
            {
                ObjBAL = new ClsBAL();
                ObjBAL.manabusRefNo = Session["manabusrefno"].ToString();
                _objDataSetBook = (DataSet)ObjBAL.GetTcktDetByMRefNo();
                if (_objDataSetBook != null)
                {
                    if (_objDataSetBook.Tables.Count > 0)
                    {
                        if (_objDataSetBook.Tables[0].Rows.Count > 0)
                        {
                            if (_objDataSetBook.Tables[0].Rows.Count < 3)
                            {
                                #region Book Ticket
                                foreach (DataRow dr in _objDataSetBook.Tables[0].Rows)
                                {
                                    string manabusrefNo = dr["OnewayMBRefNo"].ToString();
                                    apiname = dr["APIName"].ToString();
                                    sourceStationId = Convert.ToInt32(dr["SourceId"].ToString());
                                    destinationStationId = Convert.ToInt32(dr["DestinationId"].ToString());
                                    DateTime doj = Convert.ToDateTime(dr["DateOfJourney"]);
                                    boradingpointid = dr["BoardingPointID"].ToString();

                                    if (apiname == "Abhibus" || apiname == "Kallada")
                                    {
                                        journeyDate = doj.ToString("yyyy-MM-dd");
                                    }
                                    else
                                    {
                                        journeyDate = doj.ToString("yyyy/MM/dd");
                                    }

                                    if (dr["ServiceID"].ToString() == "")
                                    {
                                        serviceId = 0;
                                    }
                                    else
                                    {
                                        serviceId = Convert.ToInt64(dr["ServiceID"].ToString());
                                    }
                                    string bookinId = "";

                                    address = dr["Address"].ToString();
                                    contactNo = dr["ContactNo"].ToString();
                                    emailID = dr["EmailId"].ToString();
                                    if (dr["ServiceTranDateID"].ToString() == "")
                                    {
                                        serviceTransId = 0;
                                    }
                                    else
                                    {
                                        serviceTransId = Convert.ToInt32(dr["ServiceTranDateID"].ToString());
                                    }
                                    noOfSeats = Convert.ToInt32(dr["NoOfSeats"].ToString());
                                    if (apiname != "TicketGoose")
                                    {
                                        if (dr["BlockedId"].ToString() == "")
                                        {
                                            blockedTicketId = 0;
                                        }
                                        else
                                        {
                                            blockedTicketId = Convert.ToInt64(dr["BlockedId"].ToString());
                                        }
                                    }
                                    else { blockedTicketId = 0; bookinId = Convert.ToString(dr["BlockedId"].ToString()); }
                                    string seatNos = dr["SeatNos"].ToString();
                                    string gendertype = dr["Gender"].ToString();
                                    string psgrname = dr["FullName"].ToString();
                                    string idType = dr["IDType"].ToString();
                                    string idNo = dr["IDNumber"].ToString();
                                    string idIssuedBy = dr["IDIssuedBy"].ToString();
                                    string[] selectedSeatsArray = seatNos.Split(',');
                                    book_ticket bookTicket = new book_ticket();
                                    object[] obj = new object[2];

                                    book_ticketSeat_detailsSeat_detail[] sD = new book_ticketSeat_detailsSeat_detail[noOfSeats];

                                    string strPassengers = dr["PassengerDetails"].ToString();
                                    string[] strPassengersArray = strPassengers.Split(',');

                                    for (int i = 0; i < noOfSeats; i++)
                                    {
                                        book_ticketSeat_detailsSeat_detail sdd = new book_ticketSeat_detailsSeat_detail();
                                        sdd.seat_number = selectedSeatsArray[i].ToString();

                                        string strPassenger = strPassengersArray[i].ToString();
                                        string[] strPassengerArray = strPassenger.Split('-');

                                        sdd.title = strPassengerArray[1].ToString();

                                        if (strPassengerArray[1].ToString() == "Mr")
                                        {
                                            sdd.sex = "M";
                                        }
                                        else { sdd.sex = "F"; }

                                        if (i == 0) { sdd.is_primary = "true"; }
                                        else { sdd.is_primary = "false"; }
                                        sdd.address = address;

                                        string id = "";//1 -> Pan Card, 2 -> D/L, 3 -> Passport, 4 -> Voter, 5 -> Aadhar Card
                                        if (idType.ToString().ToLower().Contains("pan"))
                                        {
                                            id = "1";
                                        }
                                        else if (idType.ToString().ToLower().Contains("dri"))
                                        {
                                            id = "2";
                                        }
                                        else if (idType.ToString().ToLower().Contains("pass"))
                                        {
                                            id = "3";
                                        }
                                        else if (idType.ToString().ToLower().Contains("voter"))
                                        {
                                            id = "4";
                                        }
                                        else if (idType.ToString().ToLower().Contains("adhar"))
                                        {
                                            id = "5";
                                        }
                                        else if (idType.ToString().ToLower().Contains("ration"))
                                        {
                                            id = "4";
                                        }

                                        sdd.id_card_type = id;

                                        sdd.id_card_number = idNo;
                                        sdd.id_card_issued_by = idIssuedBy;
                                        sD[i] = sdd;
                                    }

                                    book_ticketSeat_details ss = new book_ticketSeat_details();
                                    ss.seat_detail = sD;
                                    book_ticketContact_detail cc = new book_ticketContact_detail();
                                    cc.mobile_number = contactNo;
                                    cc.email = emailID;
                                    cc.emergency_name = contactNo;
                                    obj[0] = ss;
                                    bookTicket.Items = obj;

                                    dsbookresult = CommonBookticket(sourceStationId, destinationStationId, journeyDate, serviceId, serviceTransId,
                                        blockedTicketId, boradingpointid, bookTicket, noOfSeats, seatNos, gendertype, psgrname, address,
                                        dr["FullName"].ToString(), contactNo, emailID, Session["manabusrefno"].ToString(), manabusrefNo,
                                        apiname, out api, out status, bookinId);
                                    if (status == "Success")
                                    {
                                        InsertBookedTicketDetails(dsbookresult, manabusrefNo, api);
                                    }
                                    else
                                    {
                                        lblReturnMsg.Text = "Failed to book. Please contact administartor of LoveJourney.";
                                        lblReturnMsg.Visible = true;
                                    }
                                }
                                #endregion

                                #region GetTicketDetails
                                if (status == "Success")
                                {
                                    #region Update Cash Coupon
                                    UpdateCashCoupon(Session["manabusrefno"].ToString(), emailID);
                                    #endregion
                                    GetTicketDetails();
                                }
                                #endregion
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }