public void ShowData() { string id = Request.QueryString["id"]; DataTable dt = BusDetailDAO.GetBusDetail(id); if (dt.Rows.Count > 0) { GridView1.DataSource = dt; GridView1.DataBind(); } else { dt.Rows.Add(dt.NewRow()); GridView1.DataSource = dt; GridView1.DataBind(); int columncount = GridView1.Rows[0].Cells.Count; GridView1.Rows[0].Cells.Clear(); GridView1.Rows[0].Cells.Add(new TableCell()); GridView1.Rows[0].Cells[0].ColumnSpan = columncount; GridView1.Rows[0].Cells[0].Text = "No Records Found"; } }
protected void AddBus(object sender, EventArgs e) { string name = txtBusName.Text; string routeID = ddlRoute.SelectedItem.Text; string f = txtFare.Text; string date = txtDate.Value; string seat = txtSeat.Text; string time = txtTime.Text; double fare; int totalSeat; if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(f) || string.IsNullOrEmpty(date) || string.IsNullOrEmpty(time) || string.IsNullOrEmpty(seat)) { AlertMessage("All data must not be empty!", "alert alert-danger"); } else if (!double.TryParse(txtFare.Text, out fare) || fare <= 0) { AlertMessage("Fare must be a possitive number!", "alert alert-danger"); } else if (!int.TryParse(seat, out totalSeat) || totalSeat <= 0) { AlertMessage("Total seat must be a possitive number!", "alert alert-danger"); } else { SqlConnection conn = DAO.Connection; SqlCommand cmd = conn.CreateCommand(); conn.Open(); Bus b = new Bus(); // create random and check BusID duplicate string bid; do { cmd.Parameters.Clear(); bid = DAO.RandomID(); cmd.CommandText = "SELECT * FROM Buses WHERE BusID = @id"; cmd.Parameters.AddWithValue("@id", bid); SqlDataReader reader = cmd.ExecuteReader(); bool exist = reader.Read(); reader.Close(); if (!exist) { break; } } while (true); //insert bus b.BusID = bid; b.BusName = name; b.RouteID = routeID; b.Fare = fare; b.TotalSeat = totalSeat; BusDAO.AddBus(b); // create random and check BusDetailID duplicate string bdID; do { cmd.Parameters.Clear(); bdID = DAO.RandomID(); cmd.CommandText = "SELECT * FROM BusDetail WHERE BusDetailID = @bdid"; cmd.Parameters.AddWithValue("@bdid", bdID); SqlDataReader reader = cmd.ExecuteReader(); bool exist = reader.Read(); reader.Close(); if (!exist) { break; } } while (true); BusDetail bd = new BusDetail(); bd.BusDetailID = bdID; bd.BusID = bid; bd.DepartureTime = time; bd.DepartureDate = Convert.ToDateTime(date); bd.AvailableSeat = totalSeat; BusDetailDAO.AddBusDetail(bd); conn.Close(); Response.Redirect("AdminBus.aspx"); } }