Andrius
Andrius

Reputation: 11

Need help (The ConnectionString property has not been initialized)

I have a problem. I cannot fix the problem with The ConnectionString property has not been initialized. The problem is in this method:

try
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    using (TransactionScope ts = new TransactionScope())
                    {
                        products.Update(dataSet, "Produktai");
                        offers.Update(dataSet, "Pasiulimai");
                        ts.Complete();
                    }
                }
                connection.Close();
            }
        }
        catch
        { }

In the class constructor i already have a SqlDataAdapter and SqlCommandBuilder declared. My connection string is in App.config and it looks like this:

<connectionStrings>
<add name="connectionString" connectionString="server=ANDREW-PC\LTSMSQL;database=MarketDB; Integrated Security=true;" providerName="System.Data.SqlClient" />

In my program I already assigned this connection string parameter to string variable. Here is a code sample:

private string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;

Any ideas how I can fix this error?

Upvotes: 1

Views: 460

Answers (3)

Andrius
Andrius

Reputation: 11

Ok, I just found the problem. For some reasons my database was "read only" for me when I connect with visual studio. I have changed some settings in the database and now it works fine. Thanks for your answers. :)

Upvotes: 0

dfdsfdsfsdf
dfdsfdsfsdf

Reputation: 663

why are you using System.Transaction.TransactionScope? are you dealing with multiples transaction aware data sources such as sql server and oracle, where you need a transaction manager to coordinate the transaction? if not, then why don't you create a transaction from the connection?

   using (var connection = new System.Data.SqlClient.SqlConnection(" "))
            {
                connection.Open();
                var tran = connection.BeginTransaction();

                var cmd = new System.Data.SqlClient.SqlCommand();
                cmd.Connection = connection;
                cmd.Transaction = tran;


                //I dont know how the sql command relates to this  
                products.Update(dataSet, "Produktai");
                offers.Update(dataSet, "Pasiulimai");

                //commit
                tran.Commit(); 
            }

Upvotes: 0

TechnicalTophat
TechnicalTophat

Reputation: 1725

The command isn't assigned to the connection. You need to use the sqlcommand constructor like so: new SQLCommand(connection, "querystring"). I also suggest you use a newer technology such as ORM. I used basic ADO.NET data access before I found Fluent NHibernate, and Fluent is so much easier to use :-)

Upvotes: 1

Related Questions