When I build my project, I didn't receive any errors but when running and entering in data, I get this error:

Invalid Login, please try again! System.InvalidOperationException: ExecuteNonQuery: Connection property has not been initialized. at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at Login.DBConnection(String txtUser, String txtPass, String txtClub) in c:\Users\Bunky\Documents\Visual Studio 2005\RotaryClub\Login.aspx.cs:line 77Error Connecting to the database

I'm not sure if my web.config file is wrong or the code behind.

[B]
<appSettings>
<add key="strConn" value="server=;Database=;Integrated Security=True;"/>
</appSettings>

public void cmdSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
if (DBConnection(txtUserName.Text.Trim(), txtPassword.Text.Trim(), txtClub.Text.Trim()))
{
FormsAuthentication.RedirectFromLoginPage (txtUserName.Text, false);
}
else
{
lblMessage.Text = "Invalid Login, please try again!";
}
}

}

private bool DBConnection(string txtUser, string txtPass, string txtClub)
{
SqlConnection myConn = new SqlConnection(ConfigurationManager.AppSettings["strConn"]);
SqlCommand myCmd = new SqlCommand();
myCmd.CommandText = "SELECT COUNT(*) AS Num_of_User FROM tbl_Login WHERE(((tbl_Login.UserName)=[@UserName]) AND ((tbl_Login.Password)=[@Password]) AND ((tbl_Login.Club)=[@Club])";

SqlParameter objParam1;
SqlParameter objParam2;
SqlParameter objParam3;
SqlParameter returnParam;

objParam1 = myCmd.Parameters.Add ("@UserName", SqlDbType.VarChar);
objParam2 = myCmd.Parameters.Add ("@Password", SqlDbType.VarChar);
objParam3 = myCmd.Parameters.Add ("@Club", SqlDbType.VarChar);
returnParam = myCmd.Parameters.Add ("@Num_of_User", SqlDbType.Int);

objParam1.Direction = ParameterDirection.Input;
objParam2.Direction = ParameterDirection.Input;
objParam3.Direction = ParameterDirection.Input;
returnParam.Direction = ParameterDirection.ReturnValue;

objParam1.Value = txtUser;
objParam2.Value = txtPass;
objParam3.Value = txtClub;

try
{
if (myConn.State.Equals(ConnectionState.Closed))
{
myConn.Open();
myCmd.ExecuteNonQuery();
}
if ((int)returnParam.Value < 1)
{
lblMessage.Text = "Invalid Login!";
return false;
}
else
{
myConn.Close();
return true;
}
}
catch (Exception ex)
{
lblMessage2.Text = ex + "Error Connecting to the database";
return false;
}

}[/B]

PLEASE HELP.

Check to see that the connection string in the web.config is correct. From what i can see from your post sever name and database are not specify and that will definitely raise an error.

This article has been dead for over six months. Start a new discussion instead.