-1

I'm a C# beginner. I have 2 DataBases. One is for when a user registers, the other is for Support section register.

Im trying to to move username, password, email from one DataGridView to the other; and then INSERT it to the DataBase.
I'm also trying to pass the same password decryption to the other database (this way the user will need to register once and access support section with same original registered info).

But, I am getting an error:

Column 'address' cannot be null

and I am unable to think of a code to match the decryption for password column.

private void button3_Click(object sender, EventArgs e)
{
    for (int i = 0; i < dataGridView2.Rows.Count; i++)
    {
        try
        {
            string constring = "datasource=server.server.com;port=0000;username=root;password=root;SSL Mode=None";
            string query = "insert into server.user_account (username, passwd) values (@username,@passwd) ON DUPLICATE KEY UPDATE username = username,passwd=passwd;insert into server.user_email (address) values (@address) ON DUPLICATE KEY UPDATE address = address";
            MySqlConnection conDataBase = new MySqlConnection(constring);
            MySqlCommand cmdDataBase = new MySqlCommand(query, conDataBase);
            MySqlDataReader myReader;

            cmdDataBase.Parameters.AddWithValue("@username", dataGridView2.Rows[i].Cells[0].Value);
            cmdDataBase.Parameters.AddWithValue("@passwd", dataGridView2.Rows[i].Cells[1].Value);
            cmdDataBase.Parameters.AddWithValue("@address", dataGridView2.Rows[i].Cells[2].Value);

            //cmdDataBase.Parameters.Clear();

            conDataBase.Open();
            myReader = cmdDataBase.ExecuteReader();

            while (myReader.Read())
            {

            }
            conDataBase.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

I've tried changing @address to ?address...but still getting same error.

Jimi
  • 29,621
  • 8
  • 43
  • 61

2 Answers2

0
Column 'address' cannot be null

This means your

dataGridView2.Rows[i].Cells[2].Value

is null

so by requirement, if users can give null as address then you have to change the database schema

Here is How do I modify a MySQL column to allow NULL?

otherwise, you have to put a null check in your code.

Mihir Dave
  • 3,954
  • 1
  • 12
  • 28
  • I changed the table in database so it would accept NULL it worked with no errors, now I just need to figure how to INSERT password with same encryption. Thanks! – Omar Mohamed Oct 12 '18 at 12:44
  • Good to know that it helped, can you please elaborate more on insert password with same encryption in your question? Like what are trying to achieve and what you have done so far and what's the road block. – Mihir Dave Oct 12 '18 at 23:36
  • I have 2 databases first database has registered user second database has support registered users Im trying to grab the info of registered users from 1st database and insert it into 2nd database...so the user doesnt need to re-register everything work except for the password. the password from 1st database is encrypted in a way and 2nd database is in a different way. So, whenever i insert the password and attempt to login, it says wrong password – Omar Mohamed Oct 13 '18 at 16:52
  • can you please attached the code where you replicate the data from one database to another ? and what have you used application level encryption or database level encryption, because workaround for both would be different – Mihir Dave Oct 14 '18 at 08:40
0

IF you have an address column in server.user_account :

In your insert query you must bring an adress :

string query = "insert into server.user_account (username, passwd, address) values (@username,@passwd,@address) ON DUPLICATE KEY UPDATE username = username,passwd=passwd;insert into server.user_email (address) values (@address) ON DUPLICATE KEY UPDATE address = address";

Or in your DB management tool set the address column in server.user_account to accept null values.

IF you don't have an address column in server.user_account, then it means that your address variable is NULL, and you need to make sure it is not null before sending it to the string.

For password decryption : if you encrypted it using hash methods like md5 or shaX you can't decrypt it. This is why it's so secure. To verify the user input you have to encrypt it and compare it to the encrypted value stored in your database. (Sorry I don't know much in encryption and security.

Hope it helps you !

Jean-Marc Zimmer
  • 537
  • 6
  • 20