简体   繁体   中英

User name can not contain null value

I am trying to insert two values to mysql database through two textboxes using WPF and C#. I am connecting to the database successfuly but when I try to insert the data I get error: Column "user_name" can not be null. What is really confusing be is that I am entering data in the first and the second textboxes. It seems that the data to inserted in the textboxes is not passed. My question is do you know where is the problem and how to fix it?

PS: my database is very simple contain id as int16 auto incremented, name as varchar100 and user_password as varchar100

Here is my code:

    private void button1_Click(object sender, EventArgs e)
    {
        MySqlConnection con = new MySqlConnection("host=tara.rdb.superhosting.bg;user=sozopouk;password=27051996;database=sozopouk_test2;");
        MySqlDataAdapter da = new MySqlDataAdapter();
        da.InsertCommand = new MySqlCommand("Insert into niki2 values (@id,@name, @user_password)", con);

        da.InsertCommand.Parameters.Add("@name", MySqlDbType.VarChar).Value = textBox1.Text;

        da.InsertCommand.Parameters.Add("@user_password", MySqlDbType.VarChar).Value=textBox2.Text;

        con.Open();
        da.InsertCommand.ExecuteNonQuery();
        con.Close();
    }

Guys after I did couple of changes I am getting this error message:

Unknown column 'name' in 'field list'

Here is edited code:

private void button1_Click(object sender, EventArgs e)
        {
            MySqlConnection con = new MySqlConnection("host=tara.rdb.superhosting.bg;user=sozopouk;password=27051996;database=sozopouk_test2;");
            MySqlDataAdapter da = new MySqlDataAdapter();
            da.InsertCommand = new MySqlCommand("INSERT INTO niki (name, user_password) VALUES (@name, @user_password)", con);

            da.InsertCommand.Parameters.Add("@name", MySqlDbType.VarChar).Value = textBox1.Text;

            da.InsertCommand.Parameters.Add("@user_password", MySqlDbType.VarChar).Value=textBox2.Text;

            con.Open();
            da.InsertCommand.ExecuteNonQuery();
            con.Close();
        }

When I replace name with user_name and try to insert data I get an error ** Column "user_name" cannot be null**

The affected line is :

da.InsertCommand.ExecuteNonQuery();

Do you have any idea how to solve it?

Look at your code:

da.InsertCommand.Parameters.Add("@user_id", MySqlDbType.Int16).Value = "";
da.InsertCommand.Parameters.Add("@user_name", MySqlDbType.VarChar).Value = "";
da.InsertCommand.Parameters.Add("@user_password", MySqlDbType.VarChar).Value = 
   textBox2.Text;

Now look at what you've said:

It seems that the data to inserted in the textboxes is not passed.

Look at the code again. Look at the value being used for user_name (and indeed user_id ). How is that using a textbox value? How are you expecting the data from textBox1 to get to the database when your code never mentions it?

Additionally, as sblom mentions in comments, I'd encourage you to use explicit column names - at which point you won't need to specify anything for user_id , which I assume is an auto-generated identity column anyway. (It therefore doesn't make sense to try to insert an empty value into it.) It's not clear why you're trying to use an empty string as a value for an Int16 column, either...

EDIT: For your edited code:

da.InsertCommand = new MySqlCommand(
   "INSERT INTO niki (name, user_password) VALUES (@name, @user_password)", con);

Given your first error, the column is user_name , not name ...

Can you just check what table you want to use exactly?

in your actual question it was

 da.InsertCommand = new MySqlCommand("Insert into **niki2** values (@id,@name, @user_password)", con);

and in updated question the table name is changed to

da.InsertCommand = new MySqlCommand("INSERT INTO **niki** (name, user_password) VALUES (@name, @user_password)", con);

I feel you have two tables you are playing with and are confused

seems like name is in niki2 and user_name is in niki probably your actual table.

I am sure no one can answer with this much ambiguity

Try

For Table niki

        da.InsertCommand = new MySqlCommand("INSERT INTO niki (user_name, user_password) VALUES (@user_name, @user_password)", con);

For Table niki2

        da.InsertCommand = new MySqlCommand("INSERT INTO niki2 (name, user_password) VALUES (@name, @user_password)", con);

OR Reversal

Luck

You haven't specified in the insert statement in which fields the values should be stored, so the values will end up in the first three fields in the order that you specified, regardless of their names. I guess that the user name field is the fourth, so it doesn't get a value, thus being null.

Specify which fields should be used:

da.InsertCommand = new MySqlCommand("INSERT INTO niki (user_id, user_name, user_password) VALUES (@user_id,@user_name, @user_password)", con);

finally I find the problem causing me this exception. As you can see my old code:

da.InsertCommand = new MySqlCommand("INSERT INTO niki (name, user_password) VALUES (@name, @user_password)", con);

da.InsertCommand.Parameters.Add("@name", MySqlDbType.VarChar).Value = textBox1.Text;

da.InsertCommand.Parameters.Add("@user_password", MySqlDbType.VarChar).Value=textBox2.Text;

enter code here

What I did was to replace the

@ with ?

And basically my code looked like :

da.InsertCommand = new MySqlCommand("INSERT INTO niki (name, user_password) VALUES (?name, ?user_password)", con);

da.InsertCommand.Parameters.Add("?name", MySqlDbType.VarChar).Value = textBox1.Text;

da.InsertCommand.Parameters.Add("?user_password", MySqlDbType.VarChar).Value=textBox2.Text;

enter code here

Thanks to all who involved in the discussion!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM