Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
}
}
}
}
Posted

1 solution

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900