Eric March

Eric March

  • 934
  • 843
  • 1.4k

Error = trying to read when no Data present.  code below

Mar 21 2024 7:21 AM

Error is trying to read when no Data present.  Here is the source code:

public partial class FormUser : Form
{
    static string initString = "Data Source=Odin\\SQLExpress; Persist Security Info=False;User ID=sa;"
            + "password=1ntelPWD!;Initial Catalog=membership"; DataSet dsUser = new DataSet();
    SqlConnection cs = new SqlConnection(initString);
    SqlDataAdapter daUser = new SqlDataAdapter();
    BindingSource tblContactBS = new BindingSource();
private void getLoggedOnID()
{
    dsUser.Clear();
    try
    {
        string s2 = tBoxUID.Text;
        string s1 = "SELECT * FROM tbUser WHERE UID = \'" + s2 + "\'";
        MessageBox.Show("s1 = " + s1);

        SqlCommand cmd = new SqlCommand(s1, cs);
        cs.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        usr.LoggedOnID = reader.GetInt32(0);
        usr.UID = reader.GetString(1);
        usr.PWD = reader.GetString(2);
        usr.PWDHint = reader.GetString(3);
        usr.DisplayName = reader.GetString(4);
        usr.Email = reader.GetString(5);
        usr.IsLoggedOn = true;

        reader.Close();
        cs.Close();

 


Answers (3)