can you check for me what is wrong to my code when i click the add button i am not seeing any gridview in the page
this is the .aspx.cs code
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication5
{
public partial class request3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SaveToDatabaseButton.Visible = false;
if (Page.IsPostBack)
{
BindTime();
if (ViewState["Requests"] == null)
{
ViewState["Requests"] = new DataTable();
((DataTable)ViewState["Requests"]).Columns.Add("SUBMITTED_BY", typeof(string));
((DataTable)ViewState["Requests"]).Columns.Add("DEPARTMENT", typeof(string));
((DataTable)ViewState["Requests"]).Columns.Add("DATE", typeof(DateTime));
((DataTable)ViewState["Requests"]).Columns.Add("START_TIME", typeof(string));
((DataTable)ViewState["Requests"]).Columns.Add("END_TIME", typeof(string));
((DataTable)ViewState["Requests"]).Columns.Add("REASON_FOR_OVERTIME", typeof(string));
((DataTable)ViewState["Requests"]).Columns.Add("STATUS", typeof(string));
GridView1.DataSource = (DataTable)ViewState["Requests"];
GridView1.DataBind();
}
}
}
private void BindTime()
{
// Set the start time (00:00 means 12:00 AM)
DateTime StartTime = DateTime.ParseExact("00:00", "HH:mm", null);
// Set the end time (23:55 means 11:55 PM)
DateTime EndTime = DateTime.ParseExact("23:55", "HH:mm", null);
TimeSpan Interval = new TimeSpan(0, 5, 0);
ddlTimeFrom.Items.Clear();
ddlTimeTo.Items.Clear();
while (StartTime <= EndTime)
{
ddlTimeFrom.Items.Add(StartTime.ToShortTimeString());
ddlTimeTo.Items.Add(StartTime.ToShortTimeString());
StartTime = StartTime.Add(Interval);
}
ddlTimeFrom.Items.Insert(0, new ListItem("--Select--", "0"));
ddlTimeTo.Items.Insert(0, new ListItem("--Select--", "0"));
}
protected void AddButton_Click(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["OvertimeRequestConnectionString"].ConnectionString;
using (SqlConnection connection = new SqlConnection(connectionString))
{
string query = "SELECT e.FIRST_NAME FROM EMPLOYEE_TABLE e INNER JOIN USER_TABLE u ON e.Id = u.EMPLOYEE_ID where u.USERNAME = '" + Session["USERNAME"] + "' AND u.PASSWORD = '" + Session["PASSWORD"] + "'";
using (SqlCommand command = new SqlCommand(query, connection))
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
Session["FIRST_NAME"] = reader.GetString(0);
}
}
}
using (SqlConnection connection = new SqlConnection(connectionString))
{
string query1 = "SELECT d.DEPARTMENT_NAME FROM DEPARTMENT d INNER JOIN USER_TABLE u ON d.DEPARTMENT_ID = u.DEPARTMENT_ID where u.USERNAME = '" + Session["USERNAME"] + "' AND u.PASSWORD = '" + Session["PASSWORD"] + "'";
using (SqlCommand command = new SqlCommand(query1, connection))
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
Session["DEPARTMENT"] = reader.GetString(0);
}
}
}
string submittedBy = Session["FIRST_NAME"].ToString();
string department = Session["DEPARTMENT"].ToString();
DateTime date = DateTime.Parse(TextBox1.Text);
string startTime = ddlTimeFrom.SelectedItem.Text;
string endTime = ddlTimeTo.SelectedItem.Text;
string reason = TextBox2.Text;
string status = "requested";
DataTable dt = (DataTable)ViewState["Requests"];
dt.Rows.Add(submittedBy, department, date, startTime, endTime, reason, status);
ViewState["Requests"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
GridView1.Visible = true;
SaveToDatabaseButton.Visible = true;
}
protected void SaveToDatabaseButton_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["Requests"];
string connectionString = ConfigurationManager.ConnectionStrings["OvertimeRequestConnectionString"].ConnectionString;
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
foreach (DataRow row in dt.Rows)
{
string submittedBy = row["SUBMITTED_BY"].ToString();
string department = row["DEPARTMENT"].ToString();
DateTime date = (DateTime)row["DATE"];
string startTime = row["START_TIME"].ToString();
string endTime = row["END_TIME"].ToString();
string reason = row["REASON_FOR_OVERTIME"].ToString();
string status = row["STATUS"].ToString();
string query = "INSERT INTO OVERTIME_REQUEST (SUBMITTED_BY, DEPARTMENT, DATE, START_TIME, END_TIME, REASON_FOR_OVERTIME,STATUS) VALUES (@SubmittedBy, @Department, @Date, @StartTime, @EndTime, @Reason, @status)";
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@SubmittedBy", submittedBy);
command.Parameters.AddWithValue("@Department", department);
command.Parameters.AddWithValue("@Date", date);
command.Parameters.AddWithValue("@StartTime", startTime);
command.Parameters.AddWithValue("@EndTime", endTime);
command.Parameters.AddWithValue("@Reason", reason);
command.Parameters.AddWithValue("@status", status);
command.ExecuteNonQuery();
}
}
}
ViewState["Requests"] = null;
GridView1.DataSource = null;
GridView1.DataBind();
}
}
}
the next one is the .aspx code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="request3.aspx.cs" Inherits="WebApplication5.request3" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 { height: 77px; }
</style>
</head>
<body>
<form id="form2" runat="server">
<div>
<div class="auto-style1">
<br />
DATE:-<asp:TextBox ID="TextBox1" runat="server" Width="168px" TextMode="Date"></asp:TextBox>
</div>
<fieldset style="width: 250px; margin-top: 55px;">
<legend>Select Time
</legend>
From:
<asp:DropDownList ID="ddlTimeFrom" runat="server"></asp:DropDownList>
To:
<asp:DropDownList ID="ddlTimeTo" runat="server"></asp:DropDownList>
</fieldset>
<p>
Reason:-<asp:TextBox ID="TextBox2" runat="server" Height="24px" Width="306px"></asp:TextBox>
</p>
<p>
<asp:Button ID="AddButton" runat="server" Style="margin-bottom: 0px; width: 41px;" Text="ADD" OnClick="AddButton_Click" />
</p>
</div>
<div>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
</asp:GridView>
<asp:Button ID="SaveToDatabaseButton" runat="server" OnClick="SaveToDatabaseButton_Click" Text="SAVE" />
</form>
</body>
</html>