0

I am trying to create registiration. Here is web.config

<configuration>
      <connectionStrings>
        <clear/>
        <add name="connect" connectionString="Data Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|web.mdf; Security=True; uid=Rahman\Rahman; pwd=12345;"/>
      </connectionStrings>
        <system.web>
          <compilation debug="true" targetFramework="4.5" />
          <httpRuntime targetFramework="4.5" />
        </system.web>

        </configuration>

and here I use sql query to write data into table called Users

protected void btnReg_Click(object sender, EventArgs e) {

    SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString);
    string q = "Insert into Users (Username, Password) Values(@username, @password)";
    SqlCommand cmd = new SqlCommand(q, cnn);
    cnn.Open();

    try {
        cmd.Parameters.AddWithValue("@username", txtUsername.Text);
        cmd.Parameters.AddWithValue("@password", txtPass.Text);
        cmd.ExecuteNonQuery();
        cnn.Close();
        textReg.Text = "Pozitive";
    }
    catch {
        textReg.Text = "Negative";
    }
}

But unfortunately I got error

"An exception of type 'System.ArgumentException' occurred in System.Data.dll but was not handled in user code"

Error points this line,

SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString);
xsami
  • 1,312
  • 16
  • 31
user1953051
  • 321
  • 2
  • 7
  • 21

2 Answers2

0

You need to reference System.Configuration

SqlConnection cnn = new SqlConnection(System.Configuration.ConfigurationManager.
    ConnectionStrings["connect"].ConnectionString);
StevieB
  • 67
  • 4
0

Use

// read connection string 
private string connString = Convert.ToString (System.Configuration.ConfigurationManager.ConnectionStrings["connect"].ConnectionString); 

// use the variable for a connection
SqlConnection cnn = new SqlConnection(connString );

Also, make sure that you have added a reference to System.Configuration in your project.

Saagar Elias Jacky
  • 2,684
  • 2
  • 14
  • 28