Dts.Transaction is null in my SSIS Script task - ssis

I have tried existing post and none of the answers helped me solve the issue. Adding the piece of code.In the below code Dts.Transaction is null and hence
sqlConn.
SqlConnection sqlConn;
sqlConn = (SqlConnection)(Dts.Connections["connectionManager"].AcquireConnection(Dts.Transaction) as SqlConnection);
sqlConn.Open();
I have an SQL Task which returns a list of records to Script Task. I am trying to use connection manager to establish a connection. Not sure what is wrong. It's ADO.net connection manager.
Please do not mark this question as duplicate as the existing thread
did not help me.

This here is working for me:
      
SqlConnection sqlCon = new SqlConnection();
sqlCon = (SqlConnection)Dts.Connections["DMP"].AcquireConnection(Dts.Transaction) as SqlConnection);
 

I have tested this and though Dts.Transaction is null, the following is working for me (also note that rather than opening/closing the connection, you use acquire/release):
SqlConnection sqlConn = (SqlConnection)Dts.Connections["connectionManager"].AcquireConnection(Dts.Transaction);
...
Dts.Connections["connectionManager"].ReleaseConnection(sqlConn);

Related

MySQL commands in vb code are not working

I've been trying to create a login page that will check if you're an administrator or a customer in my SQL data source. I am not sure why it can't understand the MySQLCommands. I added MySql.Data in the references but this doesn't seem to work.
This is where for example: MySqlConnection and MySqlCommand have blue underlinement.
Dim cmd As MySqlCommand = New MySqlCommand '(strSQL, con)
Password is a reserved word in MySql. If you want to use a field with that name then everytime you use it in your code you should remember to put it between backticks:
`password` = ...
Said that your code has serious problems. You should never concatenate strings coming from the user input to form a sql text. This leads to syntax errors caused by parsing problem and to Sql Injection attacks. You shoul use a parameterized query like this
strSQL = "SELECT name FROM employer WHERE (login=#login AND `password`=#pwd"
Dim cmd As MySqlCommand = New MySqlCommand(strSQL, con)
cmd.Parameters.Add("#login", MySqlDbType.VarChar).Value = strUser
cmd.Parameters.Add("#pwd",MySqlDbType.VarChar).Value = strPaswoord
con.Open()
If cmd.ExecuteScalar() = Nothing Then
....
Finally you should also change the way you get your data because you want to minimize the trips to access the database for performance reason. You should SELECT both the Name and the EMail with a single query and use an MySqlDataReader to get the data.
Other problems present in your code are the lack of appropriate using statement around the connection and the security problem caused by a possible clear text password stored in the database.
#GSerg asked me if I could right click and resolve.
I tried that but that was not an option.
After messing around with the error it appears that I had to write at top:
Imports MySql.Data.MySqlClient
I also had to add backticks when I used the word password for MySQL as #Steve reminded me.
Thank you for your help!

deleting items from sql table in VB.net

I am trying to remove all of the records from an SQL table in VB.net. My code for doing this is:
Dim SQL As String = "DELETE FROM MTable"
Using CN As New OleDb.OleDbConnection(AddPage.DBConnect)
CN.Open()
Dim DBcmd As New OleDb.OleDbCommand(SQL, CN)
DBcmd.ExecuteNonQuery()
CN.Close()
End Using
'SQLDataset.Tables("Mtable").Clear()
MtableTA.Update(SQLDataset)
SQL = "DELETE FROM ITable"
Using CN As New OleDb.OleDbConnection(AddPage.DBConnect)
CN.Open()
Dim DBcmd As New OleDb.OleDbCommand(SQL, CN)
DBcmd.ExecuteNonQuery()
CN.Close()
End Using
' SQLDataset.Tables("ITable").Clear()
ITableTA.Update(SQLDataset)
The Mtable and Itables are the SQL tables, while MtableTA and ItableTA are table adapters.
I also end up getting an error which states
An unhandled exception of type 'System.Data.DBConcurrencyException' occurred in System.Data.dll
Additional information: Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.
The section where this occurss is not provided in the code above, but is a call to MtableTA.update(SQLDataset). Any help would be very much appreciated. I'm also using OLEDB if that helps.
You have directly deleted the rows bypassing the TableAdapter methods to do that. So it is highly probable that when you call the Update there are some conflicts with data changed on the TableAdapter and no more available in the database.
After removing the rows directly using OleDbCommand.ExecuteNonQuery you should simply refresh the TableAdapters to sync them with the real situation on your physical database table
SQLDataset.Tables("ITable").Clear()
ITableTA.Adapter.Fill(SQLDataSet.ITable)

Rename a mysql table using textbox

