[Solved] MySql Connection Error: The sequence contains multiple matching elements

comunidadmexicanaroma
 
on Jan 31, 2022 03:04 AM
1493 Views

Hi all,

I have this never seen error using C# MySql:

System.InvalidOperationException: The sequence contains multiple matching elements

Any suggestion?

Line error

command.Connection.Open();

Webconfig

<add name="cn" connectionString="Datasource=myServer;Database=myDB;uid=XXXX;Password=XXXX;Allow User Variables=True;" providerName="MySql.Data.MySqlClient" />

Masterpage

string username = Page.User.Identity.Name.ToLower(); 
sql = @String.Format(" SELECT * FROM myTable WHERE tusername = ?; "); 
using (MySqlConnection myConnectionString =
    new MySqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
{
    using (MySqlCommand command =
        new MySqlCommand(sql, myConnectionString))
    {
        try
        {
            command.Parameters.AddWithValue("param1", username.ToString().ToUpper());
            command.Connection.Open(); 
            using (MySqlDataReader reader = command.ExecuteReader())
            {
                if (reader.HasRows)
                {
                    Container.sName = reader["Name"].ToString();
                    lb.Text = "Welcome <b>" + Container.sName;
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "alert('No user';", true);
                }
            }
        }
        catch (Exception ex)
        {
            throw new ApplicationException("operation failed!", ex);
        }
        finally
        {
            command.Connection.Close();
        }
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Feb 02, 2022 12:40 AM