Dong Lam Trien

Dong Lam Trien

  • 828
  • 986
  • 147.7k

Why can I save the error information into the table of SQL ?

Sep 28 2015 11:46 PM

Why I can not save the error information in the table: TABERRORINFO. Suppose I have two: TABCUSTOMER and TABERRORINFO.

private void cmdSave_Click(object sender, EventArgs e)

{

strSQL = "INSERT INTO TABCUSTOMER(IDCUSTOMER, FULLNAME, ADDRESS, IMAGE) " +

"VALUES (@IDCUSTOME, @FULLNAME, @ADDRESS, @ImageData);";

SqlCommand cmd = new SqlCommand(strSQL, ClsConnection.objConnect);

try

{

cmd.Parameters.AddWithValue("@IDCUSTOMER", txtIDCustomer.Text);

cmd.Parameters.AddWithValue("@FULLNAME", txtFullName.Text);

cmd.Parameters.AddWithValue("@ADDRESS", txtAddress.Text);

//byte[] ImageData = ReadFile(sPath);

cmd.Parameters.Add(new SqlParameter("@ImageData", (object)ImageData));

if (ClsConnection.objConnect.State != System.Data.ConnectionState.Open)

{

ClsConnection.objConnect.Open();

}

cmd.CommandType = CommandType.Text;

cmd.ExecuteNonQuery();

// Close database

ClsConnection.objConnect.Close();

}

catch (SqlException ex)

{

ClsConnection.objConnect.Close();

string sConError = "Save Messager Customer 1: " + ex.Message;

SaveMessageErrSQL(sConError, strSQL);

}

}

private void SaveMessageErrSQL(string MessErr, string SqlErr)

{

string sSQL = "UPDATE TABERRORINFO SET ";

sSQL = sSQL + " ERRDATE = @NgayErr";

sSQL = sSQL + ", ERRMESSAGE = @MesaErr";

sSQL = sSQL + ", ERRSQL = @SQLErr";

sSQL = sSQL + " WHERE USERNAME = N'" + sUSERNAME + "';";

Debug.Print(sSQL);

SqlCommand cmd = new SqlCommand(sSQL, objConnect);

try

{

cmd.Parameters.AddWithValue("@NgayErr", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

cmd.Parameters.AddWithValue("@MesaErr", MessErr);

cmd.Parameters.AddWithValue("@SQLErr", SqlErr);

cmd.CommandType = CommandType.Text;

cmd.ExecuteNonQuery();

objConnect.Close();

DevExpress.XtraEditors.XtraMessageBox.Show("Saved" + MessErr);

}

catch (SqlException ex)

{

objConnect.Close();

//MessageBox.Show("SaveMessageErrSQL l?i: " + sSQL);

MessageBox.Show("No Save Message Errer into SQL: " + ex.Message);

}

}


Answers (2)