i am making a simple program to create a mysql table from vb.net and renaming it by user choice.
i got success in creating the table it is quite simple but how to rename a table from textbox tortured me a lot.
here's the code to create a table:
Try
cnn.Open()
Dim query As String
query = "CREATE TABLE best.new (id INT NOT NULL, name VARCHAR(45) NULL, date DATETIME NULL, PRIMARY KEY (id));"
cmd = New MySqlCommand(query, cnn)
reader = cmd.ExecuteReader
MessageBox.Show("table created")
cnn.Close()
i tried to rename it using this code but no success....! achieved yet.
i tried few more but it gives error.
i want to rename it from the "id" given by the user:
Dim rename As String
Rename = ("RENAME TABLE new To" TextBox1.Text)
cmd = New MySqlCommand(Rename, cnn)
reader = cmd.ExecuteReader
MessageBox.Show("renamed")
cnn.Close()
please help me to find it out
There are two issues with code you posted.
First, you are missing cnn.Open. Second, you are missing a + in second line, it should be Rename = ("RENAME TABLE new To" + TextBox1.Text).
Maybe it's just a typo and there's something different wrong with a code. So, post complete code and error messages.
This question is pretty bad from a quality perspective. In the future please try to include all essential details. That would mean error messages and full/relevant source code.
As you are a newbie to SO I'll try my hand at psychic debugging.
It would appear that you have forgotten to open the database connection before trying to ExecuteReader the rename command.

Configure Enterprise Library in code

Is it possible to configure the Data Application Block in Enterprise Library entirely in code?
Instead of having the big messy config file.
Ok, after a bit of googeling and some trial and error I've come up with this solution that works pretty good. It uses the System.Data.SQLClient provider.
Just supply a connectionstring:
Dim databaseSettings As Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings
Dim connStringSection As System.Configuration.ConnectionStringsSection = New System.Configuration.ConnectionStringsSection()
Dim dictDataSource As Microsoft.Practices.EnterpriseLibrary.Common.Configuration.DictionaryConfigurationSource
Dim dbProvider As Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DbProviderMapping
Dim dbFactory As DatabaseProviderFactory
Dim database As Microsoft.Practices.EnterpriseLibrary.Data.Database
databaseSettings = New Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings()
connStringSection = New System.Configuration.ConnectionStringsSection()
dictDataSource = New Microsoft.Practices.EnterpriseLibrary.Common.Configuration.DictionaryConfigurationSource()
dbProvider = New Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DbProviderMapping(Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DbProviderMapping.DefaultSqlProviderName, GetType(Sql.SqlDatabase))
connStringSection.ConnectionStrings.Add(New System.Configuration.ConnectionStringSettings("DBConnectionString", connectionString, "System.Data.SqlClient"))
databaseSettings.ProviderMappings.Add(dbProvider)
databaseSettings.DefaultDatabase = "DBConnectionString"
'Add Database Settings to Dictionary
dictDataSource.Add(Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings.SectionName, databaseSettings)
'Add Connection String to Dictionary
dictDataSource.Add("connectionStrings", connStringSection)
dbFactory = New DatabaseProviderFactory(dictDataSource)
database = dbFactory.Create("DBConnectionString")
database.CreateConnection()
Return database
short answer: yes.
Long answer: Why would you want to do that? The whole idea of the "big messy" file is that you write somewhat generic code that uses interfaces and then configure the specifics. Want to switch from ORacle to SQL Server? I've done it just by updating a config variable. Yes it's a bear porting stored procs, but it works.

Visual Basic 2008 - NullReferenceException ERROR?

I'm using Visual Basic 2008 here, and I'm debugging my code that connects to my SQL Database and writes something in it. It was working fine and all until I came to an error like this. NullReferenceException was unhandled. What's going on? Here is the code I'm working with:
Dim conn As MySqlConnection
conn = New MySqlConnection
conn.ConnectionString = "server=...; user id=...; password=...; database=..."
Try
conn.Open()
Catch myerror As MySqlException
MsgBox("Error connecting to database")
End Try
Dim myAdapter As New MySqlDataAdapter
Dim sqlquery = "SELECT * FROM user WHERE username = '" + TextBox2.Text + "'"
Dim myCommand As New MySqlCommand()
myCommand.Connection = conn
myCommand.CommandText = sqlquery
myAdapter.SelectCommand = myCommand
Dim myData As MySqlDataReader
myData = myCommand.ExecuteReader()
It highlights it right around conn.open(), and gives me that error. It was working fine earlier until I moved my sql database to my mac. (windows -> mac) Is there a difference? I backed up my stuff from my windows vista computer and restored it on my mac. I don't think there is a difference, but I'm just putting that out there. Why does this error come up?
Thanks,
Kevin
I would suggest reviewing your connection string. I used your code and connected to our local MYSQL db no errors.
I can't really say what the problem is off the top of my head.
But the way to find out would be to put a breakpoint on the conn=New MySqlConnection and look at the variables you have as you step through.
If the problem is happening on the conn.Open line then it would probably be because conn is null (nothing). But if that is the case then conn.ConnectionString should have thrown an exception.
The other step is to look at the stack trace of the exception and see where, exactly, it is being thrown from. It could, for example, be an error somewhere inside the mysql connector.
Sorry I can't really give you a definate answer, but I thought some general comments might help you to the solution or find some more detail.
Whivh version of MySql are you using? Have you looked at the details of this bug...
http://bugs.mysql.com/bug.php?id=26393
... which is related to idle time.
If you had the full stack trace associated with the exception then it might shed some light on the problem.