V
VA_er
Guest
Where is the error? Newbie to C#, occasional coding, not familiar with C# syntax.
The access file does not have password.
Thanks.
private void btnConnect_Click(object sender, EventArgs e)
{
GlobalVariables.Username = lblUsername.Text;
GlobalVariables.ConnectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)}; Dbq=" + @"My Access File path;";
DatabaseCn = new OdbcConnection(GlobalVariables.ConnectionString);
bool SuccessfulConnection;
SuccessfulConnection = judge(lblUsername.Text);
if (SuccessfulConnection == false)
{
MessageBox.Show("Connection failed. You are not in User Access table.", "Error Message");
}
if(SuccessfulConnection==true)
{
DatabaseCn.Close();
FormInterface fInterface = new FormInterface();
fInterface.Show();
this.Close();
}
}
// Check if lblUsername.Text is in the Access table User_access fieldname UserName
public bool judge(string UserName)
{
using (OdbcConnection myConn = new OdbcConnection(GlobalVariables.ConnectionString))
{
myConn.Open();
using (OdbcCommand cmd = new OdbcCommand { Connection = myConn })
{
cmd.CommandText = "SELECT UserName FROM User_access WHERE UserName = @GlobalVariables.UserName";
cmd.Parameters.AddWithValue("@GlobalVariables.Username", UserName);
var reader = cmd.ExecuteScalar();
return reader != null;
}
}
}
Continue reading...
The access file does not have password.
Thanks.
private void btnConnect_Click(object sender, EventArgs e)
{
GlobalVariables.Username = lblUsername.Text;
GlobalVariables.ConnectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)}; Dbq=" + @"My Access File path;";
DatabaseCn = new OdbcConnection(GlobalVariables.ConnectionString);
bool SuccessfulConnection;
SuccessfulConnection = judge(lblUsername.Text);
if (SuccessfulConnection == false)
{
MessageBox.Show("Connection failed. You are not in User Access table.", "Error Message");
}
if(SuccessfulConnection==true)
{
DatabaseCn.Close();
FormInterface fInterface = new FormInterface();
fInterface.Show();
this.Close();
}
}
// Check if lblUsername.Text is in the Access table User_access fieldname UserName
public bool judge(string UserName)
{
using (OdbcConnection myConn = new OdbcConnection(GlobalVariables.ConnectionString))
{
myConn.Open();
using (OdbcCommand cmd = new OdbcCommand { Connection = myConn })
{
cmd.CommandText = "SELECT UserName FROM User_access WHERE UserName = @GlobalVariables.UserName";
cmd.Parameters.AddWithValue("@GlobalVariables.Username", UserName);
var reader = cmd.ExecuteScalar();
return reader != null;
}
}
}
Continue reading...