Object reference not set to an instance of an object-exception - asp.net

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

Related

Placing DataBound Drop Down List into TableCell

I have a bound drop down list that I would like to place into a Table Cell. I am used to Labels and Text Boxes but I cannot seem to get the syntax working on this one. This code is inside of a Webmethod and my output has to be just the html. Thank you.
[System.Web.Services.WebMethod]
public static string gatherSurchargeData(string PriceListItemID, string ProdID, string ColorCode)
{
DataTable GetProductSizes = new DataTable();
GetProductSizes = DataLayer.PricingToolDL.getProductSizes(ProdID, ColorCode);
DataTable dt = new DataTable();
dt = DataLayer.PricingToolDL.getScharge(PriceListItemID);
Table tblScharges = new Table();
tblScharges.ID = "tblScharges";
TableHeaderRow th = new TableHeaderRow();
TableHeaderCell thSizeScharge = new TableHeaderCell();
thSizeScharge.Text = "Size";
th.Cells.Add(thSizeScharge);
tblScharges.Rows.Add(th);
int i = 0;
while (i <= dt.Rows.Count - 1)
{
TableRow tr = new TableRow();
tr.ID = "tableTr" + i;
TableCell tcSizeScharge = new TableCell();
DropDownList ddl = new DropDownList();
ddl.DataSource = GetProductSizes;
ddl.DataTextField = "FitSize";
ddl.DataValueField = "FitSize";
ddl.DataBind();
//string dtMovexSKU = dt.Rows[i]["MovexSKU"].ToString();
//DataRow[] GetProductSizesMovexSKU = GetProductSizes.Select("MovexSKU Like'" + dtMovexSKU + "'");
//tcSizeScharge.ID = "tcSizeScharge" + i;
//tcSizeScharge.Text = GetProductSizesMovexSKU[0][1].ToString();
tr.Cells.Add(tcSizeScharge);
tblScharges.Rows.Add(tr);
i++;
}
string html = "";
using (StringWriter sw = new StringWriter())
{
tblScharges.RenderControl(new HtmlTextWriter(sw));
html = sw.ToString();
}
return html;
}
The commented lines would be the code I would use if I were only wanting text to appear if that helps.
You need to add control in tablecell like this
tcSizeScharge.Controls.Add(ddl);

Can't get value from dynamically created control in dynamically created foter row in grid view

I dynamically created footer row in row_created event and add dynamically created controls on button click which also dynamically created in footer I want to save data in linq table But dosen't works
here is my code I really need help
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
GridViewRow foter = new GridViewRow(0, 0, DataControlRowType.Footer, DataControlRowState.Insert);
GridViewRow foter1 = new GridViewRow(0, 0, DataControlRowType.Footer, DataControlRowState.Insert);
GridViewRow foter2 = new GridViewRow(0, 0, DataControlRowType.Footer, DataControlRowState.Insert);
GridViewRow foter3 = new GridViewRow(0, 0, DataControlRowType.Footer, DataControlRowState.Insert);
TableCell span = new TableCell();
span.Attributes.Add("rowspan", "3");
span.Attributes.Add("width", "180px");
FileUpload f = new FileUpload();
f.ID = "flupImage";
f.Attributes.Add("onchange", "ShowpImagePreview(this);");
Image img = new Image();
img.ID = "ImgPrv";
img.ClientIDMode = System.Web.UI.ClientIDMode.Static;
img.Attributes.Add("Height", "60px");
img.Attributes.Add("Width", "60px");
span.Controls.Add(img);
span.Controls.Add(new LiteralControl("<br />"));
span.Controls.Add(f);
span.HorizontalAlign = HorizontalAlign.Left;
TableCell celf = new TableCell();
Label l1 = new Label();
l1.Text = "ID ";
TextBox t1 = new TextBox();
t1.ID = "id";
celf.Controls.Add(l1);
celf.Controls.Add(t1);
celf.ColumnSpan = 4;
celf.HorizontalAlign = HorizontalAlign.Right;
TableCell celf2 = new TableCell();
Label l2 = new Label();
l2.Text = "naziv";
TextBox t2 = new TextBox();
t2.ID = "naziv";
celf2.Controls.Add(l2);
celf2.Controls.Add(t2);
celf2.ColumnSpan = 3;
celf2.HorizontalAlign = HorizontalAlign.Right;
TableCell celf3 = new TableCell();
Label l3 = new Label();
l3.Text = "opis";
TextBox t3 = new TextBox();
t3.ID = "opis";
celf3.Controls.Add(l3);
celf3.Controls.Add(t3);
celf3.ColumnSpan = 4;
celf3.HorizontalAlign = HorizontalAlign.Right;
TableCell celf4 = new TableCell();
Label l4 = new Label();
l4.Text = "cena";
TextBox t4 = new TextBox();
t4.ID = "cena";
celf4.Controls.Add(l4);
celf4.Controls.Add(t4);
celf4.ColumnSpan = 3;
celf4.HorizontalAlign = HorizontalAlign.Right;
TableCell celf5 = new TableCell();
Label l6 = new Label();
l6.Text = "kategorija";
TextBox t6 = new TextBox();
t6.ID = "kategorija";
celf5.Controls.Add(l6);
celf5.Controls.Add(t6);
celf5.ColumnSpan = 7;
celf5.HorizontalAlign = HorizontalAlign.Right;
TableCell celf6 = new TableCell();
Button Button1 = new Button();
Button1.Text = "ubaci nov";
celf6.Controls.Add(Button1);
Button1.Click += new System.EventHandler(Button1_Click);
celf6.ColumnSpan = 7;
celf6.HorizontalAlign = HorizontalAlign.Center;
foter.Cells.Add(span);
foter.Cells.Add(celf);
foter.Cells.Add(celf2);
foter1.Cells.Add(celf3);
foter1.Cells.Add(celf4);
foter2.Cells.Add(celf5);
foter3.Cells.Add(celf6);
GridView1.Controls[0].Controls.Add(foter);
GridView1.Controls[0].Controls.Add(foter1);
GridView1.Controls[0].Controls.Add(foter2);
GridView1.Controls[0].Controls.Add(foter3);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DataClasses1DataContext data = new DataClasses1DataContext();
Proizvodi video = new Proizvodi();
FileUpload file = (GridView1.FooterRow.FindControl("flupImage") as FileUpload);
byte[] fileByte = file.FileBytes;
Binary bin = new Binary(fileByte);
video.slika = bin;
string prID = (GridView1.FooterRow.FindControl("id") as TextBox).Text;
int proID = Convert.ToInt32(prID);
video.proizvodID = proID;
string naziv = (GridView1.FooterRow.FindControl("naziv") as TextBox).Text;
video.naziv = naziv;
string opis = (GridView1.FooterRow.FindControl("opis") as TextBox).Text;
video.opis = opis;
decimal cena=Convert.ToDecimal( (GridView1.FooterRow.FindControl("cena") as TextBox).Text);
video.cena = cena;
string katId = (GridView1.FooterRow.FindControl("kategorija") as TextBox).Text;
video.kategorijaID = Convert.ToInt32(katId);
data.Proizvodis.InsertOnSubmit(video);
data.SubmitChanges();
GridView1.DataBind();
}
I try some solution from net but dosen't work.
please help

