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);
You need to reference System.Configuration
SqlConnection cnn = new SqlConnection(System.Configuration.ConfigurationManager.
ConnectionStrings["connect"].ConnectionString);
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.