user3349418
user3349418

Reputation: 27

Insert Values to Database Where Users= Input

I have form1 and form2. Form1 is Login and Form2 is Main Menu. I am using (C# Access), and I have fields in my database, Here are my fields (Users, Pass, Player1, Player2).

Now, I want to insert values in player1 and player2 fields to the database with its corresponding Users field.

Note: Users is username.

How to do that? Please help me.

Here's my current code

Form1 f2 = new Form1();
            f2.ShowDialog();
           MessageBox.Show(f2.getTextBoxVal());//This is just a test in getting the Username from Form1 which is the Login form
            using (OleDbConnection con = new OleDbConnection(connParam))
            using (OleDbCommand cmd = new OleDbCommand("select count(*) from data where Users = ?"))
            {
                con.Open();
                cmd.Connection = con;
                cmd.Parameters.AddWithValue("@player1score", player1score);
                cmd.Parameters.AddWithValue("@player2score", player2score);
                object objRes = cmd.ExecuteScalar();
                if (objRes == null || (int)objRes == 0)
                {

                    cmd.Parameters.Clear();
                    cmd.CommandText = "INSERT INTO data (Player1,Player2) values(?, ?) WHERE Users='" + f2.getTextBoxVal() + "')";
                    cmd.Parameters.AddWithValue("@player1score", player1score.Text);
                    cmd.Parameters.AddWithValue("@player2score", player2score.Text);
                    int iRes = cmd.ExecuteNonQuery();//Error Here It says Missing semicolon (;) at end of SQL statement.

                    if (iRes > 0)
                        //errorProvider2.SetError(textBox1, "");
                        //errorProvider3.SetError(textBox1, "User name available!");
                        MessageBox.Show("Scores Save!!");
                }
                else
                {
                    MessageBox.Show("Just a Test!");
                }

                }

In form1 I use to have this code :

public string getTextBoxVal()
{
   return textBox1.Text;
}

This is for getting the text in form1

In form 2 I use to have this code:

Form2 f2 = new Form2();
f2.ShowDialog();
MessageBox.Show( f2.getTextBoxVal());

This is my way to retrieve textbox val using getTextBoxVal() function from **Form1* :

And I use it here:

 cmd.CommandText = "INSERT INTO data (Player1,Player2) values(?, ?) WHERE Users='" + f2.getTextBoxVal() + "')";

However, I cant save the values to the particular username or account that I use to Login.

How to fix this please?

Upvotes: 0

Views: 122

Answers (1)

Pieter21
Pieter21

Reputation: 1845

INSERT does not support or even need WHERE

You don't need it and need to adjust your query accordingly.

cmd.CommandText = "INSERT INTO data (Player1,Player2) values(?, ?)";

INSERT is about adding new entries to the table. If you want to update, please use the UPDATE command. That command supports 'WHERE'.

UPDATE typically looks something like:

UPDATE tablename
   SET col1 = 'val1', col2 = 'val2' ...
 WHERE id = id_value

Furthermore, your first use of cmd doesn't seem right to me. You pass two playerscores, where only one Username is required.

Upvotes: 1

Related Questions