473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Getting Error: Object reference not set to an instance of an object.

HI,
I was created consultant registration page using ASP.NET 4.0, and also created database, my database name is "registration.mdf", i just wrote this below code, when debugging my page getting one error like "Object reference not set to an instance of an object". Attached the document also(screen shots)!



Web.config code:


Expand|Select|Wrap|Line Numbers
  1.  <connectionStrings>
  2.  
  3.         <add name="ApplicationServices" connectionString="data source=.TEKPC26\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=registration.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
  4.     </connectionStrings>
  5.  


BUTTON CLICK CODE:
Expand|Select|Wrap|Line Numbers
  1.  protected void Button1_Click(object sender, EventArgs e)
  2.     {
  3.  
  4.         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["registration"].ToString());
  5.         con.Open();
  6.         cmd=new SqlCommand("insert into values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text+"')",con);
  7.         cmd.ExecuteNonQuery();
  8.  
  9.     }
  10.  
Nov 4 '11 #1

✓ answered by Frinavale

I guess you didn't understand my original answer.
In your web.conf file you have specified a "name" attribute for your connection string. You need to use this name as the "key" for ConfigurationManager order to retrieve the connection string from the web.config file.

Based on the code that you originally posted you would have to change 1 thing in your button click event...you need to provide "ApplicationServices" as the key to the ConfigurationManager instead of "registration". (Even though "registration" may be the name of the database that you are connecting to, you named the connection string in your web.config "ApplicationServices")

This is your original web.config code:
Expand|Select|Wrap|Line Numbers
  1.  <connectionStrings>
  2.  
  3.   <add name="ApplicationServices" connectionString="data source=.TEKPC26\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=registration.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
  4.     </connectionStrings>
  5.  


This is the modified button click event based on the original code you posted:
Expand|Select|Wrap|Line Numbers
  1. protected void Button1_Click(object sender, EventArgs e)
  2.     {
  3.  
  4.         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
  5.         con.Open();
  6.         cmd=new SqlCommand("insert into values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text+"')",con);
  7.         cmd.ExecuteNonQuery();
  8.  
  9.     }
  10.  
Now, if you want to be clear about which database the connection string connects to it might be a better idea to change the "name" of your connection string in your web.config file and leave your button click the same...

Like this:

This is the modified web.config code based on your original web.config code:
Expand|Select|Wrap|Line Numbers
  1.  <connectionStrings>
  2.  
  3.   <add name="registration" connectionString="data source=.TEKPC26\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=registration.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
  4.     </connectionStrings>
  5.  


This is the original button click event code you posted:
Expand|Select|Wrap|Line Numbers
  1. protected void Button1_Click(object sender, EventArgs e)
  2.     {
  3.  
  4.         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["registration"].ToString());
  5.         con.Open();
  6.         cmd=new SqlCommand("insert into values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text+"')",con);
  7.         cmd.ExecuteNonQuery();
  8.  
  9.     }
  10.  
So either you need to change the name of your connection string as defined in your web.config file or you need to change the name provided to the ConfigurationManager. Either approach works.

-Frinny

5 6386
Frinavale
9,735 Expert Mod 8TB
You named your connection string "ApplicationServices" in your web.config file.

You need to use this name in your C# code that you are using to retrieve the connection string....currently you are not doing so.

-Frinny
Nov 4 '11 #2
Maraj
24
You need to write your connection string name in the sql connection object like this
Expand|Select|Wrap|Line Numbers
  1.  SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());

Also always try to write your code in try-catch block this will give you clear picture of exception.
Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3. //Your code
  4. }
  5. catch(exception ex)
  6.  
  7. {
  8. //Display exception
  9. }
Nov 5 '11 #3
"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:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Configuration;
  3. using System.Data;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.HtmlControls;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Xml.Linq;
  13. using System.Data.SqlClient;
  14.  
  15.  
  16. public partial class Default2 : System.Web.UI.Page
  17. {
  18.     protected void Page_Load(object sender, EventArgs e)
  19.     {
  20.  
  21.     }
  22.     SqlConnection con = new SqlConnection("Data Source=TEKPC26\\SQLEXPRESS;Initial Catalog=registration;Integrated Security=True");
  23.     SqlCommand cmd;
  24.     protected void Button1_Click(object sender, EventArgs e)
  25.     {
  26.  
  27.         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["registration"].ToString());
  28.         con.Open();
  29.         cmd=new SqlCommand("insert into values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text+"')",con);
  30.         cmd.ExecuteNonQuery();
  31.  
  32.     }
  33. }

AND

Web.config CODE:

