Click here to Skip to main content
15,891,597 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello I'm developing a c # application that works on Sql Server 2014 with C#, I have an error on the code that see below, then this code imports a txt list inside the database, the insertion works correctly until the end of the inserts in the table ImportazioneElettroveneta, at the end of the insert operations in this table I am generated the exception below, can you explain to me where the problem is?

Exception(error):<br />
System.Data.SqlClient.SqlException (0x80131904): Invalid object name 'ImportazioneElettroveneta'.<br />
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)<br />
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)


What I have tried:

My source code:
C#
public Boolean ImportaListinoElettroveneta(String PercorsoFile)
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            Boolean ret = true;
            SqlConnection conn = Database.apriconnessione();
            SqlCommand command = conn.CreateCommand();
            command.CommandTimeout = 0;
            SqlTransaction transaction = conn.BeginTransaction("TransactionImportElettroveneta");
            try
            {
                String EliminaTabellaTemporanea = "drop table ImportazioneElettroveneta";
                command = new SqlCommand(EliminaTabellaTemporanea, conn, transaction);
                command.CommandTimeout = 0;
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Errore la tabella temporanea non esiste: " + ex);
            }
            String Query_Creazione_Tabella_Temporanea = "create table ImportazioneElettroveneta( CodiceNumericoEV varchar(5000),DescrizioneArticolo varchar(5000),CodiceArticoloEV varchar(5000),MarcaEV varchar(5000),UM varchar(5000),PrezzoListino money,Sconto1 money,Sconto2 money,Sconto3 money,Sconto4 money,PrezzoNetto money,CodiceBarreMetel bigint,IVA varchar(5000),MarcaMetel varchar(5000),ArticoloMetel varchar(5000),DescrizioneMarca varchar(5000)) ";
            command = new SqlCommand(Query_Creazione_Tabella_Temporanea, conn, transaction);
            command.CommandTimeout = 0;
            command.ExecuteNonQuery();
            command.CommandText = "INSERT Into ImportazioneElettroveneta(CodiceNumericoEV,DescrizioneArticolo,CodiceArticoloEV,MarcaEV,UM,PrezzoListino,Sconto1,Sconto2,Sconto3,Sconto4,PrezzoNetto,CodiceBarreMetel,IVA,MarcaMetel,ArticoloMetel,DescrizioneMarca) Values(@CodiceNumericoEV,@DescrizioneArticolo,@CodiceArticoloEV,@MarcaEV,@UM,@PrezzoListino,@Sconto1,@Sconto2,@Sconto3,@Sconto4,@PrezzoNetto,@CodiceBarreMetel,@IVA,@MarcaMetel,@ArticoloMetel,@DescrizioneMarca)";

            try
            {
                using (FileStream fs = File.Open(PercorsoFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    using (BufferedStream bs = new BufferedStream(fs, System.Text.ASCIIEncoding.Unicode.GetByteCount("271")))
                    using (StreamReader sr = new StreamReader(bs))
                    {
                        string s;
                        while ((s = sr.ReadLine()) != null)
                        {
                            command.Parameters.Clear();
                            try
                            {
                                command.Parameters.AddWithValue("@CodiceNumericoEV", s.Substring(startIndex: 1, length: 13));
                                command.Parameters.AddWithValue("@DescrizioneArticolo", s.Substring(startIndex: 13, length: 45));
                                command.Parameters.AddWithValue("@CodiceArticoloEV", s.Substring(startIndex: 58, length: 25));
                                command.Parameters.AddWithValue("@MarcaEV", s.Substring(startIndex: 83, length: 6));
                                command.Parameters.AddWithValue("@UM", s.Substring(startIndex: 89, length: 2));
                                command.Parameters.AddWithValue("@PrezzoListino", decimal.Parse(s.Substring(startIndex: 106, length: 15)));
                                command.Parameters.AddWithValue("@Sconto1", decimal.Parse(s.Substring(startIndex: 122, length: 5)));
                                command.Parameters.AddWithValue("@Sconto2", decimal.Parse(s.Substring(startIndex: 127, length: 5)));
                                command.Parameters.AddWithValue("@Sconto3", decimal.Parse(s.Substring(startIndex: 132, length: 5)));
                                command.Parameters.AddWithValue("@Sconto4", decimal.Parse(s.Substring(startIndex: 137, length: 5)));
                                command.Parameters.AddWithValue("@PrezzoNetto", decimal.Parse(s.Substring(startIndex: 142, length: 15)));
                                command.Parameters.AddWithValue("@CodiceBarreMetel", s.Substring(startIndex: 156, length: 13));
                                command.Parameters.AddWithValue("@IVA", s.Substring(startIndex: 169, length: 2));
                                command.Parameters.AddWithValue("@MarcaMetel", s.Substring(startIndex: 171, length: 3));
                                command.Parameters.AddWithValue("@ArticoloMetel", s.Substring(startIndex: 174, length: 16));
                                command.Parameters.AddWithValue("@DescrizioneMarca", s.Substring(startIndex: 190, length: 25));
                                command.CommandTimeout = 0;
                                command.ExecuteNonQuery();
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("Errore riga: " + ex);
                            }
                        }
                    }
                }



                command.Parameters.Clear();
                String QueryAggiornaNonDisponbili = "Update Articolo set Stato='Nondisponibile' where Importato='ELETTROVENETA' ";
                command = new SqlCommand(QueryAggiornaNonDisponbili, conn, transaction);
                command.CommandTimeout = 0;
                command.ExecuteNonQuery();
                command.Parameters.Clear();
                String QueryInserimentoNuoviArticoli = "Insert into Articolo(CodArt,Descrizione,CodMarca,CodEAN,Prezzo,PrezzoListino,UM,Fornitore,Importato) SELECT ArticoloMetel as CodArt,DescrizioneArticolo as Descrizione,MarcaMetel as CodMarca,CodiceBarreMetel as CodEAN,PrezzoNetto,PrezzoListino,UM,MarcaMetel as Fornitore,'ELETTROVENETA' as Importato FROM ImportazioneElettroveneta where ArticoloMetel not in ( select CodArt from Articolo where Importato = 'ELETTROVENETA' ) and MarcaMetel not in ( select CodMarca from Articolo where Importato = 'ELETTROVENETA' ) ";
                command = new SqlCommand(QueryInserimentoNuoviArticoli, conn, transaction);
                command.CommandTimeout = 0;
                command.ExecuteNonQuery();
                command.Parameters.Clear();
                String QueryAggiornamentoArticoliPresenti = " Update Articolo set Stato = 'Disponibile', Descrizione = i.Descrizione, CodEAN = i.CodEAN, Prezzo = i.PrezzoNetto, PrezzoListino = i.PrezzoListino, UM = i.UM, DataAggiornamento = getdate() from( SELECT ArticoloMetel as CodArt, DescrizioneArticolo as Descrizione, MarcaMetel as CodMarca, CodiceBarreMetel as CodEAN, PrezzoNetto, PrezzoListino, UM, MarcaMetel as Fornitore, 'ELETTROVENETA' as Importato FROM ImportazioneElettroveneta  where ArticoloMetel in (select CodArt from Articolo where Importato = 'ELETTROVENETA') and MarcaMetel in (select CodMarca from Articolo where Importato = 'ELETTROVENETA'))i where Articolo.CodArt = i.CodArt and i.CodMarca = Articolo.CodMarca and Articolo.Importato = 'ELETTROVENETA' and Articolo.Fornitore = i.Fornitore";
                command = new SqlCommand(QueryAggiornamentoArticoliPresenti, conn, transaction);
                command.CommandTimeout = 0;
                command.ExecuteNonQuery();

                try
                {
                    String EliminaTabellaTemporanea = "drop table ImportazioneElettroveneta";
                    command = new SqlCommand(EliminaTabellaTemporanea, conn, transaction);
                    command.CommandTimeout = 0;
                    command.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Errore la tabella temporanea non esiste: " + ex);
                }

                transaction.Commit();
            }
            catch (Exception ex)
            {
                ret = false;
                transaction.Rollback();
                Console.WriteLine("Errore Importazione Listino Elettroveneta: " + ex);
                Managementerror.SendError("Errore: " + ex);
            }
            conn.Close();
            return ret;
        }
Posted
Updated 27-Feb-18 3:44am
v2

1 solution

It might be that the table you're trying to create already exists in the database. You can try adding this before "create table..." in the first query:

SQL
IF OBJECT_ID("ImportazioneElettroveneta") IS NULL


You should probably also add this before "drop table":

SQL
IF OBJECT_ID("ImportazioneElettroveneta") IS NOT NULL 
 
Share this answer
 
v4
Comments
rikidev 27-Feb-18 9:45am    
I was also convinced that the problem was the same so I tried with this type of solution but I always have the same error!
rikidev 27-Feb-18 9:47am    
The fact is that I am forced to use sql server 2014, on my with docker where will run sql server 2017 for the test phase the code runs perfectly without errors!
#realJSOP 27-Feb-18 9:51am    
Where is the exception being thrown?
rikidev 27-Feb-18 10:12am    
at this query: QueryAggiornamentoArticoliPresenti

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