I create a PDF file using itextsharp. It's created successfully and open with adobe reader 9 not in adobe reader 7 and 8. Please help me to fix this error.
This is my partial code :
try
{
//yourFont = BaseFont.CreateFont(Application.StartupPath + "/verdana.TTF", BaseFont.WINANSI, BaseFont.EMBEDDED);
pgSize = new iTextSharp.text.Rectangle(320, 455);
doc = new Document(pgSize, 15, 5, 12, 4);
fnt = new iTextSharp.text.Font(yourFont, 7, 3);
fnt1 = new iTextSharp.text.Font(yourFont, 5, 0);
fnt2 = new iTextSharp.text.Font(yourFont, 3, 2);
fnt3 = new iTextSharp.text.Font(yourFont, 4, 6);
PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("Payslip.pdf"), FileMode.Create));
//PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("Payslip.pdf"), FileMode.Create)); //Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Payslip.pdf"
doc.Open();
DataView DView = (DataView)Session["data_value"];
dtData = DView.ToTable();
dr = dtData.Select("fldemp_no='" + Session["EmployeeID"].ToString() + "'");
doc.NewPage();
iTextSharp.text.Image ObjImg = iTextSharp.text.Image.GetInstance(Server.MapPath("~/Bin/Head.png"));
ObjImg.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
ObjImg.ScaleToFit(220f, 150f);
ObjImg.SpacingBefore = 13f;
ObjImg.SpacingAfter = 1f;
doc.Add(ObjImg);
maintable = new PdfPTable(1);
cell = new PdfPCell(new Phrase("Pay Slip for the month of " + dr[0]["fldmonth"].ToString(), fnt1));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.Border = 0;
maintable.AddCell(cell);
doc.Add(maintable);
maintable = new PdfPTable(2);
empdetright = new PdfPTable(2);
empdetleft = new PdfPTable(2);
cell = new PdfPCell(new Phrase("Emp No", fnt1));
cell.Border = 0;
empdetright.AddCell(cell);
cell = new PdfPCell(new Phrase(": " + dr[0]["fldemp_no"].ToString(), fnt1));
cell.Border = 0;
empdetright.AddCell(cell);
cell = new PdfPCell(new Phrase("Emp Name", fnt1));
cell.Border = 0;
empdetright.AddCell(cell);
cell = new PdfPCell(new Phrase(": " + dr[0]["fldempname"].ToString(), fnt1));
cell.Border = 0;
empdetright.AddCell(cell);
.......
doc.Close();
Process.Start(Server.MapPath("Payslip.pdf"));
The above code is run on local machine not on server. Please help me to fix this error..
Process.Start will attempt to open the file in the computer it is running (the server).
This is unlikely to be what you want. You should be uploading the file to the browser and let it decide (using Response.WriteFile, for instance).
Related
Good Day, guys.
I'm currentl making a full page overlay for navigation and I've been trying to figure out this problem for the past 2 days regarding my nested foreach() loops.
I have these pages which may be linked with posts. and here is what I'm trying to achieve:
Home Page
Technologies Page
Post 1/ Post 2/ Post3
Contact Us Page
About Us Page
But what I'm getting is:
Home Page
Technologies Page
Post 1/ Post 2/ Post3
Contact Us Page
Post 1/ Post 2/ Post3
About Us Page
Post 1/ Post 2/ Post3
Here are my code:
public DataTable GetMain()
{
string sql = "SELECT Id,Title,Slug from Pages";
SqlCommand SQLComm = new SqlCommand(sql, con);
SqlDataAdapter SQLAd = new SqlDataAdapter(SQLComm);
DataTable dt = new DataTable();
SQLAd.Fill(dt);
return dt;
}
private void GetAll()
{
DataTable dt = new DataTable();
HyperLink newhyperlink = new HyperLink();
Label newlabel = new Label();
foreach (DataRow row in GetMain().Rows)
{
newlabel.ID = "li";
MainMenu.Controls.Add(newlabel);
newlabel.Text = #"<li class""dropdown"" data-toggle=""collapse""";
newlabel = new Label();
newhyperlink.ID = "Main";
MainMenu.Controls.Add(newhyperlink);
newhyperlink.Text = row["Title"].ToString();
newhyperlink.NavigateUrl = row["Slug"].ToString();
newhyperlink = new HyperLink();
html.Append("></li>");
html.Append("<ul class=\"list-group\">");
var sql2 = "SELECT po.Id as PostId, po.Slug as PostSlug, po.Title as PostTitle, pa.Slug as PageSlug, pa.Id as PageId FROM Posts po " +
"LEFT JOIN PagesPostsMap m ON po.Id = m.PostId " +
"LEFT JOIN Pages pa ON m.PageId = pa.Id " +
"WHERE pa.Id = "+row["Id"] + "";
SqlDataAdapter SQLAd = new SqlDataAdapter(sql2, con);
SQLAd.Fill(dt2);
foreach (DataRow row2 in dt2.Rows)
{
object value = row2["PostId"];
if (value != DBNull.Value)
{
if (row["Id"] != row2["PageId"])
{
newlabel.ID = "li";
MainMenu.Controls.Add(newlabel);
newlabel.Text = #"<li class""dropdown-menu""";
newlabel = new Label();
newhyperlink.ID = "Sub" + i.ToString();
MainMenu.Controls.Add(newhyperlink);
newhyperlink.Text = row2["PostTitle"].ToString();
newhyperlink.NavigateUrl = row2["PageSlug"].ToString() + "/" + row2["PostSlug"].ToString();
newhyperlink = new HyperLink();
newlabel.ID = "li";
MainMenu.Controls.Add(newlabel);
newlabel.Text = "</li>";
newlabel = new Label();
}
}
}
}
}
I'm sorry if the the classes and stuff are not adding up. I'm still trying to figure out how css and JS works.
And thank for helping me.
Your code is actually just doing what you tell it to do. For every row in your GetMain, you order everything in your dt2 (datatable2). If you want what you try to achive, you have to set a condition and put your second foreach into that condition.
if(tech parts row)
{
"your second foreach"
}
If I missunderstand anything, feel free to add more info
public DataTable GetMain()
{
string sql = "SELECT Id,Title,Slug from Pages";
SqlCommand SQLComm = new SqlCommand(sql, con);
SqlDataAdapter SQLAd = new SqlDataAdapter(SQLComm);
DataTable dt = new DataTable();
SQLAd.Fill(dt);
return dt;
}
private void GetAll()
{
DataTable dt = new DataTable();
HyperLink newhyperlink = new HyperLink();
Label newlabel = new Label();
foreach (DataRow row in GetMain().Rows)
{
newlabel.ID = "li";
MainMenu.Controls.Add(newlabel);
newlabel.Text = #"<li class""dropdown"" data-toggle=""collapse""";
newlabel = new Label();
newhyperlink.ID = "Main";
MainMenu.Controls.Add(newhyperlink);
newhyperlink.Text = row["Title"].ToString();
newhyperlink.NavigateUrl = row["Slug"].ToString();
newhyperlink = new HyperLink();
html.Append("></li>");
html.Append("<ul class=\"list-group\">");
var sql2 = "SELECT po.Id as PostId, po.Slug as PostSlug, po.Title as PostTitle, pa.Slug as PageSlug, pa.Id as PageId FROM Posts po " +
"JOIN PagesPostsMap m ON po.Id = m.PostId " +
"JOIN Pages pa ON m.PageId = pa.Id " +
"WHERE pa.Id = "+row["Id"] + "";
SqlDataAdapter SQLAd = new SqlDataAdapter(sql2, con);
SQLAd.Fill(dt2);
foreach (DataRow row2 in dt2.Rows)
{
newlabel.ID = "li";
MainMenu.Controls.Add(newlabel);
newlabel.Text = #"<li class""dropdown-menu""";
newlabel = new Label();
newhyperlink.ID = "Sub" + i.ToString();
MainMenu.Controls.Add(newhyperlink);
newhyperlink.Text = row2["PostTitle"].ToString();
newhyperlink.NavigateUrl = row2["PageSlug"].ToString() + "/" + row2["PostSlug"].ToString();
newhyperlink = new HyperLink();
newlabel.ID = "li";
MainMenu.Controls.Add(newlabel);
newlabel.Text = "</li>";
newlabel = new Label();
}
}
}
I have a problem sorting this table. This was just a project that was pass unto me, and they want me to make some changes with the codes. Here's the code used to create table with data.
DataTable dt = common.get_entry(sname);
if (dt.Rows.Count != 0)
{
error_msg.Visible = false;
loader4.Visible = true;
}
else
{
loader4.Visible = false;
error_msg.Visible = true;
}
int maxsize = 10;
int rpage = Convert.ToInt32(p);
int startrow = ((rpage - 1) * maxsize) + 1;
int endrow = (rpage * maxsize);
if (dt.Rows.Count > 0)
{
int rcnt = dt.Rows.Count;
var tbl = view_tbl;
view_tbl.CssClass = "table table-hover table-bordered";
var maxpage = rcnt / maxsize;
var excess = rcnt % maxsize;
if (excess > 0) maxpage++;
TableHeaderRow r1 = new TableHeaderRow();
r1.TableSection = TableRowSection.TableHeader;
TableHeaderCell c2 = new TableHeaderCell();
c2.Controls.Add(new LiteralControl(""));
c2.Width = 45;
r1.Cells.Add(c2);
TableHeaderCell c8 = new TableHeaderCell();
c8.Controls.Add(new LiteralControl("Store Code"));
c8.Width = 120;
r1.Cells.Add(c8);
TableHeaderCell c3 = new TableHeaderCell();
c3.Controls.Add(new LiteralControl("Store Name"));
c3.Width = 250;
r1.Cells.Add(c3);
TableHeaderCell c4 = new TableHeaderCell();
c4.Controls.Add(new LiteralControl("Line of Business"));
c4.Width = 150;
r1.Cells.Add(c4);
TableHeaderCell c5 = new TableHeaderCell();
c5.Controls.Add(new LiteralControl("Visitors Name"));
c5.Width = 170;
r1.Cells.Add(c5);
TableHeaderCell c7 = new TableHeaderCell();
c7.Controls.Add(new LiteralControl("Date of Visit"));
c7.Width = 100;
r1.Cells.Add(c7);
TableHeaderCell c9 = new TableHeaderCell();
c9.Controls.Add(new LiteralControl("Added Date"));
c9.Width = 170;
r1.Cells.Add(c9);
TableHeaderCell c6 = new TableHeaderCell();
c6.Controls.Add(new LiteralControl("Action"));
c6.Width = 100;
r1.Cells.Add(c6);
tbl.Rows.Add(r1);
for (int cnt = startrow - 1; cnt < rcnt && cnt < endrow; cnt++)
{
TableRow r = new TableRow();
String sn = dt.Rows[cnt][3].ToString();
String lo = dt.Rows[cnt][5].ToString();
String id = dt.Rows[cnt][0].ToString();
String vn = dt.Rows[cnt][4].ToString();
String ed = dt.Rows[cnt][1].ToString();
String da = dt.Rows[cnt][6].ToString();
String sc = dt.Rows[cnt][7].ToString();
string[] edd = new string[2];
edd = ed.Split(' ');
ed = edd[0] + " " + dt.Rows[cnt][2].ToString();
TableCell cc1 = new TableCell();
cc1.Controls.Add(new LiteralControl("<input class='check_box' rel='" + id + "'type='checkbox'> <a href='#'><i class='icon-star-empty'></i></a>"));
r.Cells.Add(cc1);
TableCell cc2 = new TableCell();
cc2.Controls.Add(new LiteralControl("<strong>" + sc + "</strong>"));
r.Cells.Add(cc2);
TableCell cc7 = new TableCell();
cc7.Controls.Add(new LiteralControl("<strong>" + sn + "</strong>"));
r.Cells.Add(cc7);
TableCell cc3 = new TableCell();
cc3.Controls.Add(new LiteralControl("<strong>" + lo + "</strong>"));
r.Cells.Add(cc3);
TableCell cc4 = new TableCell();
cc4.Controls.Add(new LiteralControl("<strong>" + vn + "</strong>"));
r.Cells.Add(cc4);
TableCell cc5 = new TableCell();
cc5.Controls.Add(new LiteralControl("<strong>" + ed + "</strong>"));
r.Cells.Add(cc5);
TableCell cc8 = new TableCell();
cc8.Controls.Add(new LiteralControl("<strong>" + da + "</strong>"));
r.Cells.Add(cc8);
TableCell cc6 = new TableCell();
cc6.Controls.Add(new LiteralControl("<a data-target='#viewentry' data-toggle='modal' class='btn btn-mini btn-success view_btn' rel='" + id + "'><i class='icon-th-list icon-white'></i> View</a> <a data-target='#delentry' data-toggle='modal' class='btn btn-mini btn-danger del_btn hidden' rel='" + id + "'><i class='icon-trash icon-white'></i></a>"));
r.Cells.Add(cc6);
tbl.Rows.Add(r);
}
var dpagi = pagi;
LiteralControl ul = new LiteralControl();
ul.Text = "<ul>";
for (int cnt5 = 1; cnt5 <= maxpage; cnt5++)
{
if (cnt5 == rpage)
ul.Text = ul.Text + "<li class='disabled'><a href='StoreVisitView.aspx?page=" + cnt5 + "'>" + cnt5 + "</a></li>";
else
ul.Text = ul.Text + "<li><a href='StoreVisitView.aspx?page=" + cnt5 + "' class='pgn'>" + cnt5 + "</a></li>";
}
ul.Text = ul.Text + "</ul>";
dpagi.Controls.Add(ul);
pagi.CssClass = "pagination";
loader4.Visible = false;
I have no idea how to sort this because the table was made thru literalcontrol and etc..
If you want to sort the data on server side I suggest using a DataView to sort it. After it's sorted you can render it manually as you do now (iterate through DataView's rows) or bind a DataGrid to it.
DataView view = new DataView(dt);
view.Sort = "Column ASC, Column2 ASC";
// Now iterate through rows of dataview
foreach (DataRowView row in view) {...}
Other option is to sort it on client side using some sorting plugin.
For example Tablesorter seems pretty easy to use. Or just google "sort table javascript"...
I create a PDF file using iTextSharp and it's stored in my system desktop and opens successfully in my local system. Now if I upload the same code to a server, no error occurs and the PDF file isn't created. This is my partial code to create and open pdf.
PdfWriter.GetInstance(doc, new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Out.pdf", FileMode.Create));
doc.Open();
DataView DView = (DataView)Session["data_value"];
dtData = DView.ToTable();
dr = dtData.Select("fldemp_no='" + Session["EmployeeID"].ToString() + "'");
doc.NewPage();
iTextSharp.text.Image ObjImg = iTextSharp.text.Image.GetInstance(Server.MapPath("~/Bin/Head.png"));
ObjImg.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
ObjImg.ScaleToFit(220f, 150f);
ObjImg.SpacingBefore = 13f;
ObjImg.SpacingAfter = 1f;
doc.Add(ObjImg);
maintable = new PdfPTable(1);
cell = new PdfPCell(new Phrase("Pay Slip for the month of " + dr[0]["fldmonth"].ToString(), fnt1));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.Border = 0;
maintable.AddCell(cell);
doc.Add(maintable);
maintable = new PdfPTable(2);
empdetright = new PdfPTable(2);
empdetleft = new PdfPTable(2);
cell = new PdfPCell(new Phrase("Emp No", fnt1));
cell.Border = 0;
empdetright.AddCell(cell);
cell = new PdfPCell(new Phrase(": " + dr[0]["fldemp_no"].ToString(), fnt1));
cell.Border = 0;
empdetright.AddCell(cell);
cell = new PdfPCell(new Phrase("Emp Name", fnt1));
cell.Border = 0;
empdetright.AddCell(cell);
cell = new PdfPCell(new Phrase(": " + dr[0]["fldempname"].ToString(), fnt1));
cell.Border = 0;
empdetright.AddCell(cell);
doc.Close();
Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/out.pdf");
In a web application you need to return the PDF document in the response stream. Here is a simple example:
var response = HttpContext.Current.Response;
response.Clear();
response.ContentType = "application/pdf";
MemoryStream mem = new MemoryStream(); // PDF data will be written here
PdfWriter writer = PdfWriter.GetInstance(doc, mem); // tie a PdfWriter instance to the stream
doc.Open();
// ... Doing the pdf generation
doc.Close();
// write the document data to response stream
writer.Flush();
response.OutputStream.Write(mem.GetBuffer(), 0, mem.GetBuffer().Length);
response.OutputStream.Flush();
response.OutputStream.Close();
response.End();
I've added an header and a footer on my document by PdfPageEventHelper.
The document has several "large" tables populated at runtime.
I've tried to add those tables simply by "documen.Add(table)", but my header and my footer results overwritten.
I've already tried both methods to add the tables (WriteSelectedRows and document.Add(myPdfPtable).
Here is the code of the PageEventHelper:
private class MyPageEventHandler : PdfPageEventHelper
{
public iTextSharp.text.Image ImageHeader { get; set; }
public iTextSharp.text.Image ImageFooter { get; set; }
public override void OnEndPage(PdfWriter writer, Document document)
{
var fontintestazione = FontFactory.GetFont("Verdana", 10, Font.BOLD, BaseColor.LIGHT_GRAY);
var fontRight = FontFactory.GetFont("Verdana", 8, Font.BOLD, BaseColor.WHITE);
var fontFooter = FontFactory.GetFont("Verdana", 6, Font.NORMAL, BaseColor.BLUE);
float cellHeight = document.TopMargin;
Rectangle page = document.PageSize;
PdfPTable head = new PdfPTable(3);
head.TotalWidth = page.Width;
PdfPCell c = new PdfPCell(ImageHeader, true);
c.HorizontalAlignment = Element.ALIGN_LEFT;
c.FixedHeight = cellHeight;
c.Border = PdfPCell.NO_BORDER;
head.AddCell(c);
c = new PdfPCell(new Phrase("somePhrase", fontintestazione));
c.Border = PdfPCell.NO_BORDER;
head.AddCell(c);
c = new PdfPCell(new Phrase("someTextBlah", fontRight));
c.Border = PdfPCell.NO_BORDER;
c.HorizontalAlignment = 1;
c.BackgroundColor = new BaseColor(70, 130, 180);
head.AddCell(c);
head.WriteSelectedRows(0, -1, 10, page.Height - cellHeight + head.TotalHeight -30, writer.DirectContent);
PdfPTable footer = new PdfPTable(2);
footer.TotalWidth = 316f;
float[] cfWidths = new float[] { 2f, 1f };
footer.SetWidths(cfWidths);
PdfPCell cf = new PdfPCell(ImageFooter, true);
cf.HorizontalAlignment = Element.ALIGN_RIGHT;
cf.FixedHeight = cellHeight;
cf.Border = PdfPCell.NO_BORDER;
footer.AddCell(cf);
cf = new PdfPCell(new Phrase("someEndingText", fontFooter));
cf.HorizontalAlignment = Element.ALIGN_LEFT;
cf.Border = PdfPCell.NO_BORDER;
footer.AddCell(cf);
footer.WriteSelectedRows(0, -1, 10, 50, writer.DirectContent);
}
On my page, i simply do:
var document = new Document(PageSize.A4);
var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, output);
iTextSharp.text.Image imageHeader = iTextSharp.text.Image.GetInstance(Server.MapPath("/images/header.ong"));
iTextSharp.text.Image imageFooter = iTextSharp.text.Image.GetInstance(Server.MapPath("/images/footer.png"));
MyPageEventHandler eve = new MyPageEventHandler
{
ImageHeader = imageHeader ,
ImageFooter = imageFooter
};
writer.PageEvent = eve;
document.Open();
//adding a table
PdfPTable cvTable = new PdfPtable(3);
cvTable.TotalWidth = document.PageSize.Width;
PdfPCell hCell = new PdfPCell(new Phrase("Jobs By User", aCustomFont));
cvTable.AddCell(hCell);
for(int i = 0; i < myTable.Records.Count; i++)
{
PdfPCell idCell = new PdfPCell(new Phrase(myTable.Records[i]._id, aFont));
cvTable.Add(idCell);
//same stuff for other fields of table
}
//first attempt.... failed:
document.Add(cvTable) //<- header and footer are overwritten by table
//second attempt..... failed too...
cvTable.WriteSelectedRows(0, -1, 10, myPoisition, writer.DirectContent);
//kind of fail...:
//the table is large and need more pages. It is trunked on the first page and overwrite
//the footer.
In your OnEndPage method you have this line:
head.WriteSelectedRows(0, -1, 10, page.Height - cellHeight + head.TotalHeight - 30, writer.DirectContent);
That code correctly calculates where to put content based on the page's height and top margin but also includes a magical 30 in there which is causing the header to be drawn on top of the table. Change it to this and your header will be fine.
head.WriteSelectedRows(0, -1, 10, page.Height - cellHeight + head.TotalHeight, writer.DirectContent);
I'm guessing that that 30 is trying to include some padding between your header and the table itself. What I would recommend is actually changing the document's margins themselves in the main code:
document.SetMargins(document.LeftMargin, document.RightMargin, document.TopMargin + 30, document.BottomMargin);
And then accounting for that in the OnEndPage method:
float cellHeight = document.TopMargin - 30;
Your footer code doesn't actually account for the bottom margin and just draws it at 50 so this will always overlap. The quick fix would be to change it to:
footer.WriteSelectedRows(0, -1, 10, footer.TotalHeight, writer.DirectContent);
This will at least get the footer bottom-aligned. If you want some more padding like above just adjust the document margins again:
document.SetMargins(document.LeftMargin, document.RightMargin, document.TopMargin + 30, document.BottomMargin + 30);
I have a panel with ID:Panel2 in my web page and I'm adding adynamic table into it.
I'm getting an "Object reference not set to an instance of an object" exception.
My code:
for (int i = 0; i <= val; i++)
{
Table Table4 = new Table();
TableHeaderRow thr = new TableHeaderRow();
TableHeaderCell thc2 = new TableHeaderCell();
thc2.BorderColor = Color.Black;
thc2.BorderWidth = 2;
TableHeaderCell thc3 = new TableHeaderCell();
thc3.BorderColor = Color.Black;
thc3.BorderWidth = 2;
TableHeaderCell thc4 = new TableHeaderCell();
thc4.BorderColor = Color.Black;
thc4.BorderWidth = 2;
TableHeaderCell thc5 = new TableHeaderCell();
thc5.BorderColor = Color.Black;
thc5.BorderWidth = 2;
Label l2 = new Label();
l2.Text = "Check Point";
l2.ForeColor = Color.Black;
Label l3 = new Label();
l3.Text = "Applicability";
l3.ForeColor = Color.Black;
Label l4 = new Label();
l4.Text = "Effectiveness";
l4.ForeColor = Color.Black;
Label l5 = new Label();
l5.Text = "Score";
l5.ForeColor = Color.Black;
thc2.Controls.Add(l2);
thc3.Controls.Add(l3);
thc4.Controls.Add(l4);
thc5.Controls.Add(l5);
thr.Cells.Add(thc2);
thr.Cells.Add(thc3);
thr.Cells.Add(thc4);
thr.Cells.Add(thc5);
Table4.Rows.Add(thr);
TableRow tr = new TableRow();
tr.BorderColor = Color.Black;
tr.BorderWidth = 2;
TableCell c2 = new TableCell();
c2.BorderColor = Color.Black;
c2.BorderWidth = 2;
c2.Text = Convert.ToString(s[i]);
TableCell c3 = new TableCell();
c3.BorderColor = Color.Black;
c3.BorderWidth = 2;
TableCell c4 = new TableCell();
c4.BorderColor = Color.Black;
c4.BorderWidth = 2;
TableCell c5 = new TableCell();
c5.BorderColor = Color.Black;
c5.BorderWidth = 2;
DropDownList ddl1 = new DropDownList();
ddl1.Items.AddRange(items2);
ddl1.ID = "ddl1" + i;
DropDownList ddl2 = new DropDownList();
ddl2.Items.AddRange(items1);
ddl2.ID = "ddl2" + i;
TextBox t4=new TextBox();
t4.ID = "textID4" + i;
t4.EnableViewState = true;
c2.ID = "newC2" + i;
c3.ID = "newC3" + i;
c4.ID = "newC4" + i;
c5.ID = "newC5" + i;
tr.ID = "newRow" + i;
c3.Controls.Add(ddl1);
c4.Controls.Add(ddl2);
c5.Controls.Add(t4);
tr.Cells.Add(c2);
tr.Cells.Add(c3);
tr.Cells.Add(c4);
tr.Cells.Add(c5);
Table4.Rows.Add(tr);
-----> this.Panel2.Controls.Add(Table4);
Session["table"] = Table4;
}
In line (this.Panel2.Controls.Add(Table4)), I'm getting
Object reference not set to an instance of an object-exception.
even though I have created a Panel in the design page I'm getting the error
Here any table is added dynamically not the Panel, Panel is created statically and I'm inserting dynamic table in it.
There are a couple of reasons this can happen, but the most important is:
Are you absolutely sure you have created a control with the exact id
with the runat attribute set to "server"?
This applies to both Panel2 and Table4.
Other than that you need to check these things:
Your class (where the exception is occuring) is inheriting from System.UI.Page
The #Page CodeBehind attribute is set to your class (spelling, capitalisation)
You're referencing the object at the correct time during the page lifecycle (i.e. before it's being initialised)
There's no where that the reference has been modified prior to use