i have deploy my .Net web application.
i can retrieve the first page that is the login.aspx page.
but, when i click the login button, the error has occur.
Execute Reader: Connection property has not been initialized.
After user click login button from the login page, menu page should be displayed if the username and password is valid.
in the visual studio 2008 environment, when i debug the code, it work properly.
so, i dont know what is wrong with that.
is it something wrong with my coding or other setting?
i am writing database connection code for my web application in a C# Class. this is the code for my database connection class.
public class DBConn
{
SqlConnection SQLConn = new SqlConnection(@"Data Source=user-05911940da\sqlexpress;Initial Catalog=BDAS;Integrated Security=True");
public SqlConnection OpenDB()
{
try
{
SQLConn.Open();
return SQLConn;
}
catch (SqlException exp)
{
Console.Out.WriteLine("SQL error!!" + exp);
return null;
}
}
public void CloseDB()
{
try
{
SQLConn.Close();
}
catch (SqlException exp)
{
Console.Out.WriteLine("SQL error!!" + exp);
}
}
}
Every page will invoke the database connection class to perform task with database such as saving data and so on.
this is the code for my login page that calls the DBConn Class:
namespace BDAS
{
public partial class Login : System.Web.UI.Page
{
DBConn myDB = new DBConn();
SqlConnection SQLConn = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnEnter_Click(object sender, EventArgs e)
{
String username = txtUsername.Text;
String password = txtPassword.Text;
try
{
SQLConn = myDB.OpenDB();
string selectString = "SELECT * FROM BDE WHERE username='" + username + "' AND password='" + password + "'";
SqlCommand cmd = new SqlCommand(selectString, SQLConn);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
Session.Add("user1", password);
Response.Redirect("MainPage.aspx");
}
else
{
Response.Write("<script>alert('Invalid Username or Password!!!')</script>");
throw new Exception();
}
}
catch (Exception ex)
{
Response.Write("<script>alert('" + ex.Message + "')</script>");
}
finally
{
myDB.CloseDB();
}
}
}
}