Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
"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 CODE:

XML
<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>
Posted
Updated 4-Nov-11 19:56pm
v3

Run your program in a debugger and it will show you which line has the problem, and which object is null.
 
Share this answer
 
Comments
[no name] 5-Nov-11 2:11am    
This line showing error!!

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["registration"].ToString());
Mehdi Gholam 5-Nov-11 2:20am    
use "ApplicationServices" instead of "registration".
[no name] 5-Nov-11 2:25am    
How to use "ApplicationServices" ??
Mehdi Gholam 5-Nov-11 2:29am    
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
[no name] 5-Nov-11 2:33am    
getting same error! if you don't mind give me your mail id, i will send total code ,and screen shots also.
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[^]
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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