Click here to Skip to main content
15,901,853 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when i try start my project in VS2017 show this problem, i think that the problem are in my code of insert, sorry but i dont write well english, I'm pulling the connection string of a json, because the web.config is no longer there, I do not think that's the problem because the chain brings it normal and all parameters equal, leave my code, thanks.

 public class personaDAO
{
        public static int Add(mPersona obj)
        {
            int Res = 0;
            string query = "INSERT INTO `persona` (`nombre`,`edad`) values(?nombre,?edad);";
            Dictionary<string, object> Parameters = new Dictionary<string, object>();
            Parameters.Add("?nombre", obj.Nombre);
            Parameters.Add("?edad", obj.Edad);
            try
            {
                //string cadena = connection.ConnectionString;
                Res = SQL_Queries.Query_Execute(query, connection.ConnectionString, Parameters);
            }
            catch (Exception ex) { Console.Write(ex.Message); }
            return Res;
        }
    }
}


What I have tried:

ya intente cambiar las dll que tiene el proyecto, igual intente cambiando la cadena de conexión muchas veces.
Google Translate:
already try to change the dll that the project has, just try changing the connection string many times.
Posted
Updated 28-Sep-21 23:46pm
v2
Comments
[no name] 16-Jan-19 12:51pm    
Maybe you will tell us:
a.) What is the text of the exception
b.) What is the type of SQL_Queries
Member 14120185 16-Jan-19 13:21pm    
this is the exception "MySql.Data.MySqlClient.Replication.ReplicationManager threw an exception" and el type of sql_queries, is one that
the company developed it, i do stays of university
Member 14120185 16-Jan-19 13:23pm    
the exception complete is "The type initializer for 'MySql.Data.MySqlClient.Replication.ReplicationManager' threw an exception."
Richard Deeming 16-Jan-19 13:47pm    
Check the InnerException, which will contain the actual details of the error.
Member 14120185 16-Jan-19 14:37pm    
I'm working on mvc c # I can not or try to change the "Exception", for the "InnerException", and I do not get anything. Visual does not recognize the InnerException.
public static int Add(mPersona obj)
{
int Res = 0;
string query = "INSERT INTO persona (nombre, edad) values('?nombre','?edad');";
Dictionary<string, object> Parameters = new Dictionary<string, object>();
Parameters.Add("?nombre", obj.Nombre);
Parameters.Add("?edad", obj.Edad);
try
{
//string cadena = connection.ConnectionString;
Res = SQL_Queries.Query_Execute(query, connection.ConnectionString, Parameters);
}
//dont work with innerException
catch (InnerException ex) { Console.Write(ex.Message); }
return Res;
}

I don't use MySQL but with a bit of googling it looks like you have two issues.
1. You need to use @ not ? to describe your parameters in your query
2 your parameters are being defined as Dictionary object and they should be of a type cmd.Parameters
 
Share this answer
 
