"Item has already been added. Key in dictionary: '1252' Key being added: '1252'"

i have 2 proget
1 work correcly
the second have the same istruction but i receive error "Item has already been added. Key in dictionary: '1252' Key being added: '1252'"

thi is my code

        SqlConnection conn = new SqlConnection(sdc);
        conn.Open();

        cmdText = "SELECT *  FROM [database].[dbo].[Table]";
        SqlCommand cmd = new SqlCommand(cmdText, conn);
        string errore = string.Empty;
        SqlDataReader r = null;
        try
        {
            r = cmd.ExecuteReader();
        }
        catch (Exception ex)
        {
            errore = ex.Message.ToString();
        }

        string dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "M1Clienti.db3");
        if (System.IO.File.Exists(dbPath))
            System.IO.File.Delete(dbPath);
        var db = new SQLiteConnection(dbPath);
        db.CreateTable<TabellaClienti>();
        try
        {
            while (r.Read())     // istruzione che riporta l'errore - here i have the error
            {
                TabellaClienti item = new TabellaClienti();
                item.CodiceCliente = r[0].ToString(); // codice 
                item.CodiceAgente = r[1].ToString(); // nome
                item.RagioneSociale = r[2].ToString(); // ricerca
                item.Ricerca = r[3].ToString(); // ricerca
                item.Indirizzo = r[4].ToString(); // ricerca
                item.Cap = r[5].ToString(); // ricerca
                item.Localita = r[6].ToString(); // ricerca
                item.Provincia = r[7].ToString(); // ricerca
                item.Piva = r[8].ToString(); // ricerca
                item.CodiceFiscale = r[9].ToString(); // ricerca
                item.Telefono = r[10].ToString(); // ricerca
                item.Fax = r[11].ToString(); // ricerca
                item.Sito = r[12].ToString(); // ricerca
                item.Email = r[13].ToString(); // ricerca
                item.TipoPagamento = r[14].ToString(); // ricerca
                item.Listino = r[15].ToString();
                db.Insert(item);
            }
        }
        catch (Exception ex)
        {
            errore = ex.Message.ToString();
        }
        db.Close(); // chiude database locale
        conn.Close();

Posts

  • VictorGarciaAprea.9996VictorGarciaAprea.9996 USXamarin Team Xamurai

    Hi CPS.net,

    This doesn't seem related to either Xamarin or Visual Studio. You may want to try a general purpose C# forum for better luck.

    Thanks

  • DavidManzanoDavidManzano SVMember ✭✭

    CPS.net I have the same problem, I would like to know if you could fix this problem and how.
    or if you got any answer about this.

  • VCWaikatoVCWaikato NZMember

    I am also experiencing the same problem. Visual Studio 2010 and the latest version of Xamarin for Windows.
    I have tested my code in a dedicated c# project and it works ok.
    But when I use the same code as part of an android app, the error occurs.

    I am experiencing the same error.
    I have tested by writing a dedicated c# test using the same code and it runs OK.
    But the same code as part of an Android project fails.

    There is only one record in the database which matches the criteria of the search, so there should be no duplicate keys.

    The class which causes the error is as follows. The connection to the database is successful. The error occurs on the da.fill
    What am I missing? Your suggestions would be appreciated.

    public class cUser
    {

        private string strCon = "";
        private int cvUserID = 0;
        private string cvLogin = "";
        private string cvPwd = "";
        private string strSQL = "";
    
    
        public cUser(string strconnection)
        {
            strCon = strconnection;
        }
    
        public System.Data.DataSet GetUserDataUsingLogin( System.Data.DataSet ds, string Login, string Pwd, string strReply)
        {
            System.Data.SqlClient.SqlConnection dc = new System.Data.SqlClient.SqlConnection(strCon);
            System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter();
            dc.Open();
            strSQL = "select distinct U_ID, U_Login, U_Pwd from Users where U_Login = '" + Login + "' and U_Pwd = '" + Pwd + "'";
            System.Data.SqlClient.SqlCommand SqlCommand = new System.Data.SqlClient.SqlCommand(strSQL, dc);
            ds.Clear();
            da.SelectCommand = SqlCommand;
            try
            {
                ds.Clear();
                da.Fill(ds);
    
    
                SqlCommand = null;
    
                cvUserID = Convert.ToInt32(ds.Tables["Users"].Rows[0]["U_ID"]);
                cvLogin = Convert.ToString(ds.Tables["Users"].Rows[0]["U_Login"]);
                cvPwd = Convert.ToString(ds.Tables["Users"].Rows[0]["U_Pwd"]);
                dc.Close();
    
            }
            catch(Exception e)
            {
                strReply = e.Message;
            }
            return ds;
        }
    
    
    
    
    
    }
    
  • AsafHorvitzAsafHorvitz ILMember ✭✭

    i have the same problem with "select * from table".

    it works in android and not in ios.

    any suggestions ?

Sign In or Register to comment.