SpongeBob SquarePants
SpongeBob SquarePants

Reputation: 1055

Getting an error when I try to insert data into the database in VS-2010

Try
Dim SQLconnect As New SQLite.SQLiteConnection()
Dim SQLcommand As SQLiteCommand
SQLconnect.ConnectionString = "Data Source=" & Application.StartupPath & "\Database\db" & ";"
SQLconnect.Open()     ' VS highlights this line 
SQLcommand = SQLconnect.CreateCommand
SQLcommand.CommandText = "INSERT INTO Table1 (Status) VALUES ('Enabled')"
SQLcommand.ExecuteNonQuery()
SQLcommand.Dispose()
SQLconnect.Close()
Catch ex As Exception
MsgBox("Error: Operation unsuccessfull")
End Try

Getting an error when I try to insert data in to the databse,

Error: The type initializer for 'System.Transactions.Diagnostics.DiagnosticTrace' threw an exception.

How Can I solve this problem ?

Edit 1:

enter image description here

Edit 2:

I tried it without using Try Catch Block

Dim sqlConnection As New SQLite.SQLiteConnection()
Dim sqlCommand As New SQLiteCommand
sqlConnection.ConnectionString = "Data Source=db.s3db"  'is it because of the extension of the database ?
sqlConnection.Open()
sqlCommand = sqlConnection.CreateCommand()
sqlCommand.CommandText = "INSERT INTO Table1 (Status) VALUES ('Enabled')"
sqlConnection.Close()

Is it because of the database extension ? I used sqlite Admin to create tables

Edit 3:

I just tried it out in VS 2008, it works flawlessly. Why is it not working in VS 2010. Does it something have to do with app.config file ? or .net framework 4 ?

This is what I added in the app.config file

<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
<configSections>..................

Upvotes: 0

Views: 1502

Answers (3)

SpongeBob SquarePants
SpongeBob SquarePants

Reputation: 1055

I was getting the error

The type initializer for 'System.Transactions.Diagnostics.DiagnosticTrace' threw an exception

because I added this line to app.config

<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>

Without removing the old entry

<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>

I replaced the old entry with the new entry. Voila the error is gone.

Upvotes: 2

Anuraj
Anuraj

Reputation: 19618

Try run the application with Administrator privileges.(If you are using Visual Studio, right click on the Shortcut, Select Run As Administrator. You can also set this in the Properties.)

Seems like similar issue : http://connect.microsoft.com/VisualStudio/feedback/details/98437/sqlconnection-open-fails-because-of-system-transactions-diagnostics-diagnostictrace-for-non-admin-user

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460340

You should close the connection in the Finally-Statement of your Try/Catch. Otherwise the connection might keep open when an exception occurs.

Another option is to use the Using-Statement what automatically closes the Connection and disposes objects. For example:

Using SQLconnect As New SQLite.SQLiteConnection("Data Source=" & Application.StartupPath & "\Database\db" & ";")
   SQLconnect.Open()
   ' .... '
End Using

Upvotes: 0

Related Questions