0

I am trying to create page in asp.net page and I am getting the following error

Error:-System.NullReferenceException: Object reference not set to an instance of an object. at TestdateAssistor.user_info.Button1_Click1(Object sender, EventArgs e)

at this line

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Data Source=LAPTOP-O9SI19I0\SQLEXPRESS;Integrated Security=True"].ConnectionString);

This is my complete code

   try
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Data Source=LAPTOP-O9SI19I0\\SQLEXPRESS;Integrated Security=True"].ConnectionString);
        conn.Open();
        String insert = "insert into Table (NAME,ADDRESS,MOBILE NO,ADHAR NO,DOB) values (@name,@add,@mob,@adhar,@dob)";
        SqlCommand com = new SqlCommand(insert,conn);
        com.Parameters.AddWithValue("@name",TextBox1.Text);
        com.Parameters.AddWithValue("@add",TextBox2.Text);
        com.Parameters.AddWithValue("@mob",TextBox3.Text);
        com.Parameters.AddWithValue("@adhar", TextBox4.Text);
        com.Parameters.AddWithValue("@dob", TextBox5.Text);
        com.ExecuteNonQuery();
        Response.Write("Successful Registration!!");
        conn.Close();
    }

    catch (Exception ex)
    {
        Response.Write("Error:-" + ex.ToString());
    }

What changes should I make in the connection string?

  • 2
    The ConnectionStrings is a collection. You should have it defined in your web.config in the proper section. Then you retrieve the value passing the Name between the square brackets not the whole connectionstring. – Steve Jun 24 '18 at 09:32
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –  Jun 24 '18 at 09:42
  • A pet peeve of mine is proper Exception handling and your code is dangerous as you go on after fatal exceptions. Here are two artciles on the mater I link often: https://blogs.msdn.microsoft.com/ericlippert/2008/09/10/vexing-exceptions/ | https://www.codeproject.com/Articles/9538/Exception-Handling-Best-Practices-in-NET – Christopher Jun 24 '18 at 09:46

3 Answers3

2

You’re using the connection string as a key to your connection strings defined in the Web.config. So you need to define the connection string there and give it a name, then reference it in the code by name:

Web.config:

<connectionStrings>
<add name="myConnectionString" connectionString="Data Source=LAPTOP-O9SI19I0\\SQLEXPRESS;Integrated Security=True" />
</connectionStrings>

Code:

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
2

The ConnectionStrings is a collection automatically built for you by the framework. Its content is retrieved from the web.config where you should have it defined in the proper section.

Then you retrieve its value passing the Name between the square brackets not the whole connectionstring.

string cnString = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
SqlConnection conn = new SqlConnection(cnString);

and in your web.config you add the proper definition for your connectionstring

<configuration>
  <connectionStrings>
    <add name="MyConnection" connectionString="Data Source=LAPTOP-O9SI19I0\\SQLEXPRESS;Integrated Security=True"/>
  </connectionStrings>
  .....
</configuration>
Steve
  • 213,761
  • 22
  • 232
  • 286
1

Error:

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Data Source=LAPTOP-O9SI19I0\\SQLEXPRESS;Integrated Security=True"].ConnectionString); 

Solution 1:
in main program (.cs)

SqlConnection conn = new SqlConnection("Data Source=LAPTOP-O9SI19I0\\SQLEXPRESS;Integrated Security=True"); 

Solution 2:
in web.config

<configuration>
  <connectionStrings>
    <add name="MyConnection" connectionString="Data Source=LAPTOP-O9SI19I0\\SQLEXPRESS;Integrated Security=True"/>
  </connectionStrings>
</configuration>  

in main program (.cs)

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;); 

reference: https://msdn.microsoft.com/en-us/library/d7469at0(v=vs.110).aspx

Kyle_397
  • 439
  • 4
  • 14