Hi itsme,
Refer below sample.
HTML
ServiceName:
<asp:TextBox runat="server" ID="txtServiceName" />
<br />
Email :
<asp:TextBox runat="server" ID="txtEmail" />
<br />
UserName :
<asp:TextBox runat="server" ID="txtName" />
<br />
Booked Date Date :
<asp:TextBox runat="server" ID="txtDate" />
<br />
<p>
Book Slot:</p>
<asp:DropDownList ID="ddlbookSlot" runat="server">
<asp:ListItem Text="Select" />
<asp:ListItem Text="Morning" />
<asp:ListItem Text="Noon" />
<asp:ListItem Text="Evening" />
</asp:DropDownList>
<asp:Button ID="btnsubmit" runat="server" Text="Submit" OnClick="btnsubmit_Click">
</asp:Button>
appoint_req.aspx
<asp:GridView runat="server" AutoGenerateColumns="false" ID="gvRequest">
<Columns>
<asp:BoundField DataField="ServiceName" HeaderText="ServiceName" />
<asp:BoundField DataField="Email" HeaderText="Email" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Date" HeaderText="Date" />
<asp:BoundField DataField="Slot" HeaderText="Slot" />
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label Text='<%#Eval("Status") %>' runat="server" />
<asp:LinkButton ID="lnkStatus" Text="Accept" runat="server" OnClick="Update" CommandArgument='<%#Eval("BookId") %>' />
<asp:LinkButton ID="LinkButton1" Text="Reject" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Default.aspx
<asp:DropDownList runat="server" ID="ddlTime">
</asp:DropDownList>
<br />
<asp:Button Text="Send" runat="server" OnClick="Save" />
Namespaces
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
Code
CS.aspx.cs
protected void btnsubmit_Click(object sender, EventArgs e)
{
int i = 0;
try
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection conn = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("SELECT Status FROM Booking WHERE ServiceName=@ServiceName AND Date =@Date", conn);
cmd.Parameters.AddWithValue("@ServiceName", txtServiceName.Text);
cmd.Parameters.AddWithValue("@Date", Convert.ToDateTime(txtDate.Text));
conn.Open();
string status = Convert.ToString(cmd.ExecuteScalar());
if (string.IsNullOrEmpty(status))
{
cmd = new SqlCommand("INSERT INTO Booking(ServiceName,Email,Name,Date,Slot,Status) VALUES(@ServiceName,@Email,@Name,@Date,@Slot,@Status)", conn);
cmd.Parameters.AddWithValue("@ServiceName", txtServiceName.Text);
cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Date", Convert.ToDateTime(txtDate.Text));
cmd.Parameters.AddWithValue("@Slot", ddlbookSlot.SelectedItem.Text);
cmd.Parameters.AddWithValue("@Status", "UnApproved");
i = cmd.ExecuteNonQuery();
conn.Close();
if (i > 0)
{
Response.Redirect("appoint_req.aspx");
}
else
{
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Error While Adding.');", true);
}
}
else
{
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Slot is booked.');", true);
}
}
catch (Exception ex)
{
Response.Write(ex);
}
}
appoint_req.aspx.cs
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("SELECT * FROM Booking", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
gvRequest.DataSource = dt;
gvRequest.DataBind();
}
protected void Update(object sender, EventArgs e)
{
LinkButton lnk = (sender) as LinkButton;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("UPDATE Booking SET Status = @Status WHERE BookId = @BookId", con);
cmd.Parameters.AddWithValue("@BookId", lnk.CommandArgument);
cmd.Parameters.AddWithValue("@Status", "Approved");
con.Open();
int result = Convert.ToInt16(cmd.ExecuteNonQuery());
if (result > 0 && lnk.Text.ToLower() == "accept")
{
Response.Redirect("Default.aspx?Id=" + lnk.CommandArgument);
}
con.Close();
BindGrid();
}
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection conn = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("SELECT * FROM Booking WHERE BookId=@Id", conn);
cmd.Parameters.AddWithValue("@Id", Request.QueryString["Id"]);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows[0]["Slot"].ToString().ToLower() == "morning")
{
DateTime dtstart = Convert.ToDateTime("06:00");
DateTime StartTime = DateTime.ParseExact(dtstart.ToString("HH:mm"), "HH:mm", null);
DateTime dtend = Convert.ToDateTime("12:00");
DateTime EndTime = DateTime.ParseExact(dtend.ToString("HH:mm"), "HH:mm", null);
TimeSpan Interval = new TimeSpan(1, 0, 0);
while (StartTime <= EndTime)
{
if (StartTime == Convert.ToDateTime("12:00"))
{
ddlTime.Items.Add(StartTime.AddMinutes(-5).ToShortTimeString());
}
else
{
ddlTime.Items.Add(StartTime.ToShortTimeString());
}
StartTime = StartTime.Add(Interval);
}
ddlTime.Items.Insert(0, new ListItem("--Select--", "0"));
}
}
}
protected void Save(object sender, EventArgs e)
{
string timeslot = ddlTime.SelectedItem.Text;
// write to code save in datable table.
ScriptManager.RegisterStartupScript(this, this.GetType(), "CropImage", "alert('Your time slot is : " + timeslot + "');", true);
}
Screenshot