如何摆脱问题“未设置为对象实例的对象引用” [英] How to get rid of the problem"object reference not set to instance of an object"

查看:100
本文介绍了如何摆脱问题“未设置为对象实例的对象引用”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我正在创建一个带控件的表格,当我在文本框中输入值1并单击提交按钮时

1应该创建带有控件的表,如果我在文本框中输入值2并单击提交按钮

2个带控件的表格应该显示,,,,,直到这个确定

所以下面是我错了的代码plz建议我

使用System;

使用System.Collections。 Generic;

使用System.Linq;

使用System.Web;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用System.Data;

使用System.Data.SqlClient;



public partial class dynamic2:System.Web.UI.Page

{

protected void Page_Load(object sender,EventArgs e)

{



}

protected void Button1_Click(对象发送者,EventArgs e)

{



int rowCount = Convert.ToInt32(TextBox1.Text);

表tbldynamic = new Table();

//表tblanother = new 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 T ableRow();

TableCell tc4 = new TableCell();

TableRow tr5 = new TableRow();





for(int i = 0;我< rowCount时; i ++)

{





TextBox TxtBoxU = new TextBox();



TextBox TxtBoxE = new TextBox();

TextBox txt = new TextBox();

CheckBox chk = new CheckBox(); < br $>


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 =儿童名字:;

Panel1.Controls.Add(lblU);

Panel1.Controls.Add(TxtBoxU);

lblE.Text =children lastname:;

Panel1.Controls.Add(lblE);

Panel1.Controls.Add(TxtBoxE);

lbh.Text =去吧o学校?:;

Panel1.Controls.Add(lbh);

Panel1.Controls.Add(chk);

Panel1。 Controls.Add(chka);

lba.Text =学校名称:;

grade.Text =等级:;

Panel1.Controls.Add(lba);

Panel1.Controls.Add(txt);

Panel1.Controls.Add(等级);

Panel1.Controls.Add(dd);



tc.Controls.Add(lblU);

tc.Controls.Add(TxtBoxU );

chk.Text =是;

chka.Text =no;

tc4.Controls.Add(等级) ;

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.A dd(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.Paramete rs.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;

}

}

}

}

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;
}
}
}
}

推荐答案

这是我们遇到的最常见的问题之一,它也是我们最不具备的问题之一到了swer,但你最有能力回答自己。



让我解释一下错误的含义:你试图使用变量,属性或方法返回值但它包含null - 这意味着变量中没有类的实例。

它有点像一个口袋:你的衬衫里有一个口袋,用来拿笔子。如果你进入口袋并发现那里没有笔,你就不能在一张纸上签名 - 如果你尝试的话,你会得到非常有趣的外观!空口袋给你一个空值(这里没有笔!)所以你不能做任何你检索笔通常做的事情。它为什么空?这就是问题 - 可能是你今天早上离开家时忘了拿起你的笔,或者你昨晚把它拿到昨天的衬衫口袋里时可能会把笔留下来。



我们无法分辨,因为我们不在那里,更重要的是,我们甚至看不到你的衬衫,更不用说口袋里的东西了!



回到计算机,你做了同样的事情,不知何故 - 我们看不到你的代码,更不用说运行它了,找不到包含null的东西。

但是你可以 - 而Visual Studio将在这里帮助你。在调试器中运行您的程序,当它失败时,VS将向您显示它发现问题的行。然后,您可以开始查看它的各个部分,以查看哪个值为null,并开始回顾代码以找出原因。因此,在包含错误行的方法的开头放置一个断点,然后从头再次运行程序。这一次,VS会在错误发生前停止,让你通过查看代码来查看你的价值来检查发生了什么。



但我们做不到那 - 我们没有您的代码,如果我们拥有它,我们不知道如何使用它,我们没有您的数据。所以试试吧 - 看看你能找到多少信息!
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!


这篇关于如何摆脱问题“未设置为对象实例的对象引用”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