Avatar billede keepy Seniormester
16. november 2017 - 07:59 Der er 1 kommentar og
1 løsning

sql get

Hej
Jeg har en application der ca. hver 10 sek. forespørger efter data i en sql server 2000 database.
En gang imellem går det ikke godt, så får jeg en exception om at Connectionen er open eller closed. Min kode nedefor skulle gerne sørge via brugen af using at connectionen bliver lukket.

Hvad tror i der kan være galt ??


KODEN:
using (SqlConnection connection = new SqlConnection(_ConnectionString))
                {
                    connection.Open();
                    using (SqlCommand objSelectCommand = new SqlCommand())
                    {
                        objSelectCommand.Connection = m_objConnection;
                        objSelectCommand.CommandType = CommandType.StoredProcedure;
                        objSelectCommand.CommandText = "Data_Load";

                        objSelectCommand.Parameters.Add("@Id", SqlDbType.Char, 3).Value = _Id;
                        m_objAdapter.SelectCommand = objSelectCommand;
                        m_objAdapter.Fill(ds, "Data_Id");
                        blnSuccess = true;
                    }
                }

FEJLEN 1:
Exception: ExecuteReader requires an open and available Connection. The connection's current state is open.
StackTrace: at System.Data.SqlClient.SqlConnection.GetOpenConnection(String method)
at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)
at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable)



FEJLEN 2:

Exception: Invalid operation. The connection is closed.
StackTrace: at System.Data.ProviderBase.DbConnectionClosed.CreateReferenceCollection()
at System.Data.ProviderBase.DbConnectionInternal.AddWeakReference(Object value, Int32 tag)
at System.Data.SqlClient.SqlConnection.AddWeakReference(Object value, Int32 tag)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable)
Avatar billede Spotgun Seniormester
16. november 2017 - 11:39 #1
Det lader til at du bruger objektet "m_objConnection" i stedet for "connection" på din SQLCommand, så det er formentlig derfor at forbindelsen ind i mellem er lukket.
Avatar billede keepy Seniormester
16. november 2017 - 12:10 #2
For den da, det er godt set 1000 tak
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
IT-kurser om Microsoft 365, sikkerhed, personlig vækst, udvikling, digital markedsføring, grafisk design, SAP og forretningsanalyse.

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester