Reputation: 117
It was working before, I could see the Gridview
in the website. But now, I can not see it any more. I don't know what I've changed in the code.
In the page SI.aspx I have:
<asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false"
style="margin-top: 0px" CssClass="tb" HorizontalAlign="Center"
>
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="SI_id" />
<asp:TemplateField HeaderText="CTNS_NO">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SEAL_NO">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PCS">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="W.G.">
<ItemTemplate>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CBM">
<ItemTemplate>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Line"
onclick="ButtonAdd_Click" CssClass="btn"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
I think I have changed something in the code behind, thats why the gridview Could not be displayed. Here is the code behind SI.aspx.cs:
private void SetInitialRow()
{
try
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("CTNS_NO", typeof(string)));
dt.Columns.Add(new DataColumn("SEAL_NO", typeof(string)));
dt.Columns.Add(new DataColumn("PCS", typeof(string)));
dt.Columns.Add(new DataColumn("[W.G.]", typeof(string)));
dt.Columns.Add(new DataColumn("CBM", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["CTNS_NO"] = string.Empty;
dr["SEAL_NO"] = string.Empty;
dr["PCS"] = string.Empty;
dr["[W.G.]"] = string.Empty;
dr["CBM"] = string.Empty;
dt.Rows.Add(dr);
//Store the DataTable in ViewState
ViewState["SI"] = dt;
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
catch
{
}
}
private void AddNewRowToGrid()
{
try
{
int rowIndex = 0;
if (ViewState["SI"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["SI"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//extract the TextBox values
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox5");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
dtCurrentTable.Rows[i - 1]["CTNS_NO"] = box1.Text;
dtCurrentTable.Rows[i - 1]["SEAL_NO"] = box2.Text;
dtCurrentTable.Rows[i - 1]["PCS"] = box3.Text;
dtCurrentTable.Rows[i - 1]["[W.G.]"] = box4.Text;
dtCurrentTable.Rows[i - 1]["CBM"] = box5.Text;
System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["EPSYLOG_DBBConnectionString"].ConnectionString);
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "INSERT into SI ([CTNS_NO],[SEAL_NO],[PCS],[W.G.],[CBM],[name],[bk_nbr],[shipper],[cons],[pol],[pod],[notify],[size]) values('" + box1.Text + "','" + box2.Text + "','" + box3.Text + "','" + box4.Text + "','" + box5.Text + "','" + name.Text + "','" + bn.Text + "','" + shp.Text + "','" + cng.Text + "','" + pol.Text + "','" + POD.Text + "','" + notif.Text + "','" + sz.Text + "')";
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["SI"] = dtCurrentTable;
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}
catch{ }
}
private void SetPreviousData()
{
try
{
int rowIndex = 0;
if (ViewState["SI"] != null)
{
DataTable dt = (DataTable)ViewState["SI"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox5");
box1.Text = dt.Rows[i]["CTNS_NO"].ToString();
box2.Text = dt.Rows[i]["SEAL_NO"].ToString();
box3.Text = dt.Rows[i]["PCS"].ToString();
box4.Text = dt.Rows[i]["[W.G.]"].ToString();
box5.Text = dt.Rows[i]["CBM"].ToString();
rowIndex++;
}
}
}
}
catch
{ }
}
protected void Page_Load(object sender, EventArgs e)
{
Gridview1.DataBind();
if (!Page.IsPostBack)
{
SetInitialRow();
}
try
{
if (Session["MySession"] == "")
{
Response.Redirect("authentication.aspx");
}
else
{
mail.Text = Session["e"].ToString();
}
}
catch
{
Response.Redirect("authentication.aspx");
}
try
{
name.Text = Session["MySession"].ToString();
bn.Text = Convert.ToString(Request.QueryString["arg1"]);
shp.Text = Convert.ToString(Request.QueryString["arg2"]);
cng.Text = Convert.ToString(Request.QueryString["arg3"]);
pol.Text = Convert.ToString(Request.QueryString["arg4"]);
POD.Text = Convert.ToString(Request.QueryString["arg5"]);
notif.Text = Convert.ToString(Request.QueryString["arg6"]);
}
catch
{
Response.Redirect("authentication.aspx");
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}
This is how the Gridview looks like, It's empty and we should fill it and add this to the Database:
Upvotes: 0
Views: 1641
Reputation: 33839
(1). Set ShowHeaderWhenEmpty = "True"
ShowHeaderWhenEmpty
is to get or set a value that indicates whether the heading of a column in the GridView control is visible when the column has no data.
<asp:gridview ID="Gridview1" ShowHeaderWhenEmpty="True" ...
...
</asp:gridview>
(2). Check your Css Class tb
and make sure it is not set to hidden.
Not sure why calling DataBind()
method before the Page.PostBack
check
protected void Page_Load(object sender, EventArgs e)
{
//Gridview1.DataBind(); Remove it
if (!Page.IsPostBack)
{
SetInitialRow();
}
...
}
Upvotes: 1
Reputation: 1249
This kind of problem occurs when your database(Or tables that you are looking for) doesn't contain anyting. Try to add some data to your tables if that will not work remove your trycatch blocks and then you will see where is the problem
Upvotes: 1