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

  • Question

  • User-1292128788 posted

    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


    Saturday, August 20, 2011 7:51 AM

All replies

  • User1508394307 posted

    That's strange that it shows an error on that line. Try to disable try..catch for the moment and see if you would get another error. 

    Saturday, August 20, 2011 8:01 AM
  • User-1292128788 posted

    Same error.

    Saturday, August 20, 2011 11:08 AM
  • User1508394307 posted

    Your code looks correct. Try to debug it, for example, by executing following code

    // Get the ConnectionStrings collection.
    ConnectionStringSettingsCollection connections = ConfigurationManager.ConnectionStrings;
    Response.Write ("count=" + connections.Count);
    // Get the collection elements.
    foreach (ConnectionStringSettings connection in connections)
    	string name = connection.Name;
    	string provider = connection.ProviderName;
    	string connectionString = connection.ConnectionString;
    	Response.Write("Name=" + name);
    	Response.Write("Connection string=" + connectionString);
    	Response.Write("Provider=" + provider);

    Maybe it could help to see where the error comes from.

    Sunday, August 21, 2011 4:46 PM