未将对象引用设置到对象的实例??

ealzero 2011-03-15 04:30:25
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:


行 24: using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["CarManage"].ConnectionString))
行 25: {
行 26: using (SqlCommand command = new SqlCommand("insert into UserMessage(UserName,UPassword,UCode,UName,USex,UBir,UMZ,USFZ,UPhone,UBuMen,UAddress,UPerMissions) values('" + this.TextBox1.Text + "','" + this.TextBox2.Text + "','" + this.TextBox4.Text + "','" + this.TextBox9.Text + "'," + this.RadioButtonList1.SelectedValue + ",'" + birthday.Value + "','" + DropDownListNation.Items[this.DropDownListNation.SelectedIndex].Text + "','" + this.TextBox5.Text + "','" + this.TextBox6.Text + "','" + this.bumen.Items[this.DropDownListNation.SelectedIndex].Text + "','" + this.TextBox7.Text + "','" + RadioButtonList2.SelectedItem.Text + "')"))
行 27: {
行 28: connection.Open();//打开数据库连接


源文件: f:\CarManage\Register.aspx.cs 行: 26

//源文件
public partial class Register : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
using (SqlConnection connection = new SqlConnection

(ConfigurationManager.ConnectionStrings["CarManage"].ConnectionString))
{
using (SqlCommand command = new SqlCommand("insert into UserMessage

(UserName,UPassword,UCode,UName,USex,UBir,UMZ,USFZ,UPhone,UBuMen,UAddress,UPerMissio

ns) values('" + this.TextBox1.Text + "','" + this.TextBox2.Text + "','" +

this.TextBox4.Text + "','" + this.TextBox9.Text + "'," +

this.RadioButtonList1.SelectedValue + ",'" + birthday.Value + "','" +

DropDownListNation.Items[this.DropDownListNation.SelectedIndex].Text + "','" +

this.TextBox5.Text + "','" + this.TextBox6.Text + "','" + this.bumen.Items

[this.DropDownListNation.SelectedIndex].Text + "','" + this.TextBox7.Text + "','" +

RadioButtonList2.SelectedItem.Text + "')"))
{
connection.Open();
command.Connection = connection;
int result = (int)command.ExecuteNonQuery();
if (result != 0)
{
JScript.AlertAndRedirect("登录成功!", "Login.aspx");

}
else
{
JScript.AlertAndRedirect("登录失败,请重新输入您的用户名和密码!", "#");
}
}
}
}


乞求高手前来赐教
...全文
110 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
a312991915 2011-03-16
  • 打赏
  • 举报
回复
26行有的值为空,调试看看
hkssy 2011-03-16
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 wxr0323 的回复:]

检查 using (SqlCommand command = new SqlCommand("insert into UserMessage(UserName,UPassword,UCode,UName,USex,UBir,UMZ,USFZ,UPhone,UBuMen,UAddress,UPerMissions) values('" + this.TextBox1.Text + "','" + ……
[/Quote]

这样传值好像太不规范了吧,还很容易出错
子夜__ 2011-03-15
  • 打赏
  • 举报
回复
检查 using (SqlCommand command = new SqlCommand("insert into UserMessage(UserName,UPassword,UCode,UName,USex,UBir,UMZ,USFZ,UPhone,UBuMen,UAddress,UPerMissions) values('" + this.TextBox1.Text + "','" + this.TextBox2.Text + "','" + this.TextBox4.Text + "','" + this.TextBox9.Text + "'," + this.RadioButtonList1.SelectedValue + ",'" + birthday.Value + "','" + DropDownListNation.Items[this.DropDownListNation.SelectedIndex].Text + "','" + this.TextBox5.Text + "','" + this.TextBox6.Text + "','" + this.bumen.Items[this.DropDownListNation.SelectedIndex].Text + "','" + this.TextBox7.Text + "','" + RadioButtonList2.SelectedItem.Text + "')"))
里有没有空值 用断点。
haojuntu 2011-03-15
  • 打赏
  • 举报
回复
string con="ConfigurationManager.ConnectionStrings["CarManage"].ConnectionString)";
看一下连接获取到没,进行调试一下。
勇敢的心515 2011-03-15
  • 打赏
  • 举报
回复
对,要学会自己去调试..
jeje 2011-03-15
  • 打赏
  • 举报
回复
调试下下,看你26的值是不是NUll
mangchao330688 2011-03-15
  • 打赏
  • 举报
回复
一般都是没取到值,你设断点调试一下,看哪没取到值
宝_爸 2011-03-15
  • 打赏
  • 举报
回复
八成是在这些地方

DropDownListNation.Items[this.DropDownListNation.SelectedIndex].Text
this.bumen.Items[this.DropDownListNation.SelectedIndex].Text
RadioButtonList2.SelectedItem.Text

26行设置个断点。看看用到的值里哪个是null。

SK_Aqi 2011-03-15
  • 打赏
  • 举报
回复
是不是你的item里面没有选中值啊
比如你直接替换:
DropDownListNation.Items[this.DropDownListNation.SelectedIndex].Text

为一个字符试试吧...
yan267 2011-03-15
  • 打赏
  • 举报
回复
this.bumen.Items[this.DropDownListNation.SelectedIndex].Text 


这块是不是写错了?

或者某个控件没有?
大笨蛋 2011-03-15
  • 打赏
  • 举报
回复
ealzero 2011-03-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lbq0801 的回复:]
哪一句报错,调试去
[/Quote]
行26 有误 有调试下,不知道该如何解决 望高手们能给些点子
tony312ws 2011-03-15
  • 打赏
  • 举报
回复
你检查下你的参数是不是那个是空的!!
ealzero 2011-03-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sk_aqi 的回复:]
那里报的错啊?
[/Quote]
行26 提示--System.NullReferenceException: 未将对象引用设置到对象的实例。
SK_Aqi 2011-03-15
  • 打赏
  • 举报
回复
那里报的错啊?
lbq0801 2011-03-15
  • 打赏
  • 举报
回复
哪一句报错,调试去

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