Quote:
I have two connection (conl and conr)
No, you have only one.
sqlcon = New SqlConnection(conr)
sqlcon = New SqlConnection(conl)
Your second constructor call overrides the previous one, so
sqlcon
always points to the instance with
conl
connection string. And you go ahead and make it even worse with this line,
da.SelectCommand.Connection = New SqlConnection(conl)
Now you create a new instance, and pass that to the
Connection
property—
this could lead to memory leaks. Not just that, here you do that again,
da.InsertCommand.Connection = New SqlConnection(conr)
And although very unclear, where does this
cmd
come from?
cmd.Parameters.Clear()
This might be the reason where the null pointer is found, and the exception being raised. You can check against,
If cmd IsNot Nothing Then
cmd.Parameters.Clear()
End If
This might be able to overcome the error in this code, but you will stumble upon similar errors in future too, so please I recommend, instead of fixing this error—as important it might be—please learn some database strategies of development, and how objects are reused in the process.
Please read my article on this,
How to connect SQL Database to your C# program, beginner's tutorial[
^], it might give you an idea of how SQL Server databases are to be programmed in .NET, that article is in C#, you can relate it with VB.NET.
Also, as a bonus, I can tell you a few more things about your code, rewrite it like this,
Dim sqlconr As SqlConnection = New SqlConnection(conr)
Dim sqlconl As SqlConnection = New SqlConnection(conl)
Dim da As New SqlDataAdapter()
Dim ds As New DataSet()
sqlconr.Open()
sqlconl.Open()
da.SelectCommand.Connection = sqlconl
da.SelectCommand = New SqlCommand("select * from dbo.TxnEnrollment where URN not in (select URN from dbo.URN)")
da.Fill(ds)
da.InsertCommand.Connection = sqlconr
sqlconr.Close()
sqlconl.Close()
This might be better approach, but still it is unclear as to what you really need. :-)