protected void btnReserve_Click(object sender, EventArgs e) { if (Session["UserName"] == null) { Response.Redirect("~/UserLogin.aspx"); } else { int lineId = Convert.ToInt32(Request["id"]); var lineTable = new TourLineTableAdapter().GetDataByID(lineId); var lineRow = lineTable.Single(); // 判断当天余量是否充足 long lineID = lineRow.LineID; // 订单编号 int maxPeople = lineRow.LineMaxPeople; // 最大人数 DateTime travelTime = CaleOrderTime.SelectedDate.Date; // 旅行日期 if (travelTime == DateTime.Parse("0001-01-01")) { // 判断是否选择合法日期 ClientScript.RegisterStartupScript(Page.GetType(), "ss", "<script>alert('请选择日期')</script>"); return; } // 获取已选人数 string count = string.Format("select count(*) as ordered from OrderInfo where LineID={0} and TravelDate='{1}';", lineID, travelTime); SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DataSource"].ConnectionString); SqlCommand cmd = new SqlCommand(count, conn); try { conn.Open(); // 打开数据库连接 int ordered = (int)(cmd.ExecuteScalar()); // 获取已选人数 if (ordered >= maxPeople) { ClientScript.RegisterStartupScript(Page.GetType(), "ss", "<script>alert('Sorry,当前已选人数已满,请更换日期')</script>"); } } catch { } finally { conn.Close(); // 关闭数据库连接 } string orderID = DateTime.Now.ToString("yyyyMMddHHmmss") + Session["UserName"].ToString(); // 订单编号 // 将此订单加入数据库 string insert = string.Format(@"insert into OrderInfo(OrderID, UserName, LineID, TravelDate, IsComplete, IsComment) Values('{0}', '{1}', {2}, '{3}', {4}, {5})", orderID, Session["UserName"].ToString(), lineID, travelTime, 0, 0); SqlCommand insertCmd = new SqlCommand(insert, conn); try { conn.Open(); insertCmd.ExecuteNonQuery(); ClientScript.RegisterStartupScript(Page.GetType(), "ss", "<script>alert('订购成功');{location.href='UserViewOrders.aspx'}</script>"); } catch { } finally { conn.Close(); } } }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { int lineId = Convert.ToInt32(Request["id"]); var lineTable = new TourLineTableAdapter().GetDataByID(lineId); if (lineTable.Count <= 0) { litLineName.Text = "线路不存在"; } var lineRow = lineTable.Single(); litLineName.Text = lineRow.LineName; litLineType.Text = lineRow.LineType; litLinePrice.Text = lineRow.LinePrice.ToString() + "¥"; litLineDays.Text = lineRow.LineDays.ToString() + "天"; litLineMaxPeople.Text = lineRow.LineMaxPeople.ToString() + "人"; litLineIntroduce.Text = lineRow.LineIntroduce; litLineAttention.Text = lineRow.LineAttention; this.Title = litLineName.Text; // 显示评论 SqlConnection conn = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["DataSource"].ConnectionString); string select = string.Format("select UserName, Grade, Content from CommentTable where LineID={0}", Convert.ToInt32(Request["id"])); DataSet dataset = new DataSet(); try { conn.Open(); SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(select, conn); adapter.Fill(dataset); GridView1.DataSource = dataset; GridView1.DataBind(); } finally { conn.Close(); } // 综合评分 var table = new GradeTableAdapters.CommentTableTableAdapter(); int? count = table.ScalarQueryCount(Convert.ToInt32(Request["id"])); double? sum = table.ScalarQuerySum(Convert.ToInt32(Request["id"])); if ((count == null) || (sum == null)) { litGrade.Text = "尚无评分"; } else { litGrade.Text = (sum / count).ToString() + "/5"; } } }