Problem accessing SQL Server [Local computer] in asp.net website? RRS feed

  • Question

  • Hi all 

    I was designing an registration system in which i want that the info (Name, user name, pass, country) should go to the table in SQL server express.

    Database Info : i have created an ShareSignup database in which there is one table named tblReg having columns Name, Username, pass,                                 country.

    The database is in local computer (my computer) in SQL Server 2008 Express and i use MyPC\SQLEXPRESS as server name to login to the SQL Server.


    Now i was designing a registration system in which i have a SignUp.aspx page, i added following code to web.config page (for connecting to Database)

     <add name="MyCon" connectionString="server=(local);Initial Catalog=ShareSignup;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />

    Now on the next for SignUp.aspx.cs page i added the code

     public string GetConnectionString()
       //sets the connection string from your web config file "ConnString" is the name of your Connection String
       return System.Configuration.ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString;
      private void ExecuteInsert(string name, string uname, string pass, string country)
       SqlConnection conn = new SqlConnection(GetConnectionString());
       string sql = "INSERT INTO tblReg (Name, UserName, Password,Country) VALUES "
           + " (@Name,@UserName,@Password,@Country)";
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlParameter[] param = new SqlParameter[4];
        param[0] = new SqlParameter("@Name", SqlDbType.VarChar, 50);
        param[1] = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
        param[2] = new SqlParameter("@Password", SqlDbType.VarChar, 50);
        param[3] = new SqlParameter("@Country", SqlDbType.VarChar, 15);
        param[0].Value = name;
        param[1].Value = uname;
        param[2].Value = pass;
        param[3].Value = country;
        for (int i = 0; i < param.Length; i++)
        cmd.CommandType = CommandType.Text;
       catch (System.Data.SqlClient.SqlException ex)
        string msg = "Insert Error:";
        msg += ex.Message;
        throw new Exception(msg);
      protected void Button1_Click(object sender, EventArgs e)
       if (TxtPassword.Text == TxtRePassword.Text)
        //call the method to execute insert to the database
        ExecuteInsert(TxtName.Text, TxtUserName.Text, TxtPassword.Text, TxtAddress.Text);
        Response.Write("Record was successfully added!");
        Response.Write("Password did not match");

    Now on runing the page and hitting Submit (button1) i get this error.


    Object reference not set to an instance of an object.


    and it shows the error at line

    return System.Configuration.ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString;

     what can be the problem ?

     may be in web.config code ? 

    or may be with setting <connectingString> ?


    Please help me out or suggest me how to connect with SQL Express

    Kundan S.
    Saturday, August 20, 2011 3:34 PM


  • The database is in local computer (my computer) in SQL Server 2008 Express and i use MyPC\SQLEXPRESS as server name to login to the SQL Server.


    Your connection string needs to also specify the instance name, SQLEXPRESS.  Assuming your web site is running on the same machine as the database server, the web.config entry should be:

     <add name="MyCon" connectionString="server=(local)\SQLEXPRESS;Initial Catalog=ShareSignup;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />


    However, it doesn't seem your code is getting that far.  It looks like the connection string entry is missing from the config file or the name is not spelled correctly.  Either will result in the object reference exception.







    Dan Guzman, SQL Server MVP, http://weblogs.sqlteam.com/dang/
    • Proposed as answer by Lola Wang Tuesday, August 23, 2011 3:28 AM
    • Marked as answer by Lola Wang Friday, August 26, 2011 10:14 AM
    Saturday, August 20, 2011 3:57 PM