sakusa1 sakusa1 - 4 years ago 183
C# Question

MySqlexception was unhandled, Fatal error encountered during command execution

i try to add some values to table(tbllogs) after loging in, but i got an error

here's my code

if (ctr == 1)
{
Data.con.Open();
MetroMessageBox.Show(this, "Login Success!", "Welcome!", MessageBoxButtons.OK, MessageBoxIcon.Information);
MySqlDataAdapter da = new MySqlDataAdapter("Select * From dbinfo.tbluser where UserName = '" + txtUser.Text + "' and UserPassword = '" + txtPass.Text + "'", Data.con);
DataTable dt = new DataTable();
da.Fill(dt);
Data.UserID = dt.Rows[0][0].ToString();
Data.UserName = dt.Rows[0][1].ToString();
Data.UserLevel = dt.Rows[0][3].ToString();
string SaveStr = "Insert into dbinfo.tbllogs (LogsUser, LogsPassword) Values (@LogsUser, @LogsPassword)";
MySqlCommand SaveCmd = new MySqlCommand(SaveStr, Data.con);
//SaveCmd.Parameters.AddWithValue("@LogsID", Data.UserID = dt.Rows[0][0].ToString());
SaveCmd.Parameters.AddWithValue("@LogsName", txtUser.Text);
SaveCmd.Parameters.AddWithValue("@LogsPassword", txtPass.Text);
//SaveCmd.Parameters.AddWithValue("@LogsLevel", Data.UserLevel = dt.Rows[0][3].ToString());
//SaveCmd.Parameters.AddWithValue("@LogsDateIN", timelabel.Text);
SaveCmd.ExecuteNonQuery();
Data.con.Close();
LoadData();
Menu frmMenu = new Menu();
frmMenu.Show();
this.Hide();
}


i tried changing the code
SaveCmd.Parameters.AddWithValue("@LogsName", txtUser.Text);
to
SaveCmd.Parameters.AddWithValue("@LogsName", Data.Password = dt.Rows[0][2].ToString()));
but didn't work either.

i will provide more code if this isn't enough, Thanks in advance!

Answer Source

Change this line:

   SaveCmd.Parameters.AddWithValue("@LogsName", txtUser.Text);

Into this:

 SaveCmd.Parameters.AddWithValue("@LogsUser", txtUser.Text);

Also, as others have mentioned, you should not log the password as plain text.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download