Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ERROR:System.NullReferenceException:Object reference not set to an instance of an object.

unhandled exception occurred during the execution of the current webpart.


CODE:
C#
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["ApplicationServices"].ToString());
        con.Open();
        cmd = new SqlCommand("insert into values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')", con);
        cmd.ExecuteNonQuery();
    }
}


WEB.CONFIG CODE:
XML
<connectionStrings>
    <add name="registration" connectionString="data source=.TEKPC26\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=registration.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
  </connectionStrings>



when execute my program getting one error!! is there any changes in web.config file?
Posted
Updated 6-Nov-11 23:13pm
v4
Comments
thatraja 7-Nov-11 5:22am    
Check my updated answer
[no name] 7-Nov-11 5:41am    
i was changed my code with your advice, but one error..
<connectionstrings>
<add name="registration" connectionstring="data source=.TEKPC26\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=registration.mdf;User Instance=true" providername="System.Data.SqlClient">



"Unrecognized configuration section connectionstrings"
thatraja 7-Nov-11 7:29am    
Check my updated answer again. Easy fix there at last. Let me know if still you are facing any issue.

Debug your code & find out which line contains the issue

What does Object reference not set to an instance of an object mean[^]

EDIT
-------------------------------
Try this code
C#
public partial class Default2 : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;

    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    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();
    }
}

HTML
<connectionstrings>
    <add name="registration" connectionstring="data source=.TEKPC26\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=registration.mdf;User Instance=true" providername="System.Data.SqlClient" />
  </connectionstrings>


BTW avoid Sql injection attacks
SQL injection
SQL Injection Attacks and Some Tips on How to Prevent Them[^]
SQL injection attacks[^]
SQL Injection Knowhow[^]
SQL Injection and Cross-Site Scripting[^]

EDIT 2
-------------------------------------
muralikrishna53 wrote:
i was changed my code with your advice, but one error..

"Unrecognized configuration section connectionstrings"

Here an easy fix
Error Solution: Fix for ‘Unrecognized configuration section connectionStrings’ Error in ASP.NET 2.0[^]
 
Share this answer
 
v3
Comments
[no name] 7-Nov-11 4:53am    
button click event,this line..


SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
thatraja 7-Nov-11 4:57am    
The Key name is different in two locations. In web.config its registration but in code its ApplicationServices. So make it same at both places.

BTW at that time my reply you didn't include the web.config details in your question(Looks like you did update your question again) otherwise I could have fixed that.
No need of following code

SqlConnection con = new SqlConnection("Data Source=.TEKPC26\\SQLEXPRESS;Initial Catalog=registration;Integrated Security=True");



Replace "ApplicationServices" with "registration" like below
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["registration"].ToString());
 
Share this answer
 
v2
Comments
[no name] 7-Nov-11 4:55am    
i just replaced your code but again showing same error!!
[no name] 7-Nov-11 4:58am    
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."
P.Salini 7-Nov-11 5:22am    
It means either Your connection string is wrong or you dont have permission to access that database server.
[no name] 7-Nov-11 6:36am    
how to write connection strings related my code?
In web.config you have give name of connection string as registration

so while accessing use that name

use this
C#
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["registration"].ToString());
 
Share this answer
 
v2
Comments
P.Salini 7-Nov-11 5:23am    
It means either Your connection string is wrong or you dont have permission to access that database server.
[no name] 7-Nov-11 5:36am    
i have permission, i was doing in administrator account. can u suggest how to write connection string code!!!

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



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