[HELP]C# MySQL Command insert if not exsist?

09/03/2009 11:54 Basser#1
Code:
MySqlCommand Command = new MySqlCommand("INSERT INTO accounts(AccountID,LogonType) VALUES ('" + fillBox.Text + "','3')", Connection);
Now, I want it to check if the value of fillBox.Text doesn't excist before making the account, so It wont create the same account. I tried making it in a try, that didn't help, it just make several accounts with the same name.

~Bas
09/03/2009 19:59 PeTe Ninja#2
Umm.. I dont know if this will help, check out the register page?

I did this before but i dont remember how... I made a fully working accoun register form, i dont have it anymore though sorry.
09/03/2009 20:21 Basser#3
I did, but the $check2 part is hard to translate to C#..
09/03/2009 20:37 _Emme_#4
Here's a crappy way of doing it:


Quote:
MySqlCommand cmd = new MySqlCommand("SELECT * FROM `accounts` WHERE `AccountID` = "'" + fillBox.Text + "'", Connection);
MySqlDataReader rdr = cmd.ExecuteReader();
bool exist = false;
if (rdr.Read())
exist = true;

if (!exist)
{
Your code goes here.
}
09/03/2009 20:38 Basser#5
Thanks a lot, testing this now.
I'll edit this.

Edit:

Didn't work.
09/03/2009 20:52 bisiol#6
Code:
        private bool Check(string acc)
        {
            bool exists = false;
            MySqlCommand Command = new MySqlCommand("SELECT * FROM `accounts` WHERE `AccountID` = \"" + acc + "\"", Connection);
            MySqlDataReader DR = Command.ExecuteReader(CommandBehavior.CloseConnection);
            while (DR.Read())
            {
                if (Convert.ToString(DR["AccountID"]) == acc)
                    exists = true;
            }
            DR.Close();
            Command.Dispose();
            return exists;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string acc = fillBox.Text;
            if (acc != "")
                if (!Check(acc))
                {
                    MySqlCommand Command = new MySqlCommand("INSERT INTO accounts(AccountID,LogonType) VALUES ('" + acc + "','3')", Connection);
                    Command.ExecuteNonQuery();
                    Command.Connection.Close();
                    Command.Connection.Dispose();
                    Command.Dispose();
                    MessageBox.Show("AccountID created");
                }
                else
                {
                    MessageBox.Show("AccountID already taken");
                }
        }
I hope it helps.
09/03/2009 20:56 Basser#7
#request close

bisiol solved the problem.
Thnx a lot.
09/03/2009 22:00 PeTe Ninja#8
Nice bisol
09/04/2009 00:11 tanelipe#9
#Closed