Hello i just started up today with C#,
Im femilair with PHP so i can do some things i have google etc.

But now im stuck.. with this:

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

namespace WindowsApplication2
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();

            username.Text = "";
            password.PasswordChar = '*';

            string loginus = "";
            string loginpas = "";

            loginus = username.Text;
            loginpas = password.Text;

            if (loginus == false)
            {
                string MyConString = "SERVER=localhost;" +
                          "DATABASE=ot;" +
                          "UID=" + loginus + ";" +
                          "PASSWORD=" + loginpas + ";";
                MySqlConnection connection = new MySqlConnection(MyConString);
                MySqlCommand command = connection.CreateCommand();
                MySqlDataReader Reader;
                command.CommandText = "select * from players ORDER BY id";
                connection.Open();
                Reader = command.ExecuteReader();
                while (Reader.Read())
                {
                    string idPlayers = "";
                    idPlayers = Reader.GetString(0) + "\t" + Reader.GetString(1);
                    listBox1.Items.Add(idPlayers);
                }
                connection.Close();
            }
        }



        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
   
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string loginus = "";
            string loginpas = "";

            loginus = username.Text;
            loginpas = password.Text;
        }

    }
}

I made a form with a user/password fields and a logon button,
and a field that reads from DB. Everything did work, but now i want to make the user/password field active to work (to access the db) not sure how yet.. so if you guys can help me with that also it would be greate!

With this code im getting the error for the if statement i made ;)
(not sure how to fix it properly)


Im not sure if some one can help me out

Hi,

In C# if you are gonig to use the == operator its not defined for type string and boll(ie string == bool)but since you are trying to check it the string 'loginus' is not empty you can use one of this options

1. if(loginus != "")
2. if(loginus != string.Empty)
3. if(!string.IsNullOrEmpty())
4. if(string.IsNullOrEmpty() == false)
5. if(string.Count != 0)

All this are valids if statements pick the one you like better.

Regards,
Camilo

Hi,

In C# if you are gonig to use the == operator its not defined for type string and boll(ie string == bool)but since you are trying to check it the string 'loginus' is not empty you can use one of this options

1. if(loginus != "")
2. if(loginus != string.Empty)
3. if(!string.IsNullOrEmpty())
4. if(string.IsNullOrEmpty() == false)
5. if(string.Count != 0)

All this are valids if statements pick the one you like better.

Regards,
Camilo

Camilo big Thanks! now i have another problem.. =)

When im trying to send the information from username/password to my MySQL string it just wont refresh from1() anny idea how i should do it? i have it now like this:

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

namespace WindowsApplication2
{
    public partial class Form1 : Form
    {
        string loginus = "";
        string loginpas = "";

        private void button1_Click(object sender, EventArgs e)
        {
            loginus = username.Text;
            loginpas = password.Text;
        }

        public Form1()
        {
            InitializeComponent();
            if (loginus != string.Empty)
            {
                string MyConString = "SERVER=localhost;" +
                          "DATABASE=ot;" +
                          "UID=" + loginus + ";" +
                          "PASSWORD=" + loginpas + ";";
                MySqlConnection connection = new MySqlConnection(MyConString);
                MySqlCommand command = connection.CreateCommand();
                MySqlDataReader Reader;
                command.CommandText = "select * from players ORDER BY id";
                connection.Open();
                Reader = command.ExecuteReader();
                while (Reader.Read())
                {
                    string idPlayers = "";
                    idPlayers = Reader.GetString(0) + "\t" + Reader.GetString(1);
                    listBox1.Items.Add(idPlayers);
                }
                connection.Close();
            }
        }



        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
   
        }



    }
}

Yes

The code whe you are populating the LisBox is the Constructor and only executes whe you crate a new objec(when you use the 'new' keyword) and at this point the 'logingus' is empty so it would not 'enter' the 'if'. A solution is to change that code to the button1Click method.

Camilo

This article has been dead for over six months. Start a new discussion instead.