I'm new to asp.net and I was trying to make a simple login page that will search the username and password entered in a database. I created the App_Data
folder and I have database in it called Database1.mdf
. Inside that database there is a table called users
.
This is the code behind the login page:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
string mcn = Request.Form["username"];
string pass = Request.Form["pass"];
string query = "SELECT * FROM users WHERE mcn = '" + mcn + "' AND pass = '" + pass + "'";
string connectionstring = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=App_Data\Database1.mdf;Integrated Security=True";
SqlConnection con = new SqlConnection(connectionstring);
SqlDataAdapter ad = new SqlDataAdapter(query, con);
con.Open();
SqlCommand com = new SqlCommand(query, con);
SqlDataReader data = com.ExecuteReader();
bool found;
found = (bool)data.Read();
con.Close();
if (found)
{
Session["user"] = true;
}
Response.Redirect("Main.aspx");
}
}
When I run the page and submit the login form which calls this Page_Load
I get this error:
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code
Additional information: An attempt to attach an auto-named database for file App_Data\Database1.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
Thanks in advance!