0

How can I retrieve or display a name from a MySQL database? I have a database table where the columns are fID, USERNAME, PASSWORD and NAME. I am using Form 1 as a log-in form. Now I want to display the name of the user on my Form 2 Label. How can I do that?

I'm stuck here:

void log_in_btn_Click(object sender, EventArgs e)
{
    try
    {
            RF.openConnection("");
            RF.selectCommand("SELECT * FROM sign_up_table WHERE USERNAME = '" + txt_Username.Text + "' AND PASSWORD = '" + txt_Password.Text + "'");

            if (RF.result.Rows.Count == 1)
            {
                this.Visible = false;
                //here i want to display the name of the user but i dont know how so I'm just displaying the username.
                frm2.lbl_users_name.Text = txt_Username.Text;
                frm2.ShowDialog();
            }
            else
            {
                MessageBox.Show("Incorrect\nUsername or \nPassword!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            RF.closeConnection("");
    }
    catch (NullReferenceException ex)
    {
        MessageBox.Show(ex.Message);
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
newby
  • 19
  • 1
  • 8
  • 4
    First of all, learn to use [parameterized queries](http://dev.mysql.com/doc/connector-net/en/connector-net-tutorials-parameters.html), you can avoid [SQL Injection](https://technet.microsoft.com/en-us/library/ms161953(v=sql.105).aspx) – DonBoitnott Feb 04 '15 at 11:59
  • 2
    Second of all, don't store your passwords as a plain text. http://stackoverflow.com/questions/1054022/best-way-to-store-password-in-database – Soner Gönül Feb 04 '15 at 12:00
  • `frm2.lbl_users_name.Text = RF.result.Rows[0]["Name"].ToString()`...but first, fix the rest of that! – DonBoitnott Feb 04 '15 at 12:03
  • Can you clarify your question. Your title suggests you're struggling to fetch data from the database but within your question you mention you want to display the logged in user's username on a second form. Which are you stuck on? – Greg B Feb 04 '15 at 12:03
  • No, I said I want to display the name of the user on a the second form, because my form 1 is my log-in form and i don't know how to display the name of the user in form2 after logging in. – newby Feb 04 '15 at 12:09
  • Soner Gönül - thank man! it works ;D – newby Feb 04 '15 at 12:13

1 Answers1

0

Try this code:

Public void log_in_btn_Click(object sender, EventArgs e)
{
    try
    {
        con.open();
        String strQuery = "SELECT * FROM sign_up_table WHERE USERNAME = '" + txt_Username.Text + "' AND PASSWORD = '" + txt_Password.Text + "'";
        sqlcommand cmd = new sqlcommand(strQuery,con);
        sqlDataAdapter adpt = new sqlDataAdapter(cmd);
        Datatable dt = new Datatable();
        adpt.Fill(dt);
        if (dt.Rows.Count == 1)
        {
            frm2.lbl_users_name.Text = dt.Rows[0]["NAME"].ToString();
            frm2.ShowDialog();
        }
        else
        {
            MessageBox.Show("Incorrect\nUsername or \nPassword!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        con.close();
    }
    catch (NullReferenceException ex)
    {
        MessageBox.Show(ex.Message);
    }
}
vignesh
  • 97
  • 5