Click here to Skip to main content
15,915,042 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
while running my code getting this error. Please help me.

I have two connection (conl and conr)
using sql dataadapter and dataset to select data from conl.dbo.txnenrollment and insert it into conr.dbo.txnenrollment.
Unable to understand where is the error.

What I have tried:

Dim conr As String = ("Data Source=MOONLOADER\MSSQLSERVER2005;Initial Catalog=SSBYM;User ID=sa;Password=sasa")
    Private Sub btnupload_Click(sender As Object, e As RoutedEventArgs) Handles btnupload.Click
        Try
            sqlcon = New SqlConnection(conr)
            sqlcon = New SqlConnection(conl)
            Dim da As New SqlDataAdapter()
            Dim ds As New DataSet()
            sqlcon.Open()
            da.SelectCommand.Connection = New SqlConnection(conl)
            da.SelectCommand = New SqlCommand("select * from dbo.TxnEnrollment where URN not in (select URN from dbo.URN)")
            da.Fill(ds)
            da.InsertCommand.Connection = New SqlConnection(conr)
            da.InsertCommand = New SqlCommand("insert into dbo.TxnEnrollment where URN not in (select URN from dbo.TxnEnrollment)")
            cmd.Parameters.Clear()
            sqlcon.Close()
            MessageBox.Show("Uploaded Successfully")
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())

        End Try


    End Sub
End Class
Posted
Updated 19-Dec-18 1:26am
Comments
F-ES Sitecore 19-Dec-18 7:19am    
This question is asked every day, please google the error message and you'll find how to deal with these errors. Also any time you get an error message always say what line it occurs on.

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,
VB.NET
' Excuse my illiteracy in VB.NET
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,
VB
' Create instances; ignore Dim if you have fields with these names
Dim sqlconr As SqlConnection = New SqlConnection(conr)
Dim sqlconl As SqlConnection = New SqlConnection(conl)

Dim da As New SqlDataAdapter()
Dim ds As New DataSet()

' Open connections
sqlconr.Open()
sqlconl.Open()

' Now connect connections with commands; reuse the existing ones
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

' I would comment this
' cmd.Parameters.Clear() 

' End with
sqlconr.Close()
sqlconl.Close()
This might be better approach, but still it is unclear as to what you really need. :-)
 
Share this answer
 
v2
Comments
Member 12495621 20-Dec-18 2:52am    
Hello Afzaal! thanks for your reply and detailed exploration. As your advice I changed my code, but same error again.
Actually I have columns with null value in some rows. I need to insert data from one server to another server. No matter there is null value or not.
Now my code is.

Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Linq


Class MainWindow
Dim sqlcon As New SqlConnection
Dim cmd As New SqlCommand
Dim conl As String = ConfigurationManager.ConnectionStrings("conl").ConnectionString
Dim conr As String = ("Data Source=MOONLOADER\MSSQLSERVER2005;Initial Catalog=SSBYM;User ID=sa;Password=sasa")
Private Sub btnupload_Click(sender As Object, e As RoutedEventArgs) Handles btnupload.Click
Try
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
da.InsertCommand = New SqlCommand("insert into dbo.TxnEnrollment where URN not in (select URN from dbo.TxnEnrollment)")
If cmd IsNot Nothing Then
cmd.Parameters.Clear()
End If
sqlconr.Close()
sqlconl.Close()
MessageBox.Show("Uploaded Successfully")
Catch ex As Exception
MessageBox.Show(ex.Message.ToString())

End Try

End Sub
End Class
This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Comments
Member 12495621 20-Dec-18 2:36am    
Thanks for your reply and advice. I did as you say, but it's not working because my code is not failed in VB.net. It is running properly and the error cumming from the program, not from VB.So unable to find out the error coding using VB. Thanks for giving me a brief description of null value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900