Expand|Select|Wrap|Line Numbers
  1. <configuration>
  2.  
  3.   <connectionStrings>
  4.  
  5.         <add name="ApplicationServices" connectionString="data source=.TEKPC26\\SQLEXPRESS;
  6.          Integrated Security=SSPI;AttachDBFilename=C:\\Documents and Settings\\Administrator\\My Documents\\Visual Studio 2010\\WebSites\\WebSite1\\App_Data\\registration.mdf;
  7.          User Instance=true&quot; providerName=&quot;System.Data.SqlClient" />
  8.     </connectionStrings>
Nov 5 '11 #4
Frinavale
9,735 Expert Mod 8TB
I guess you didn't understand my original answer.
In your web.conf file you have specified a "name" attribute for your connection string. You need to use this name as the "key" for ConfigurationManager order to retrieve the connection string from the web.config file.

Based on the code that you originally posted you would have to change 1 thing in your button click event...you need to provide "ApplicationServices" as the key to the ConfigurationManager instead of "registration". (Even though "registration" may be the name of the database that you are connecting to, you named the connection string in your web.config "ApplicationServices")

This is your original web.config code:
Expand|Select|Wrap|Line Numbers
  1.  <connectionStrings>
  2.  
  3.   <add name="ApplicationServices" connectionString="data source=.TEKPC26\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=registration.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
  4.     </connectionStrings>
  5.  


This is the modified button click event based on the original code you posted:
Expand|Select|Wrap|Line Numbers
  1. protected void Button1_Click(object sender, EventArgs e)
  2.     {
  3.  
  4.         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
  5.         con.Open();
  6.         cmd=new SqlCommand("insert into values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text+"')",con);
  7.         cmd.ExecuteNonQuery();
  8.  
  9.     }
  10.  
Now, if you want to be clear about which database the connection string connects to it might be a better idea to change the "name" of your connection string in your web.config file and leave your button click the same...

Like this:

This is the modified web.config code based on your original web.config code:
Expand|Select|Wrap|Line Numbers
  1.  <connectionStrings>
  2.  
  3.   <add name="registration" connectionString="data source=.TEKPC26\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=registration.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
  4.     </connectionStrings>
  5.  


This is the original button click event code you posted:
Expand|Select|Wrap|Line Numbers
  1. protected void Button1_Click(object sender, EventArgs e)
  2.     {
  3.  
  4.         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["registration"].ToString());
  5.         con.Open();
  6.         cmd=new SqlCommand("insert into values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text+"')",con);
  7.         cmd.ExecuteNonQuery();
  8.  
  9.     }
  10.  
So either you need to change the name of your connection string as defined in your web.config file or you need to change the name provided to the ConfigurationManager. Either approach works.

-Frinny
Nov 5 '11 #5
I just replaced your code, when executing the program getting one error! like..

ERROR:sqlexception was unhandled by user code.

"A network related or instance-specific error occurred while establishing a connection to sql server.the server was not found or was not accessible. Verify that the instance name is correct and that sql server is configured to allow remote connections."
Nov 7 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Pkpatel | last post by:
Hi, I keep getting this error every time I try to load crystalreportviewer on a webform with a dataset. Here is the error: -------------------------------------------------------- Server...
3
by: Adam | last post by:
We have a web site that uses .vb for the web pages and .cs for a class module. We are getting the error in .NET 2.0 and VS 2005 beta 2. It does work with .NET 1.1. When trying to access a page...
9
by: Remulac | last post by:
Hello, I'm trying to get the value out of a dropdown list box and assign it to a variable. When I click on the list box, I invoke this line of code. I get the error, "Object reference not set...
7
by: Brett | last post by:
I'm not sure why I keep getting this error, "Object reference not set to an instance of an object". Private Function somefunction() as string Dim MyCurrentClass As New Class1 Try For i As...
2
by: ankur seth via DotNetMonster.com | last post by:
Getting error Null Reference exception when a procedure is called in vb.net ..Why does it occur and how to resolve it. --- ANkur -- Message posted via http://www.dotnetmonster.com
1
by: somersbar | last post by:
i have a webform with a dropdownlist and 2 textboxes. it has a dataset with a table with 2 fields, 'areaID' and 'areaName'. in the Data properties window of the DDL, i have the DataTextField set...
9
by: bill | last post by:
I keep getting Object reference not set to an instance of an object error when trying to run my application on an installed client machine. I installed it on several others and it runs fine. I...
10
by: Arpan | last post by:
Consider the following code that adds a table to a DataSet dynamically: <script runat="server"> Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) 'Create an empty DataSet Dim dSet As...
6
by: kalaivanan | last post by:
hi, i am a beginner in c#. i have theoretical knowledge about object, reference and instance. but i want to know clearly about what is an object, reference and instance. can any one help me? or...
2
by: rksadhi | last post by:
/*Geting error ---object reference not set to an instance---at bold line----plz reply asap thanks in advance*/ cmd = new OleDbCommand ("SELECT e.emp_id,e.email, m.email AS Email FROM emp_details...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.