The visual c# code returns no data exists for row/column on select * from table even though the data exists
OleDbDataReader dataReader;
OleDbCommand cmd1 = new OleDbCommand("SELECT * FROM username",conn);
MessageBox.Show(conn.ConnectionString);
try
{
//Execute the command store in cmd1 and save the return value in datareader
dataReader = cmd1.ExecuteReader();
MessageBox.Show(dataReader.GetString(0));
if (dataReader.Read())
{
/*if (dataReader.GetString(0) == txtLoginPass.Text)
{
frmWelcome fm = new frmWelcome(this);
txtRegisterUserName.Text = "";
txtRegisterPass.Text = "";
txtLoginUserName.Text = "";
txtLoginPass.Text = "";
txtRegisterConfirmPass.Text = "";
txtLoginUserName.Focus();
this.Hide();
fm.Show();
}*/
if (dataReader.GetString(0) == "admin")
{
MessageBox.Show("Welcome Admin");
}
else if(dataReader.GetString(0) == "under")
{
MessageBox.Show("Welcome.......");
}
else
{
MessageBox.Show("Invalid Username/Password!");
usernameTextBox.Text = "";
passwordTextBox.Text = "";
usernameTextBox.Focus();
}
}
else
{
MessageBox.Show("Invalid Username/Password!");
usernameTextBox.Text = "";
passwordTextBox.Text = "";
usernameTextBox.Focus();
}
dataReader.Close();
}
catch (Exception e1)
{
MessageBox.Show(e1.Message);
}

1 answers
Initially, the record pointer is just before the first record so you need to call the Read() method before you can access the data in the first record.
So try it like this:
answered one year ago by:
17279
412
What ecatly are we doing here? I mean I want to know the implication of this code/effort.
17279
The way I read it is that the OP is trying to get the first row from the database and see whether the first column contains 'admin', 'under' or something else and issue the appropriate message. This, of course, is just a test as eventually the OP will want to check the user's name and password with what's in the database and proceed accordingly.