Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! Everyone can you help me to fix this code wherein the error comes out with this message "Object reference not set to an instance of an object."
VB
Const CSV_CONNECTIONSTRING As String = "Data Source=192.168.0.4;Initial Catalog=CYBERYA;Persist Security Info=True;User ID=cyberya;Password=Piso4minutes;MultipleActiveResultSets=True"
       Dim CSVpath As String = "C:\"
       ' CSV file Path
       Dim AllFiles = New DirectoryInfo(CSVpath).GetFiles("*.CSV")
       Dim File_Name As String = String.Empty
       Dim ConStr As String = ConfigurationManager.ConnectionStrings("ConStr").ConnectionString

       'ConnectionString

       For i As Integer = 0 To AllFiles.Length - 1
           Try
               File_Name = AllFiles(i).Name
               Dim dt As New DataTable()

               Using con As New OleDbConnection(String.Format(CSV_CONNECTIONSTRING, CSVpath))
                   Using da As New OleDbDataAdapter("select * from [" & File_Name & "]", con)
                       da.Fill(dt)
                   End Using
               End Using

               Using bulkCopy As New SqlBulkCopy(ConStr)
                   bulkCopy.ColumnMappings.Add("status", "status")
                   bulkCopy.ColumnMappings.Add("SRN", "SRN")
                   bulkCopy.ColumnMappings.Add("SrCode", "SrCode")
                   bulkCopy.ColumnMappings.Add("CustomerName", "CustomerName")
                   bulkCopy.ColumnMappings.Add("HomeContact", "HomeContact")
                   bulkCopy.ColumnMappings.Add("BusinessContact", "BusinessContact")
                   bulkCopy.ColumnMappings.Add("MobileContact", "MobileContact")
                   bulkCopy.ColumnMappings.Add("LotHouseNo", "LotHouseNo")
                   bulkCopy.ColumnMappings.Add("RoomUnitStall", "RoomUnitStall")
                   bulkCopy.ColumnMappings.Add("BldgFloor", "BldgFloor")
                   bulkCopy.ColumnMappings.Add("BldgName", "BldgName")
                   bulkCopy.ColumnMappings.Add("Street", "Street")
                   bulkCopy.ColumnMappings.Add("Subdivision", "Subdivision")
                   bulkCopy.ColumnMappings.Add("Barangay", "Barangay")
                   bulkCopy.ColumnMappings.Add("CityMunicipality", "CityMunicipality")
                   bulkCopy.ColumnMappings.Add("Province", "Province")
                   bulkCopy.ColumnMappings.Add("Region", "Region")
                   bulkCopy.ColumnMappings.Add("Package", "Package")
                   bulkCopy.ColumnMappings.Add("PromoCode", "PromoCode")
                   bulkCopy.ColumnMappings.Add("PaymentType", "PaymentType")
                   bulkCopy.ColumnMappings.Add("ApplicationDate", "ApplicationDate")
                   bulkCopy.ColumnMappings.Add("Endorseddate", "Endorseddate")
                   bulkCopy.ColumnMappings.Add("loanstatus", "loanstatus")
                   bulkCopy.ColumnMappings.Add("PaymentDate", "PaymentDate")
                   bulkCopy.ColumnMappings.Add("ScheduleDate", "ScheduleDate")
                   bulkCopy.ColumnMappings.Add("ActivationDate", "ActivationDate")
                   bulkCopy.ColumnMappings.Add("PCDelivery", "PCDelivery")
                   bulkCopy.ColumnMappings.Add("NoPCAvailed", "NoPCAvailed")
                   bulkCopy.ColumnMappings.Add("DatePCPickUp", "DatePCPickUp")
                   bulkCopy.ColumnMappings.Add("SalesAgent", "SalesAgent")
                   bulkCopy.ColumnMappings.Add("Coordinator", "Coordinator")
                   bulkCopy.ColumnMappings.Add("SalesChannel", "SalesChannel")
                   bulkCopy.ColumnMappings.Add("SalesGroup", "SalesGroup")
                   bulkCopy.ColumnMappings.Add("Created by", "Created by")
                   bulkCopy.ColumnMappings.Add("TimeStamp", "TimeStamp")
                   bulkCopy.DestinationTableName = "tblsummaryIMPORT"
                   bulkCopy.BatchSize = dt.Rows.Count
                   bulkCopy.WriteToServer(dt)
                   bulkCopy.Close()

               End Using

which is the source error is this:

Line 249: Dim AllFiles = New DirectoryInfo(CSVpath).GetFiles("*.CSV")
Line 250: Dim File_Name As String = String.Empty
Line 251: Dim ConStr As String = ConfigurationManager.ConnectionStrings("ConStr").ConnectionString
Line 252:
Line 253: 'ConnectionString
Posted
Updated 2-Jun-13 18:35pm
v2

If this exception was really thrown by the line of code you've shown in bold, it simply means that either ConfigurationManager (not likely) or ConfigurationManager.ConnectionStrings("ConStr") is null.

You should master dealing with such bugs by yourself instead of asking questions every time you face them. Happens for the first time? Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx[^],
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx[^].

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

Good luck,
—SA
 
Share this answer
 
If you found error comes from the line
Line 251: Dim ConStr As String = ConfigurationManager.ConnectionStrings("ConStr").ConnectionString
then first you should configure your ConfigurationManager with the tag of ConnectionStrings and name it "ConStr" , first you assign your connection string to it and then only you can get the same.
Gud Luck
 
Share this answer
 

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