“错误:对象引用未设置为对象的实例.(NullReferenceException由用户代码未处理)" [英] "Error: Object reference not set to an instance of an object.(NullReferenceException was unhandled by user code)"
问题描述
错误:对象引用未设置为对象的实例.(用户代码未处理NullReferenceException)"
你能建议我走出错误!.
我创建了注册页面,还创建了数据库.当我们提交注册字段时,该字段应存储在数据库中(我的数据库名称为"registration").我认为这是连接字符串问题.我在"Web.config" 中为连接字符串编写了代码.
你能建议我走出错误!.
代码:
"Error: Object reference not set to an instance of an object.(NullReferenceException was unhandled by user code)"
Can you please suggest me to come out of the error !.
I was created registration page, and also created database. when we submit that registration fields that fields should be stored in database(my database name is "registration"). i think it''s connection strings problem.I written a code in "Web.config" for connection string.
Can you please suggest me to come out of the error !.
CODE:
using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
SqlConnection con = new SqlConnection("Data Source=TEKPC26\\SQLEXPRESS;Initial Catalog=registration;Integrated Security=True");
SqlCommand cmd;
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["registration"].ToString());
con.Open();
cmd=new SqlCommand("insert into values(''" + TextBox1.Text + "'',''" + TextBox2.Text + "'',''" + TextBox3.Text+"'')",con);
cmd.ExecuteNonQuery();
}
}
AND
Web.config代码:
AND
Web.config CODE:
<configuration>
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.TEKPC26\\SQLEXPRESS;
Integrated Security=SSPI;AttachDBFilename=C:\\Documents and Settings\\Administrator\\My Documents\\Visual Studio 2010\\WebSites\\WebSite1\\App_Data\\registration.mdf;
User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
推荐答案
在调试器中运行程序,它将向您显示出现问题的行以及哪个对象为null
.
Run your program in a debugger and it will show you which line has the problem, and which object is null
.
同意Mehdi.您必须调试代码以找出问题所在.这是有关该错误的文章,对您会很有用.
未将对象引用设置为对象实例的意思是什么 [
Agree with Mehdi. You must debug your code to find out the issue. Here a post about that error which''ll be useful for you.
What does Object reference not set to an instance of an object mean[^]
这篇关于“错误:对象引用未设置为对象的实例.(NullReferenceException由用户代码未处理)"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!