Beispiel #1
0
        protected void BtnReject_Click(object sender, EventArgs e)
        {
            int rowIndex = ((sender as LinkButton).NamingContainer as GridViewRow).RowIndex;

            //Get the value of column from the DataKeys using the RowIndex.
            int               id         = Convert.ToInt32(GridView1.DataKeys[rowIndex].Values[0]);
            int               userId     = Convert.ToInt32(GridView1.DataKeys[rowIndex].Values[2]);
            string            email      = Session["email"].ToString();
            HouseRentEntities houseRent  = new HouseRentEntities();
            var               emailCheck = houseRent.Users.Where(a => (a.Email.ToLower() == email.ToLower())).FirstOrDefault();
            //User user = houseRent.Users.First(i => i.Email == email);
            UserPost userPost = houseRent.UserPosts.First(i => i.PostId == id && i.RequestedUserId == userId);

            userPost.PostStatus  = 3;
            userPost.UpdatedDate = DateTime.Now;
            userPost.UpdatedBy   = emailCheck.id;
            houseRent.SaveChanges();
            Response.Redirect("ViewRequestedUsers.aspx");
        }
        protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton        lnkBtn     = (LinkButton)e.Row.FindControl("BtnBookHouse");
                Label             lblStatus  = (Label)e.Row.FindControl("lblStatus");
                int               postId     = Convert.ToInt32(GridView1.DataKeys[e.Row.RowIndex].Values[0]);
                string            email      = Session["email"].ToString();
                HouseRentEntities houseRent  = new HouseRentEntities();
                var               emailCheck = houseRent.Users.Where(a => (a.Email.ToLower() == email.ToLower())).FirstOrDefault();
                UserPost          userPost   = houseRent.UserPosts.FirstOrDefault(i => i.PostId == postId && i.RequestedUserId == emailCheck.id);

                if (userPost != null && (userPost.PostStatus == 1 || userPost.PostStatus == 2))
                {
                    lblStatus.Text = userPost.Status.StatusDescription;
                    lnkBtn.Visible = false;
                }
            }
        }
        private void BindGridView()
        {
            try
            {
                string            email     = Session["email"].ToString();
                HouseRentEntities houseRent = new HouseRentEntities();
                Post     post       = new Post();
                User     user       = new User();
                UserPost userPost   = new UserPost();
                var      emailCheck = houseRent.Users.Where(a => (a.Email.ToLower() == email.ToLower())).FirstOrDefault();
                //var postdetailsCheck = houseRent.UserPosts.Where(a => a.RequestedUserId == emailCheck.id).Select(a=> a.PostId).;
                var postdetailsCheck = (from x in houseRent.UserPosts select x).Where(a => a.RequestedUserId == emailCheck.id).DefaultIfEmpty();
                //var postDetails = houseRent.Posts.Where(a => (a.Id == postdetailsCheck.)).ToList();

                //var postDetails = (from x in houseRent.Posts select x).Where(x => (x.Id == postdetailsCheck.Where(a=> a.PostId));
                var postDetails = (from a in houseRent.Posts
                                   join b in postdetailsCheck on a.Id equals b.PostId
                                   select new
                {
                    Id = a.Id,
                    HouseName = a.HouseName,
                    Rent = a.Rent,
                    Image = a.Image,
                    Status = b.Status.StatusDescription,
                    statusId = b.PostStatus
                }
                                   ).ToList();


                GridView1.DataSource = postDetails;
                GridView1.DataBind();
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }
        protected void BtnBookHouse_Click(object sender, EventArgs e)
        {
            try
            {
                int               rowIndex        = ((sender as LinkButton).NamingContainer as GridViewRow).RowIndex;
                int               postId          = Convert.ToInt32(GridView1.DataKeys[rowIndex].Values[0]);
                string            email           = Session["email"].ToString();
                HouseRentEntities houseRent       = new HouseRentEntities();
                var               emailCheck      = houseRent.Users.Where(a => (a.Email.ToLower() == email.ToLower())).FirstOrDefault();
                int               requestedUserId = emailCheck.id;
                int               statusId        = 1;
                UserPost          userPost        = houseRent.UserPosts.Where(a => a.PostId == postId && a.RequestedUserId == emailCheck.id).FirstOrDefault();
                if (userPost != null && userPost.PostStatus != 1)
                {
                    userPost.PostStatus  = statusId;
                    userPost.UpdatedBy   = requestedUserId;
                    userPost.UpdatedDate = DateTime.Now;
                    houseRent.SaveChanges();
                }
                else
                {
                    UserPost userP = new UserPost();

                    userP.PostId          = postId;
                    userP.RequestedUserId = requestedUserId;
                    userP.PostStatus      = statusId;
                    userP.CreatedDate     = DateTime.Now;
                    userP.CreatedBy       = requestedUserId;
                    userP.UpdatedBy       = requestedUserId;
                    userP.UpdatedDate     = DateTime.Now;
                    houseRent.UserPosts.Add(userP);
                    houseRent.SaveChanges();
                }



                Response.Redirect("CustomerPage.aspx");
                //var userPostCheck = houseRent.UserPosts.Where(a => a.PostId == postId && a.RequestedUserId == requestedUserId).FirstOrDefault();
                //if(userPostCheck != null)
                //{
                //    LinkButton BtnBookHouse = (LinkButton)sender;
                //    BtnBookHouse.Visible = false;
                //    Label lblStatus = (Label)sender;
                //    lblStatus.Text = "Requested";
                //}

                //((Label)GridView1.SelectedRow.FindControl("lblStatus")).Text = "Requested";
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
            //finally
            //{
            //    int rowIndex = ((sender as LinkButton).NamingContainer as GridViewRow).RowIndex;
            //    int postId = Convert.ToInt32(GridView1.DataKeys[rowIndex].Values[0]);
            //    string email = Session["email"].ToString();
            //    HouseRentEntities houseRent = new HouseRentEntities();
            //    var emailCheck = houseRent.Users.Where(a => (a.Email.ToLower() == email.ToLower())).FirstOrDefault();
            //    int requestedUserId = emailCheck.id;

            //    var userPostCheck = houseRent.UserPosts.Where(a => a.PostId == postId && a.RequestedUserId == requestedUserId).FirstOrDefault();
            //    if (userPostCheck != null)
            //    {

            //        LinkButton BtnBookHouse = (LinkButton)sender;
            //        BtnBookHouse.Visible = false;
            //        //int index = GridView1.EditIndex;
            //        //GridViewRow row = GridView1.Rows[index];
            //        //Label lblStatus = (Label)row.FindControl("lblStatus");
            //        //lblStatus.Text = "Requested";
            //    }
            //}
        }