hi all i am creating a table with controls when i enter value 1 in textbox and click submit button
1 table with controls should be created so if i enter value 2 in textbox and click on submit button
2 tables with controls should be displayed,,,,,up to this ok
when i am inserting this dynamic controls values to database then error coming like object reference not set to an instance of object
so below is the code where i am wrong plz suggest me
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class dynamic2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
int rowCount = Convert.ToInt32(TextBox1.Text);
Table tbldynamic = new Table();
//Table tblanother = new Table();
//Table tba = new Table();
//Table per = new Table();
//Table ing = new Table();
tbldynamic.ID = "table1";
//tblanother.ID = "table 2";
//tba.ID = "table3";
//per.ID = "table4";
//ing.ID = "table5";
TableCell tc = new TableCell();
TableRow tr1 = new TableRow();
TableCell tc1 = new TableCell();
TableRow tr2 = new TableRow();
TableCell tc2 = new TableCell();
TableRow tr3 = new TableRow();
TableCell tc3 = new TableCell();
TableRow tr4 = new TableRow();
TableCell tc4 = new TableCell();
TableRow tr5 = new TableRow();
for (int i = 0; i < rowCount; i++)
{
TextBox TxtBoxU = new TextBox();
TextBox TxtBoxE = new TextBox();
TextBox txt = new TextBox();
CheckBox chk = new CheckBox();
CheckBox chka = new CheckBox();
Label lblU = new Label();
Label lblE = new Label();
Label lbh = new Label();
Label lba = new Label();
Label grade = new Label();
DropDownList dd = new DropDownList();
ListItem[] items = new ListItem[3];
items[0] = new ListItem("A Grade", "1");
items[1] = new ListItem("B Grade", "2");
items[2] = new ListItem("C Grade", "3");
dd.Items.AddRange(items);
dd.DataBind();
TxtBoxU.ID = "TextBoxU" + i.ToString();
TxtBoxE.ID = "TextBoxE" + i.ToString();
txt.ID = "txt" + i.ToString();
chk.ID = "chk" + i.ToString();
chka.ID = "chka" + i.ToString();
lblU.ID = "LabelU" + i.ToString();
lblE.ID = "LabelE" + i.ToString();
lbh.ID = "label h" + i.ToString();
lba.ID = "labela" + i.ToString();
grade.ID = "labelb" + i.ToString();
dd.ID = "da" + i.ToString();
lblU.Text = "Children firstname: ";
Panel1.Controls.Add(lblU);
Panel1.Controls.Add(TxtBoxU);
lblE.Text = "children lastname : ";
Panel1.Controls.Add(lblE);
Panel1.Controls.Add(TxtBoxE);
lbh.Text = "Going to school?:";
Panel1.Controls.Add(lbh);
Panel1.Controls.Add(chk);
Panel1.Controls.Add(chka);
lba.Text = "school name:";
grade.Text = "Grade:";
Panel1.Controls.Add(lba);
Panel1.Controls.Add(txt);
Panel1.Controls.Add(grade);
Panel1.Controls.Add(dd);
tc.Controls.Add(lblU);
tc.Controls.Add(TxtBoxU);
chk.Text = "yes";
chka.Text = "no";
tc4.Controls.Add(grade);
tc4.Controls.Add(dd);
tr1.Cells.Add(tc);
tr1.Cells.Add(tc);
tc1.Controls.Add(lblU);
tc1.Controls.Add(TxtBoxU);
tc2.Controls.Add(lbh);
tc2.Controls.Add(chk);
tc2.Controls.Add(chka);
tc3.Controls.Add(lba);
tc3.Controls.Add(txt);
tr4.Cells.Add(tc3);
tr4.Cells.Add(tc3);
tr3.Cells.Add(tc2);
tr3.Cells.Add(tc2);
tr3.Cells.Add(tc2);
tr2.Cells.Add(tc1);
tr2.Cells.Add(tc1);
tr5.Cells.Add(tc4);
tr5.Cells.Add(tc4);
tbldynamic.Controls.Add(tr1);
tbldynamic.Controls.Add(tr1);
//tbldynamic.Controls.Add(tr2);
//tbldynamic.Controls.Add(tr2);
//tbldynamic.Controls.Add(tr3);
//tbldynamic.Controls.Add(tr3);
//tbldynamic.Controls.Add(tr3);
//tbldynamic.Controls.Add(tr4);
//tbldynamic.Controls.Add(tr4);
//tbldynamic.Controls.Add(tr5);
//tbldynamic.Controls.Add(tr5);
tbldynamic.Controls.Add(tr2);
tbldynamic.Controls.Add(tr2);
tbldynamic.Controls.Add(tr3);
tbldynamic.Controls.Add(tr3);
tbldynamic.Controls.Add(tr3);
tbldynamic.Controls.Add(tr4);
tbldynamic.Controls.Add(tr4);
tbldynamic.Controls.Add(tr5);
tbldynamic.Controls.Add(tr5);
Panel1.Controls.Add(tbldynamic);
//Panel1.Controls.Add(tblanother);
//Panel1.Controls.Add(tba);
//Panel1.Controls.Add(per);
//Panel1.Controls.Add(ing);
}
}
protected void Button2_Click(object sender, EventArgs e)
{
String str = string.Empty;
TextBox firstname = (TextBox)Panel1.FindControl("TxtBoxU");
// str = txtUserName.Text;
TextBox lastname = (TextBox)Panel1.FindControl("txtEmail");
CheckBox chkk = (CheckBox)Panel1.FindControl("chk");
TextBox txttt = (TextBox)Panel1.FindControl("txt");
DropDownList dda = (DropDownList)Panel1.FindControl("dd");
using (SqlConnection con = new SqlConnection("Data Source=HOME;Initial Catalog=login;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO studentdetails(sname,slname,gsch,ssname,sgr) VALUES(@sname,@slname,@gsch,@ssname,@sgr)", con))
{
con.Open();
cmd.Parameters.AddWithValue("@sname", firstname.Text);
cmd.Parameters.AddWithValue("@slname", lastname.Text);
cmd.Parameters.AddWithValue("@gsch", chkk.Text);
cmd.Parameters.AddWithValue("@ssname", txttt.Text);
cmd.Parameters.AddWithValue("@sgr", dda.SelectedItem.Text);
cmd.ExecuteNonQuery();
firstname.Text = string.Empty;
lastname.Text = string.Empty;
chkk.Text = string.Empty;
txttt.Text = string.Empty;
dda.SelectedItem.Text = string.Empty;
//lastname.Text = string.Empty;
}
}
}
}