Click here to Skip to main content
15,891,855 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Error:System.NullReferenceException: Object reference not set to an instance of an object. at Samregistration.Button1_Click(Object sender, EventArgs e)<br />


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Text;
using System.Configuration;
using System.Data;

public partial class Samregistration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
{
       SqlConnection conn= new SqlConnection (ConfigurationManager.ConnectionStrings["Data Source=SUMIT92\\SQLEXPRESS;Initial Catalog=Registration1.dbo;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"].ConnectionString);
        conn.Open();
        string insertQuery ="insert into Regstration2 (UserName,E-mail,Password,Country) values (@Uname,@email,@password,@country)";
        SqlCommand com = new SqlCommand (insertQuery, conn);
        com.Parameters.AddWithValue("@Uname", TextBoxUN.Text);
            com.Parameters.AddWithValue("@email",TextBoxEM.Text);
            com.Parameters.AddWithValue("@password",TextBoxp.Text);
            com.Parameters.AddWithValue("@country",CN.SelectedItem.ToString());

            com.ExecuteNonQuery();
            Response.Write("Registration is sucessfull");

            conn.Close();
        }
        catch (Exception ex)
        {
          Response.Write("Error:"+ex.ToString());
        }
    }
}
Posted
Updated 6-Jul-15 19:52pm
v2
Comments
Harshad Kathiriya 7-Jul-15 1:51am    
Do you debug the code? Find which object is not initialized.
PIEBALDconsult 7-Jul-15 1:53am    
Luke... Use the debugger...
I would first check the value of CN.SelectedItem
harpreet.singh6771 7-Jul-15 2:11am    
com.Parameters.AddWithValue("@country",CN.SelectedItem.Value.ToString());

It looks like you're trying to use the entire connection string as a key to a connection string. When you call the indexed on ConfigurationManager.ConnectionStrings I would expect that you're supposed to pass a name/key that identifies a connection string in the config, but you're passing the entire connection string as name/key.

Try changing
C#
SqlConnection conn= new SqlConnection (ConfigurationManager.ConnectionStrings["Data Source=SUMIT92\\SQLEXPRESS;Initial Catalog=Registration1.dbo;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"].ConnectionString);

to
C#
SqlConnection conn= new SqlConnection ("Data Source=SUMIT92\\SQLEXPRESS;Initial Catalog=Registration1.dbo;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"]);


Hope this helps,
Fredrik
 
Share this answer
 
Best guess - no selected item in the combo box, so it fails on this line:
C#
com.Parameters.AddWithValue("@country",CN.SelectedItem.ToString());

Check your values at the top of the method and make sure that all the user inputs are valid before you even try to go to the database. For example: is the username entered? Is it unique? Is there an email, and is it a valid format?

In addition:
1) Your connection string name should not be the same as the connection string:
C#
SqlConnection conn= new SqlConnection (ConfigurationManager.ConnectionStrings["Data Source=SUMIT92\\SQLEXPRESS;Initial Catalog=Registration1.dbo;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"].ConnectionString);
Shoudl be somethign like:
C#
SqlConnection conn= new SqlConnection (ConfigurationManager.ConnectionStrings["Registration"].ConnectionString);
.
2) You should Dispose of Connection and Command objects - your code leaves the connection open if there is a problem because you don't close it in the catch block. Consider using a using block to ensure objects are always destroyed when you are finished with them:
C#
try
   {
   using (SqlConnection conn= new SqlConnection (ConfigurationManager.ConnectionStrings["Registration"].ConnectionString))
       {
       conn.Open();
       string insertQuery ="insert into Regstration2 (UserName,E-mail,Password,Country) values (@Uname,@email,@password,@country)";
       using (SqlCommand com = new SqlCommand (insertQuery, conn))
           {
           com.Parameters.AddWithValue("@Uname", TextBoxUN.Text);
           com.Parameters.AddWithValue("@email",TextBoxEM.Text);
           com.Parameters.AddWithValue("@password",TextBoxp.Text);
           com.Parameters.AddWithValue("@country",CN.SelectedItem.ToString());

           com.ExecuteNonQuery();
           Response.Write("Registration is sucessfull");
           }
       }
   }
   catch (Exception ex)
       {
       Response.Write("Error:"+ex.ToString());
       }

3) And please, never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]
Also see here: Code crime: Text based passwords[^]
 
Share this answer
 

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