In my C# application, i am trying to connect to a DataConnection. The application is just a simple Login form but as soon as the user clicks the login button, it throws an exception error referencing the Connection String. Can anyone help?
public void userLoginSuccessfull()
{
try
{
//////////////////////////////////
// This line is throwing the error
//////////////////////////////////
string connString = System.Configuration.ConfigurationManager
.ConnectionStrings["connectionString"].ConnectionString;
if (txtUsername.Text != "" & txtPassword.Text != "")
{
string queryText = @"SELECT Count(*) FROM Users
WHERE Username = @Username AND Password = @Password";
using (SqlConnection cn = new SqlConnection(connString))
using (SqlCommand cmd = new SqlCommand(queryText, cn))
{
cn.Open();
cmd.Parameters.AddWithValue("@Username", txtUsername.Text);
cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
int result = (int)cmd.ExecuteScalar();
if (result > 0)
{
loadUserForm();
}
}
}
}
catch (Exception ee)
{
Console.WriteLine(ee.StackTrace);
}
}
This is the error
'EnviroWaste Job Logger.vshost.exe' (CLR v4.0.30319: EnviroWaste Job Logger.vshost.exe): Loaded 'C:UserslistmDocumentsVisual Studio 2013ProjectsEnviroWaste Job LoggerEnviroWaste Job LoggerinDebugEnviroWaste Job Logger.exe'. Symbols loaded.
A first chance exception of type 'System.Configuration.ConfigurationErrorsException' occurred in System.Configuration.dll
Configuration system failed to initialize
A first chance exception of type 'System.TypeInitializationException' occurred in System.Data.dll
The connection string looks like this:
connectionString="Data Source=(LocalDB)v11.0;AttachDbFilename=|DataDirectory|UsersDatabase.mdf;Integr??ated Security=True"
See Question&Answers more detail:os