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 = "  " + "<B>Branch Date:</B>" + " ";
tc.Controls.Add(lblbranchdate);
Label lblbranchdatedata = new Label();
lblbranchdatedata.ID = "lbltotnumbird1" + i.ToString();
lblbranchdatedata.Text = branchdate + "   ";
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>" + " ";
tc.Controls.Add(lbltotwt1);
Label lbltotwt1data = new Label();
lbltotwt1data.ID = "lbltotwt1data" + i.ToString();
lbltotwt1data.Text = totwt + "   ";
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>" + " ";
tc.Controls.Add(lbltotbirds);
Label lbltotbirddata = new Label();
lbltotbirddata.ID = "lbltotbirdata" + i.ToString();
lbltotbirddata.Text = totbirds + "   ";
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>" + " ";
tc.Controls.Add(lblmeatyield);
Label lblmeatyielddata = new Label();
lblmeatyielddata.ID = "lblmeatyied" + i.ToString();
lblmeatyielddata.Text = meatyield + "   ";
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>" + " ";
tc.Controls.Add(lblavgweight);
Label lblavgweightdata = new Label();
lblavgweightdata.ID = "lblavgweightdata" + i.ToString();
lblavgweightdata.Text = Avgweight + "   ";
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
Related
Getting old values in DataTable, I need new values which I updated. Please help me?
Update on button click
My code:
DataSet Ds = new DataSet();
string Query = "";
Query = "select Conv_0,Unit_0,Rate_0,Text_0,Barcode_0,Conv_1,Unit_1,Rate_1,Text_1,Barcode_1,Conv_2,Unit_2,Rate_2,Text_2,Barcode_2,Conv_3,Unit_3,Rate_3,Text_3,Barcode_3,Conv_4,Unit_4,Rate_4,Text_4,Barcode_4 from Prod_Unit where Code='" + a + "'";
Ds = SqlClass1.GetData_from_localhost(Query);
if (Ds.Tables[0].Rows.Count > 0)
{
dt.Columns.Clear();
dt.Rows.Clear();
dt.Columns.Add("NO");
dt.Columns.Add("UNITS");
dt.Columns.Add("CONVERSION");
dt.Columns.Add("BARCODE");
dt.Columns.Add("RATE 1");
dt.Columns.Add("RATE 2");
dt.Columns.Add("PRINT TEXT");
// dt.Rows.Add();
for (int i = 0; i < Ds.Tables[0].Columns.Count; i++)
{
if (i >= 0 && i <= 4)
{
dt.Rows.Add();
dt.Rows[i]["NO"] = i + 1;
string unit = Ds.Tables[0].Rows[0]["Unit_" + i + ""].ToString();
DataSet temp = SqlClass1.GetData_from_localhost("select Unit FROM Units where Code=" + unit + "");
if (temp.Tables[0].Rows.Count > 0)
{
dt.Rows[i]["UNITS"] = temp.Tables[0].Rows[0][0].ToString();
}
dt.Rows[i]["CONVERSION"] = Ds.Tables[0].Rows[0]["Conv_" + i + ""].ToString();
dt.Rows[i]["BARCODE"] = Ds.Tables[0].Rows[0]["Barcode_" + i + ""].ToString();
dt.Rows[i]["RATE 1"] = Ds.Tables[0].Rows[0]["Rate_" + i + ""].ToString();
dt.Rows[i]["RATE 2"] = Ds.Tables[0].Rows[0]["Rate_" + i + ""].ToString();
dt.Rows[i]["PRINT TEXT"] = Ds.Tables[0].Rows[0]["Text_" + i + ""].ToString();
}
}
if (i == 0)
{
// ViewState["CurrentTable"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
// SetPreviousData();
}
}
ViewState["CurrentTable"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
SetPreviousData();// function to get previous data in
textbox
This is my binding function that I call in postback. How can I get updated values in ViewState["CurrentTable"] = dt;
on Update Button
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("NO");
dt.Columns.Add("UNITS");
dt.Columns.Add("CONVERSION");
dt.Columns.Add("BARCODE");
dt.Columns.Add("RATE 1");
dt.Columns.Add("RATE 2");
dt.Columns.Add("PRINT TEXT");
dr = dt.NewRow();
dr["NO"] = 1;
dr["UNITS"] = string.Empty;
dr["CONVERSION"] = string.Empty;
dr["BARCODE"] = string.Empty;
dr["RATE 1"] = string.Empty;
dr["RATE 2"] = string.Empty;
dr["PRINT TEXT"] = string.Empty;
dt.Rows.Add(dr);
int rowIndex = 0;
for (int i = 0; i < GridView1.Rows.Count; i ++)
{
if (GridView1.Rows[rowIndex].RowType == DataControlRowType.DataRow)
{
DropDownList box1 = (DropDownList)GridView1.Rows[rowIndex].Cells[1].FindControl("DropDownList1");
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");
TextBox box6 = (TextBox)GridView1.Rows[rowIndex].Cells[6].FindControl("TextBox6");
dt.Rows[i]["NO"] = i + 1;
dt.Rows[i]["Units"] = box1.Text;
dt.Rows[i]["Conversion"] = box2.Text;
dt.Rows[i]["Barcode"] = box3.Text;
dt.Rows[i]["Rate 1"] = box4.Text;
dt.Rows[i]["Rate 2"] = box5.Text;
dt.Rows[i]["Print Text"] = box6.Text;
dt.Rows.Add();
rowIndex++;
}
}
ViewState["CurrentTable"] = dt;
Save();//function for save
}
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
I have an aspx page that contains a grid-view. The data-source for the gird-view is a datatable.
I want the last column in the grid view to contain a list of hyperlinks. I cannot figure out how to store or display this data
private void retrieveGroups()
{
// UserTab is a DataTable
// UserGrid is a GridView
UserTab.Columns.Add("LoginName", typeof(string));
UserTab.Columns.Add("DisplayName", typeof(string));
UserTab.Columns.Add("Kind", typeof(string));
UserTab.Columns.Add("Count", typeof(string));
UserTab.Columns.Add("Managers", typeof(string));
UserGrid.DataSource = UserTab;
SqlCommand cmd = FmaDb.makeCmd(FmaDb.SubDB.FMA_CUSTOMER, "fetchGroupIds", CommandType.StoredProcedure,
delegate(SqlCommand inCmd) { inCmd.Parameters.Add("#siteid", SqlDbType.Int); });
List<int> groupIds = new List<int>();
FmaDb.runReadCmd(cmd, delegate(SqlCommand inCmd) { inCmd.Parameters["#siteid"].Value = int.Parse(LFList.SelectedItem.Value); }, null,
delegate(SqlDataReader qrdr) { groupIds.Add(FmaDb.getDbInt(qrdr, 0)); });
UserGrid.Visible = true;
HyperLinkField hf = new HyperLinkField();
BoundField bf = new BoundField();
hf.HeaderText = "Login";
hf.DataTextField = "LoginName";
hf.DataNavigateUrlFields = new String[1] { "login" };
hf.DataNavigateUrlFormatString = "~/fausage.aspx?cn=login&lg={0}&ad=" + ad + "&as=" + aStatus;
UserGrid.Columns.Add(hf);
bf = new BoundField();
bf.HeaderText = "Name";
bf.DataField = "DisplayName";
UserGrid.Columns.Add(bf);
bf = new BoundField();
bf.HeaderText = "Type";
bf.DataField = "Kind";
UserGrid.Columns.Add(bf);
bf = new BoundField();
bf.HeaderText = "# Members";
bf.DataField = "Count";
UserGrid.Columns.Add(bf);
bf = new BoundField();
bf.HeaderText = "Managers";
bf.DataField = "Managers";
UserGrid.Columns.Add(bf);
foreach (int gId in groupIds)
{
Advisor advGroup = new Advisor(gId);
List<HyperLink> linkList = new List<HyperLink>();
advGroup.fetch();
if ((advGroup.Kind == AdvisorKind.Branch) || (advGroup.Kind == AdvisorKind.Team))
{
List<AdvisorMembership> members = advGroup.MyMembers(null, false);
if (advGroup.Kind == AdvisorKind.Branch)
{
List<AdvisorMembership> mgrs = advGroup.MyMembers(null, true);
HyperLink link;
foreach (AdvisorMembership m in mgrs)
{
if (m.MemberCanLoginAsContainer)
{
link = new HyperLink();
link.Text = m.Member.LoginName;
link.NavigateUrl = "~/fausage.aspx?cn=login&lg=" + m.Member.LoginName + "&ad=" + ad + "&as=" + aStatus;
link.Target = "blank";
linkList.Add(link);
}
}
}
UserTab.Rows.Add(advGroup.LoginName, advGroup.DisplayName, advGroup.Kind.ToString(), members.Count.ToString(), linkList);
}
ViewState["DataTable"] = UserTab;
}
}
}
}
When I click on the button just nothing happens!
public void cmdButton1_OnClick(object sender, EventArgs e)
{
Panel myPanel = new Panel();
myPanel.ID = "Panel1";
for (int i = 0; i < numtourist; i++)
{
Label myLabel = new Label();
myLabel.ID = "lblNameL" + i.ToString();
myLabel.Text = "Трите имена на латиница ";
TextBox myTextBox1 = new TextBox();
myTextBox1.ID = "txtNameL" + i.ToString();
myPanel.Controls.Add(myLabel);
myPanel.Controls.Add(myTextBox1);
Label mylabel2 = new Label();
mylabel2.ID = "lblNameK" + i.ToString();
mylabel2.Text = "Трите имена на кирилица";
TextBox myTextBox2 = new TextBox();
myTextBox2.ID = "txtNameK" + i.ToString();
myPanel.Controls.Add(mylabel2);
myPanel.Controls.Add(myTextBox2);
Label myLabel3 = new Label();
myLabel3.ID = "lblEGN" + i.ToString();
myLabel3.Text = "EГН";
TextBox myTextBox3 = new TextBox();
myTextBox3.ID = "txtEGN" + i.ToString();
myPanel.Controls.Add(myLabel3);
myPanel.Controls.Add(myTextBox3);
Label myLabel4 = new Label();
myLabel4.ID = "lblPersonalCardNum" + i.ToString();
myLabel4.Text = "Номер на лична карта";
TextBox myTextBox4 = new TextBox();
myTextBox4.ID = "txtPersonalCardNum" + i.ToString();
myPanel.Controls.Add(myLabel4);
myPanel.Controls.Add(myTextBox4);
Label myLabel5 = new Label();
myLabel5.ID = "lblDateOfIssuePC " + i.ToString();
myLabel5.Text = "Дата на издаане на лична карта:";
TextBox myTextBox5 = new TextBox();
myTextBox5.ID = "txtDateOfIssuePC" + i.ToString();
myPanel.Controls.Add(myLabel5);
myPanel.Controls.Add(myTextBox5);
Label myLabel6 = new Label();
myLabel6.ID = "lblDateOfExpiryPC " + i.ToString();
myLabel6.Text = "Дата на валидност на лична карта:";
TextBox myTextBox6 = new TextBox();
myTextBox6.ID = "txtDateOfExpiryPC" + i.ToString();
myPanel.Controls.Add(myLabel6);
myPanel.Controls.Add(myTextBox6);
Label mylabel6_1 = new Label();
mylabel6_1.ID = "lblIssuedFrom" + i.ToString();
mylabel6_1.Text = "Издадеа от";
TextBox myTextBox6_1 = new TextBox();
myTextBox6_1.ID = "txtIssuedFrom" + i.ToString();
myPanel.Controls.Add(mylabel6_1);
myPanel.Controls.Add(myTextBox6_1);
Label myLabel7 = new Label();
myLabel7.ID = "lblDateOfIssuePass " + i.ToString();
myLabel7.Text = "Дата на издаване на международен паспорт:";
TextBox myTextBox7 = new TextBox();
myTextBox7.ID = "txtDateOfIssuePass" + i.ToString();
myPanel.Controls.Add(myLabel7);
myPanel.Controls.Add(myTextBox7);
Label myLabel7_1 = new Label();
myLabel7_1.ID = "lblPassportNum" + i.ToString();
myLabel7_1.Text = "Номер на паспорт:";
TextBox myTextBox7_1 = new TextBox();
myTextBox7_1.ID = "txtPassportNum" + i.ToString();
myPanel.Controls.Add(myLabel7_1);
myPanel.Controls.Add(myTextBox7_1);
Label myLabel8 = new Label();
myLabel8.ID = "lblDateOfExpiryPass " + i.ToString();
myLabel8.Text = "Дата на валидност на международен паспорт:";
TextBox myTextBox8 = new TextBox();
myTextBox8.ID = "txtDateOfExpiryPass" + i.ToString();
myPanel.Controls.Add(myLabel8);
myPanel.Controls.Add(myTextBox8);
Label myLabel9 = new Label();
myLabel9.ID = "lblHomeContact" + i.ToString();
myLabel9.Text = "Домашен телефон";
TextBox myTextBox9 = new TextBox();
myTextBox9.ID = "txtHomeContact" + i.ToString();
myPanel.Controls.Add(myLabel9);
myPanel.Controls.Add(myTextBox9);
Label myLabel10 = new Label();
myLabel10.ID = "lblMobContact" + i.ToString();
myLabel10.Text = "Мобилен телефон";
TextBox myTextBox10 = new TextBox();
myTextBox10.ID = "txtMobContact" + i.ToString();
myPanel.Controls.Add(myLabel10);
myPanel.Controls.Add(myTextBox10);
Label myLabel11 = new Label();
myLabel11.ID = "lblEmail" + i.ToString();
myLabel11.Text = "E-mail адрес";
TextBox myTextBox11 = new TextBox();
myTextBox11.ID = "txtEmail" + i.ToString();
myPanel.Controls.Add(myLabel11);
myPanel.Controls.Add(myTextBox11);
Label myLabel12 = new Label();
myLabel12.ID = "lblAddress" + i.ToString();
mylabel2.Text = "Адрес";
TextBox myTextBox12 = new TextBox();
myTextBox12.ID = "txbAddress" + i.ToString();
myPanel.Controls.Add(myLabel12);
myPanel.Controls.Add(myTextBox12);
}
}
You are missing one more step. Now add your panel control to another control container or at least your page control.
You have created a panel and addet the others controls on it,
but you forgot to add this created panel (myPanel) to your form.
Youre missing the following line of code:
myForm.Controls.Add(myPanel);
Change myForm to the name of your form.
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