Click here to Skip to main content
15,920,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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)

C#
<connectionstrings>

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

  </connectionstrings>


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

C#
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)";

            try
            {
                conn.Open();
                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.Parameters.Add(param[i]);

                }



                cmd.CommandType = CommandType.Text;

                cmd.ExecuteNonQuery();
            }

            catch (System.Data.SqlClient.SqlException ex)
            {

                string msg = "Insert Error:";

                msg += ex.Message;

                throw new Exception(msg);

            }

            finally
            {

                conn.Close();

            }
        }

        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!");



              
            }

            else
            {

                Response.Write("Password did not match");

                TxtPassword.Focus();

            }
        }


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

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

Thanks
Posted

Check the configuration file contains an entry for MyCon in connectionStrings.
 
Share this answer
 
Try using this

HTML
connectionstring= "Server=myServerAddress;Database=myDataBase;Integrated Security=True;Asynchronous Processing=True;"

or 

connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=myDataBase;Integrated Security=True"
 
Share this answer
 
No still the same problem faced.

Please help me out
 
Share this answer
 
Got the solution and also there was some error at web.config file..
Thanks everyone
 
Share this answer
 
You connection string is wrong Sql Express default instance is .\sqlexpress, so you must say somenthing like : "server=localhost\sqlexpress".
Check this:

http://www.connectionstrings.com

for more details about connections string
 
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