Sorting table when used with literalControl ASP.net

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"...

how to add button in Dynamic gridview?

public void BuildMeatTally()
{
try
{
DataTable dtdate = new DataTable();
objRetailPL.status = 4;
dtdate = objRetailBAL.GetTypeNew(objRetailPL);
if (dtdate.Rows.Count > 0)
{
for (int i = 0; i < dtdate.Rows.Count; i++)
{
string branchdate;
int totbirds; float totwt, meatyield, Avgweight;
DateTime datewr;
DataTable dtdatedetailsTable = new DataTable();
objRetailPL.branchdate = dtdate.Rows[i]["BranchDate"].ToString();
objRetailPL.sno = Convert.ToInt32(dtdate.Rows[i]["sno"].ToString());
dtdatedetailsTable = objRetailBAL.getbradisdatewisedet(objRetailPL);
branchdate = dtdatedetailsTable.Rows[0]["BranchDate"].ToString();
totbirds = Convert.ToInt32(dtdatedetailsTable.Rows[0]["numofbirds"].ToString());
totwt = Convert.ToSingle(dtdatedetailsTable.Rows[0]["totalweght"].ToString());
meatyield = Convert.ToSingle(dtdatedetailsTable.Rows[0]["totalmeatyeild"].ToString());
Avgweight = Convert.ToSingle(dtdatedetailsTable.Rows[0]["AvgWeight"].ToString());
Table tbldynamic = new Table();
tbldynamic.BorderStyle = BorderStyle.Ridge;
tbldynamic.Width = 600;
TableCell tc = new TableCell();
TableRow tr = new TableRow();
Label lblbranchdate = new Label();
lblbranchdate.ID = "lblbrandate" + i.ToString();
lblbranchdate.Text = "&nbsp&nbsp" + "<B>Branch Date:</B>" + "&nbsp";
tc.Controls.Add(lblbranchdate);
Label lblbranchdatedata = new Label();
lblbranchdatedata.ID = "lbltotnumbird1" + i.ToString();
lblbranchdatedata.Text = branchdate + "&nbsp&nbsp&nbsp";
tc.Controls.Add(lblbranchdatedata);
tr.Cells.Add(tc);
tbldynamic.Rows.Add(tr);
Label lbltotwt1 = new Label();
lbltotwt1.ID = "lbltotwt1" + i.ToString();
lbltotwt1.Text = "<B>Total Weight:</B>" + "&nbsp";
tc.Controls.Add(lbltotwt1);
Label lbltotwt1data = new Label();
lbltotwt1data.ID = "lbltotwt1data" + i.ToString();
lbltotwt1data.Text = totwt + "&nbsp&nbsp&nbsp";
tc.Controls.Add(lbltotwt1data);
tr.Cells.Add(tc);
tbldynamic.Rows.Add(tr);
tc = new TableCell();
tr = new TableRow();
Label lbltotbirds = new Label();
lbltotbirds.ID = "lbltotbird" + i.ToString();
lbltotbirds.Text = "<B>Total Birds:</B>" + "&nbsp";
tc.Controls.Add(lbltotbirds);
Label lbltotbirddata = new Label();
lbltotbirddata.ID = "lbltotbirdata" + i.ToString();
lbltotbirddata.Text = totbirds + "&nbsp&nbsp&nbsp";
tc.Controls.Add(lbltotbirddata);
tr.Cells.Add(tc);
tbldynamic.Rows.Add(tr);
Label lblmeatyield = new Label();
lblmeatyield.ID = "lblmeatyield" + i.ToString();
lblmeatyield.Text = "<B>Meat Yield in Kgs:</B>" + "&nbsp";
tc.Controls.Add(lblmeatyield);
Label lblmeatyielddata = new Label();
lblmeatyielddata.ID = "lblmeatyied" + i.ToString();
lblmeatyielddata.Text = meatyield + "&nbsp&nbsp&nbsp";
tc.Controls.Add(lblmeatyielddata);
tr.Cells.Add(tc);
tbldynamic.Rows.Add(tr);
Label lblavgweight = new Label();
lblavgweight.ID = "lblavgweight" + i.ToString();
lblavgweight.Text = "<B>Avg Weight:</B>" + "&nbsp";
tc.Controls.Add(lblavgweight);
Label lblavgweightdata = new Label();
lblavgweightdata.ID = "lblavgweightdata" + i.ToString();
lblavgweightdata.Text = Avgweight + "&nbsp&nbsp&nbsp";
tc.Controls.Add(lblavgweightdata);
tr.Cells.Add(tc);
tbldynamic.Rows.Add(tr);
DataTable dtdcDetails = new DataTable();
objRetailPL.sno = Convert.ToInt32(dtdate.Rows[i]["sno"].ToString());
dtdcDetails = objRetailBAL.dtdateTallyTable(objRetailPL);
GridView gv = new GridView();
//gv.ID = "gv" + dtdate.Rows[i]["BranchDate"].ToString();
gv.Width = 600;
Label lblTotal = new Label();
Label lbltotaleggs = new Label();
BoundField partyname = new BoundField();
partyname.HeaderText = "PartyName";
partyname.DataField = "partyname";
gv.Columns.Add(partyname);
BoundField birdtype = new BoundField();
birdtype.HeaderText = "Bird Type";
birdtype.DataField = "birdname";
gv.Columns.Add(birdtype);
BoundField totweight = new BoundField();
totweight.HeaderText = "Total Weight";
totweight.DataField = "totalweight";
gv.Columns.Add(totweight);
BoundField rateperkg = new BoundField();
rateperkg.HeaderText = "Rate/Kg";
rateperkg.DataField = "rateperkg";
gv.Columns.Add(rateperkg);
BoundField Bdcno = new BoundField();
Bdcno.HeaderText = "DCNo";
Bdcno.DataField = "dcno";
gv.Columns.Add(Bdcno);
// Here I want to add Button(confirm) In dynamic Gridview..
gv.AutoGenerateColumns = false;
gv.ShowFooter = true;
gv.DataSource = dtdcDetails;
gv.DataBind();
}
}
}
how can i add button control in Dynamic gridview , i tried with Templeate field, but i am not getting exactly output...after how can i add Button Click event for it? please help me?
Thanks in advance...
Sambasiva
You cannot add control dynamically in GridView Control,
You need to create a class and extends it ITemplate.
Following links will explain it
http://www.codedigest.com/Articles/ASPNET/168_Create_Dynamic_GridView_Control_in_C_ASPNet.aspx
http://www.codeproject.com/Articles/13462/How-to-create-template-columns-dynamically-in-a-gr

Controls in DataGrid WPF with dynamic columns

I need to put a stackpanel inside the table cell but if i did my logic like this....
DataGrid dg = new DataGrid();
StackPanel sp = new StackPanel();
sp.Height = 18;
sp.Width = 60;
sp.Orientation = Orientation.Horizontal;
Button btn = new Button();
btn.Width = 10;
btn.Height = 10;
sp.Children.Add(btn);
Label bt = new Label();
bt.Content = "test";
bt.Margin = new Thickness(0, -3, 0, 0);
sp.Children.Add(bt);
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("hello");
dt.Columns.Add(dc);
DataRow dr = dt.NewRow();
dr[0] = sp; dt.Rows.Add(dr);
dg.ItemsSource = dt.DefaultView;
l1.Content = dg;
it is showing "System.Windows.Controls.StackPanel" in the cell..... how to get a StackPanel(with multiple Controls) in side the GridView cell
You should create custom template for your grid cells. See this.

Resources