0

I got a question about c# and mysql. I would like to make a very simpel login form that is connected to my local db. I got the connection to work (tested it) but i have a problem with reading my data that is returned from a select.

I'm trying to put an ID into a string so I can display it(this is just for testing). Now I have searched a lot on google and almost everyone has something like this. When I execute it doesn't give error but my sqldatareader finds nothing. In the first if I ask if it has got any rows and there are none.

What am I doing wrong? My username/password do exist in my db.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;


namespace eindwerk
 {
  public partial class LoginForm : Form
   {
    string myConnection = "Server=localhost;Database=mydb;Uid=root;Pwd=root;";
    MySqlCommand cmd;
    MySqlConnection connection;

    public LoginForm()
    {
        InitializeComponent();
        connection  = new MySqlConnection(myConnection);

        connection.Open();
    }

    private void loginForm_Load(object sender, EventArgs e)
    {
        this.Location = new Point((Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2,
                       (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2);
    }

    private void btnLogin_Click(object sender, EventArgs e)
    {

        try
        {
            cmd = connection.CreateCommand();
            cmd.CommandText = "SELECT idlogin FROM login WHERE (username='@username') AND (password='@password') LIMIT 1;";
            cmd.Parameters.AddWithValue("@username", txtbxLoginUsername.Text);
            cmd.Parameters.AddWithValue("@password", txtbxLoginPassword.Text);


            MySqlDataReader rdr = cmd.ExecuteReader();

            rdr.Read();
            if (rdr.HasRows)
            {
                while (rdr.Read())
                {
                    label1.Text = rdr.GetInt32("idlogin").ToString();
                }
            }
            else
            {
                lblLoginError.Visible = true;
            }


            rdr.Close();

        }
        catch {
            lblLoginError.Text = "Nope";
            lblLoginError.Visible = true;
        }


    }
}

}

2
  • 4
    Show your code as a text.. Commented Apr 27, 2014 at 12:48
  • yep. You should use Edit button for that Commented Apr 27, 2014 at 12:52

3 Answers 3

1

You are calling Read() Multiple time. Call the While(Reader.Read()) single time and check the result by if(rdr.HasRows()){} for check result is return or nothing is come in the response.

Sign up to request clarification or add additional context in comments.

8 Comments

How do I check while for 1 time : I did while(rdr.read()) and then my if function, is that correct ?
While(rdr.Read()) only execute if it's have rows and it's run in sequence for every row in the result. Suppose if your select command return many rows then it will be run in while(rdr.Read()) one by one.
@user3578247 The good way is try to check if(rdr.HasRows()) first then call while() then you can run your conditional (if else code) inside he while block.
I've tried both options, but still it returns nothing. I've posted a link to my OneDrive for a screenshot, I can't answer my own question because of reputation points. Maybe you can spot the problem : 1drv.ms/1lX9Xfk
@user3578247 Try ExecuteScalar instead of ExecuteReader()
|
0

You are returning only 1 row, but you are calling Read() twice. Your row is effectively discarded before you look at your data.

2 Comments

I removed my while with the read statement, still it returns nothing. Am I correct to assume that first I read and then i check if Hasrows ?
I personally never used HasRows. If you expect a single row, I would just do something like if (rdr.Read()) { get values } else { no values }
0

After a long search i have found the problem ! In my sql query i put username='@username', there lies the problem. You can't use single quotes !!!. I removed the quotes and it works perfectly.

That is whay you get for trusthing a search result on the third page of google...

Thanks to all !

1 Comment

I had similar problem. Removing the @ from the parameter got it working for me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.