Comments
Member 14120185 16-Jan-19 14:32pm    
podrías dejar ejemplos de esto, por favor, no entendí donde irían los @ y lo de los parámetros menos xD, este es el primer proyecto en donde me sale este error, como quitaron el web.config pues me encentre en un problema hasta que llegue aquí.
RmcbainTheThird 16-Jan-19 15:59pm    
ok here is an example from one of my projects. I am going to assume that MySQL and sqlserver have similar layouts
public bool UpdateInfoCenter(InfoCenterParent icData)
{
bool retVal = true;
string sqlStr = "UPDATE [dbo].[InfoCenterParent]"
+ " SET[ListOrder] = @ListOrder"
+ ",[DisplayText] = @DisplayText"
+ ",[Description] = @Description"
+ ",[ImageFile] = @ImageFile"
+ ",[ChildDisplayOrder] = @ChildDisplayOrder"
+ ",[LastUpdatedBy] = @LastUpdatedBy"
+ " WHERE ID = @icID";
List<sqlparameter> parameters = new List<sqlparameter>
{
new SqlParameter {ParameterName = "@icID", SqlDbType = SqlDbType.Int, Value = icData.ID},
new SqlParameter {ParameterName = "@ListOrder", SqlDbType = SqlDbType.Int, Value = icData.ListOrder},
new SqlParameter {ParameterName = "@DisplayText", SqlDbType = SqlDbType.VarChar,Value = icData.DisplayText},
new SqlParameter {ParameterName = "@Description", SqlDbType = SqlDbType.VarChar,Value = icData.Description},
new SqlParameter {ParameterName = "@ImageFile", SqlDbType = SqlDbType.VarChar,Value = icData.ImageFile},
new SqlParameter {ParameterName = "@ChildDisplayOrder",SqlDbType = SqlDbType.VarChar,Value = icData.ChildDisplayOrder},
new SqlParameter {ParameterName = "@LastUpdatedBy", SqlDbType = SqlDbType.VarChar,Value = icData.LastUpdatedBy}
};
try
{
retVal = true;
DataAccess.ExecuteSQL(_connStr, sqlStr, parameters);
}
catch (Exception ex)
{
retVal = false;
FrameworkLogger.Error("Failed to upadte infocenter parent", null, ex);
}
return retVal;
}
this is the method DataAccess.ExecuteSql:

public static void ExecuteSQL(string connStr, string sqlStr, List<sqlparameter> parameters = null)
{
try
{
//using (new Impersonator("ZS9ADAU", "Corp", pword))
{

using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
SqlCommand cmd = new SqlCommand(sqlStr, conn);
if (parameters != null)
{
cmd.Parameters.AddRange(parameters.ToArray());
}
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
FrameworkLogger.Error("Failed to execute sql staement ", null, ex);
throw ;
}
}
My reason for this issue was incompatibility between MySql.Data.dll and .Net framework version. Once I found lates version of MySql.Data.dll for .NET 5 (in my particular case), this issue gone. Prior to change dll I gave a try to various suggestions within and without config file, with no luck. You don''t need simply "replace dll" you need to find exact MySQL library that match your execution environment. After this exception won't fire anymore.
 
Share this answer
 
I pasting here for basic insert operation using mysql db

public static string AddAusSururb()
        {
            string connstr = @"server=" + GetAppSetting(AppSettings.serverName) + ";database=" + GetAppSetting(AppSettings.databaseName) + ";userid=" + GetAppSetting(AppSettings.userID) + ";password=" + GetAppSetting(AppSettings.dbPassword) + ";CHARSET=" + GetAppSetting(AppSettings.AcceptCharset) + ";convert zero datetime=" + GetAppSetting(AppSettings.convertDatetime) + "";
            MySqlConnection conn = null;
            MySqlDataReader rdr = null;
            string status = string.Empty;
            try
            {
                string stm = "SELECT Id FROM aussuburb ORDER BY Id DESC LIMIT 1;";
                var sql = "INSERT INTO aussuburb(Id, Postcode, Suburb, State)VALUES(@ID,@Postcode,@Suburb,@State);";
                conn = new MySqlConnection(connstr);
                conn.Open();
                MySqlCommand cmd = new MySqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = stm;
                int LastID = Convert.ToInt32(cmd.ExecuteScalar());
                cmd.CommandText = sql;
                cmd.Prepare();
                cmd.Parameters.AddWithValue("@ID", LastID + 1);
                cmd.Parameters.AddWithValue("@Postcode", "TEST");
                cmd.Parameters.AddWithValue("@Suburb", "TEST");
                cmd.Parameters.AddWithValue("@State", "TEST");
                cmd.ExecuteNonQuery();
                status = "success";
            }
            catch (MySqlException ex)
            {
                status = "failed" + ex.ToString();
            }
            finally
            {
                if (rdr != null) { rdr.Close(); }
                if (conn != null) { conn.Close(); }
            }
            return status;
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900