Example #1
0
 //Insert message into the database
 public void ChatSendMessage(string chatMessageInsert, string username)
 {
     Models.Database d = Models.Database.CurrentInstance;
     try
     {
         //Initialize Command
         MySqlCommand cmd = new MySqlCommand(queryString, d.conn);
         //Open connection
         d.OpenConnection();
         //Set date time
         DateTime date = DateTime.Now;
         string   dateStr, timeStr, dateTimeStr;
         dateTimeStr = date.ToString("dd/MM/yyy hh:mm tt");
         dateStr     = date.ToString("dd/MM/yyyy");
         timeStr     = date.ToString("hh:mm tt");
         //Encode msg
         chatMessageInsert = encInit.EncodeStuff(chatMessageInsert);
         //Encrypt msg
         chatMessageInsert = aes_obj.AesEncrypt(chatMessageInsert);
         //Insert query
         queryString     = "INSERT INTO dububase.chat(chatMessage, chatDate, chatTime, username) VALUES(@sendmessage, @chatdate, @chattime, @username)";
         cmd.CommandText = queryString;
         //Add parameters
         cmd.Parameters.AddWithValue("@sendmessage", chatMessageInsert);
         cmd.Parameters.AddWithValue("@chatdate", dateStr);
         cmd.Parameters.AddWithValue("@chattime", dateTimeStr);
         cmd.Parameters.AddWithValue("@username", username);
         cmd.ExecuteNonQuery();
     }//Exception
     catch (System.Data.SqlClient.SqlException ex)
     {
         string errorMsg = "Error";
         errorMsg += ex.Message;
         throw new Exception(errorMsg);
     }
     finally
     {
         //Close connection
         d.CloseConnection();
     }
 }