Get a value from the database in C #

advertisements

Afternoon all!

So, I'm trying to get a value from my database and this is my sample code:

MySqlCommand cmd = new MySqlCommand("Select * from tbluser where userName ='" + txtUser.Text + "' and userPass ='" + txtPass.Text + "'", con);

                con.Open();
                reader = cmd.ExecuteReader();
                int count = 0;

                while (reader.Read())
                {
                    count = count + 1;
                }

                if (count == 1)
                {

                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                lblID.Text = reader(0);
                            }
                         }

                    MessageBox.Show("You have successfully logged in!");

                    homeMain homeMain = new homeMain();

                    homeMain.Passvalue = txtUser.Text;
                    homeMain.Passvalue = lblID.Text;
                    homeMain.Show();
                    this.Hide();

                }

What the code is trying to achieve is, when I press LOG-IN, it searches the database equal to the txtUser and then displays the id on the lbl.Text. I got squiggly lines under reader(0). What seems to be the problem?


Try this alternatively: lblID.Text = reader.GetValue(0).ToString();