3 Replies - 1629 Views - Last Post: 10 August 2012 - 04:21 AM Rate Topic: ***** 1 Votes

#1 TomL  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-August 12

Login using ComboBox?

Posted 10 August 2012 - 03:19 AM

Hi all, first post here. I'm having some trouble with a login form. I have studied c# in the past but have recently just got back into using it.

Basically, I am wishing to login with the Username that the user selects from a ComboBox? I can populate the ComboBox but is rattling my brain as to how you can login using data from the ComboBox.

At present, I have a simple login script with text boxes. But, I would like to change this as users at the moment don't have a password set.

  private void btnLogin_Click(object sender, EventArgs e)
        {
            //New SQL Command for logon.
            SqlCommand login = new SqlCommand("Select * from Users where Username = '" + textUser.Text + "' and Password = '" + textPassword.Text + "'", sql);
            sql.Open();
            SqlDataReader reader = login.ExecuteReader();
            if (reader.Read())
            {
                MessageBox.Show("Login Successful");
                Main form = new Main();
                form.Show();
            }
            else
            {
                MessageBox.Show("Incorect Login Information");
            }
        }


Here is my code for the login using just the text boxes.

SqlCommand sc = new SqlCommand("SELECT Username from Users", sql);
            SqlDataAdapter da = new SqlDataAdapter(sc);
            da.SelectCommand.CommandText = sc.CommandText.ToString();
            DataTable dt = new DataTable();

            da.Fill(dt);
            comboBox1.DataSource = dt;

            comboBox1.DisplayMember = "Username";
            comboBox1.ValueMember = "Username";


And here is the code to populate the ComboBox.

Any help would be much appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Login using ComboBox?

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3650
  • View blog
  • Posts: 11,419
  • Joined: 05-May 12

Re: Login using ComboBox?

Posted 10 August 2012 - 03:28 AM

What's the problem?

Just like when you used a TextBox for the username, you'll still do the same thing with a ComboBox: you'll need to use the Text property. In the case of the ComboBox, it'll be ComboBox.Text: http://msdn.microsof...bobox.text.aspx
Was This Post Helpful? 1
  • +
  • -

#3 TomL  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-August 12

Re: Login using ComboBox?

Posted 10 August 2012 - 04:19 AM

Of course it is. Schoolboy error! Fixed now, thank you :)
Was This Post Helpful? 0
  • +
  • -

#4 asmat.jamal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-August 12

Re: Login using ComboBox?

Posted 10 August 2012 - 04:21 AM

u can use following property
combobox.selecteditem.text;
this will give you selected username
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1