dba123
asked on
A first chance exception of type 'System.NullReferenceException' occurred in sss.exe
For the code here, I'm getting  think a null value returned from the recorset or my code for the select is wrong...I'm not sure how to resolve:
 Public Sub MoveFiles(ByVal sFolder As String)
    Dim files() As String = System.IO.Directory.GetFil es(sFolder , "*.TXT")
    For Each sFile As String In files
      Dim sFileName As String = System.IO.Path.GetFileName (sFile)
      Dim sPart As String = sFileName.Substring(0, 4)
      Dim sNewFolder As String = GetFolder(sPart)
      System.IO.File.Move(sFile, System.IO.Path.Combine(sNe wFolder, sFileName))
    Next
  End Sub
  Private Function GetFolder(ByVal PartnerID As String) As String
    Dim result As String = ""
    Dim objConn As SqlConnection = New SqlConnection(System.Confi guration.C onfigurati onManager. AppSetting s("MYConn" ))
    Dim objComm As New SqlCommand("SELECT FTPFilePath FROM mnt_FileName_Map WHERE PartnerID = @PartnerID")
    objComm.Parameters.Add(New SqlParameter("@PartnerID", PartnerID))
    Try
      objComm.Connection.Open()
      result = objComm.ExecuteScalar()
    Catch ex As Exception
      ex.Message.ToString()
    Finally
      If objConn.State = ConnectionState.Open Then objConn.Close()
    End Try
    Return result
  End Function
ERROR: A first chance exception of type 'System.NullReferenceExcep tion' occurred in sss.exe
 Public Sub MoveFiles(ByVal sFolder As String)
    Dim files() As String = System.IO.Directory.GetFil
    For Each sFile As String In files
      Dim sFileName As String = System.IO.Path.GetFileName
      Dim sPart As String = sFileName.Substring(0, 4)
      Dim sNewFolder As String = GetFolder(sPart)
      System.IO.File.Move(sFile,
    Next
  End Sub
  Private Function GetFolder(ByVal PartnerID As String) As String
    Dim result As String = ""
    Dim objConn As SqlConnection = New SqlConnection(System.Confi
    Dim objComm As New SqlCommand("SELECT FTPFilePath FROM mnt_FileName_Map WHERE PartnerID = @PartnerID")
    objComm.Parameters.Add(New
    Try
      objComm.Connection.Open()
      result = objComm.ExecuteScalar()
    Catch ex As Exception
      ex.Message.ToString()
    Finally
      If objConn.State = ConnectionState.Open Then objConn.Close()
    End Try
    Return result
  End Function
ERROR: A first chance exception of type 'System.NullReferenceExcep
Wheres the error pointing to anyway??
ASKER
Ok, I found out it's pointing here:
System.IO.File.Move(sFile, System.IO.Path.Combine(sNe wFolder, sFileName))
True error really is: Object Reference not set to an instance of an object
System.IO.File.Move(sFile,
True error really is: Object Reference not set to an instance of an object
ASKER
I'm assuming it's a problem with my GetFolder but not sure what
Make these two changes and see if that makes any difference,
Dim objComm As New SqlCommand("SELECT FTPFilePath FROM mnt_FileName_Map WHERE PartnerID = @PartnerID",objConn)
and while opening:objConn.Open() instead of objComm.Connection.Open()
Might not be a mistake but lets do it the traditional way..Bcoz honestly i never used that
Dim objComm As New SqlCommand("SELECT FTPFilePath FROM mnt_FileName_Map WHERE PartnerID = @PartnerID",objConn)
and while opening:objConn.Open() instead of objComm.Connection.Open()
Might not be a mistake but lets do it the traditional way..Bcoz honestly i never used that
ASKER
ok, sNewFolder is returning NULL
sNewFolder ???you might wanna post some more code here
ok got it ...sorry
ASKER
Here's the whole thing:
Imports System.IO
Imports System
Imports System.Configuration
Imports System.Data
Imports System.Text
Imports System.Data.SqlClient
Module Module1
  Sub Main()
    Dim f As New Security.Permissions.FileI OPermissio n(Security .Permissio ns.Permiss ionState.N one)
    f.AllLocalFiles = Security.Permissions.FileI OPermissio nAccess.Re ad
    Dim file As New IO.FileInfo("\\sss\f$\inet pub\wwwroo t\sss\ssis \maintenan ce\sss\pha se2\msss_i nput\sss_i nput.mnt")
    Dim filefs As New IO.FileStream(file.FullNam e, IO.FileMode.Open)
    Dim reader As New IO.StreamReader(filefs)
    Dim counter As Integer = 0
    Dim CurrentFS As IO.FileStream
    Dim CurrentWriter As IO.StreamWriter
    While Not reader.Peek < 0
      Dim Line As String = reader.ReadLine
      If IsNumeric(Line.Substring(0 , 1)) Then
        Dim Parts() As String = Line.Split(" "c)
        If Parts(0).Length = 8 Then
          counter += 1
          If Not CurrentWriter Is Nothing Then CurrentWriter.Flush() : CurrentWriter.Close()
          CurrentFS = New IO.FileStream(IO.Path.Comb ine(IO.Pat h.GetDirec toryName(" \\sss\f$\i netpub\www root\sss\s sis\mainte nance\sss\ phase2\sss _output\") , Line.Substring(59, 4) & "[" & counter.ToString & "]" & Now.ToString("MM-dd-yyyy") & IO.Path.GetExtension(file. FullName)) , IO.FileMode.Create)
          CurrentWriter = New IO.StreamWriter(CurrentFS)
        End If
        If Not CurrentWriter Is Nothing Then
          CurrentWriter.WriteLine(Li ne)
        End If
      End If
    End While
    If Not CurrentWriter Is Nothing Then CurrentWriter.Flush() : CurrentWriter.Close()
    'Console.Read()
    MoveFiles("\\sss\f$\inetpu b\wwwroot\ sss\ssis\m aintenance \sss\phase 2\sss_outp ut\")
  End Sub
  Public Sub MoveFiles(ByVal sFolder As String)
    Dim files() As String = System.IO.Directory.GetFil es(sFolder , "*.mnt")
    For Each sFile As String In files
      Dim sFileName As String = System.IO.Path.GetFileName (sFile)
      Dim sPart As String = sFileName.Substring(0, 4)
      Dim sNewFolder As String = GetFolder(sPart)
      MsgBox("sFileName:" & sFileName & "sNewFolder:" & sNewFolder)
      System.IO.File.Move(sFile, System.IO.Path.Combine(sNe wFolder, sFileName))
    Next
  End Sub
  Private Function GetFolder(ByVal PartnerID As String) As String
    'MsgBox(PartnerID)
    Dim result As String = ""
    Dim objConn As SqlConnection = New SqlConnection(System.Confi guration.C onfigurati onManager. AppSetting s("MYConn" ))
    Dim objComm As New SqlCommand("SELECT FTPFilePath FROM mnt_FileName_Map WHERE PartnerID = @partnerID", objConn)
    objComm.Parameters.Add(New SqlParameter("@PartnerID", PartnerID))
    Try
      objComm.Connection.Open()
      result = objComm.ExecuteScalar()
    Catch ex As Exception
      MsgBox("Error:" & vbCrLf & ex.Message)
    Finally
      If objConn.State = ConnectionState.Open Then objConn.Close()
    End Try
    Return result
  End Function
End Module
Imports System.IO
Imports System
Imports System.Configuration
Imports System.Data
Imports System.Text
Imports System.Data.SqlClient
Module Module1
  Sub Main()
    Dim f As New Security.Permissions.FileI
    f.AllLocalFiles = Security.Permissions.FileI
    Dim file As New IO.FileInfo("\\sss\f$\inet
    Dim filefs As New IO.FileStream(file.FullNam
    Dim reader As New IO.StreamReader(filefs)
    Dim counter As Integer = 0
    Dim CurrentFS As IO.FileStream
    Dim CurrentWriter As IO.StreamWriter
    While Not reader.Peek < 0
      Dim Line As String = reader.ReadLine
      If IsNumeric(Line.Substring(0
        Dim Parts() As String = Line.Split(" "c)
        If Parts(0).Length = 8 Then
          counter += 1
          If Not CurrentWriter Is Nothing Then CurrentWriter.Flush() : CurrentWriter.Close()
          CurrentFS = New IO.FileStream(IO.Path.Comb
          CurrentWriter = New IO.StreamWriter(CurrentFS)
        End If
        If Not CurrentWriter Is Nothing Then
          CurrentWriter.WriteLine(Li
        End If
      End If
    End While
    If Not CurrentWriter Is Nothing Then CurrentWriter.Flush() : CurrentWriter.Close()
    'Console.Read()
    MoveFiles("\\sss\f$\inetpu
  End Sub
  Public Sub MoveFiles(ByVal sFolder As String)
    Dim files() As String = System.IO.Directory.GetFil
    For Each sFile As String In files
      Dim sFileName As String = System.IO.Path.GetFileName
      Dim sPart As String = sFileName.Substring(0, 4)
      Dim sNewFolder As String = GetFolder(sPart)
      MsgBox("sFileName:" & sFileName & "sNewFolder:" & sNewFolder)
      System.IO.File.Move(sFile,
    Next
  End Sub
  Private Function GetFolder(ByVal PartnerID As String) As String
    'MsgBox(PartnerID)
    Dim result As String = ""
    Dim objConn As SqlConnection = New SqlConnection(System.Confi
    Dim objComm As New SqlCommand("SELECT FTPFilePath FROM mnt_FileName_Map WHERE PartnerID = @partnerID", objConn)
    objComm.Parameters.Add(New
    Try
      objComm.Connection.Open()
      result = objComm.ExecuteScalar()
    Catch ex As Exception
      MsgBox("Error:" & vbCrLf & ex.Message)
    Finally
      If objConn.State = ConnectionState.Open Then objConn.Close()
    End Try
    Return result
  End Function
End Module
ASKER
so there's somethign not right in my GetFolder function but not sure what it is...preferably I'd like to reference a stored proc anyway rather than an inline select
ASKER
The Lookup table definitely has valid lookup values...I'm sure something is malformed but can't pinpoint it in my code
I am sorry...did u check what objcomm.executescalar is returning...!!!!!
ASKER
Ok, it's not getting there (to the result) because now the error is saying the ConnectionString property has not been initialized.
ASKER
It's talking about ObjConn not being initialized
ASKER
If I put in objConn.Open(), I still get the message saying that objConn hasn't been initialized.
change this
system.Configuration.Confi gurationSe ttings.App Settings(" MYConn") and see..
system.Configuration.Confi
ASKER
Warning      2      'Public Shared ReadOnly Property AppSettings() As System.Collections.Special ized.NameV alueCollec tion' is obsolete: 'This method is obsolete, it has been replaced by System.Configuration!Syste m.Configur ation.Conf igurationM anager.App Settings'
srivatsavaye , I'm coding in .NET 2.0, you can't use that anymore...
srivatsavaye , I'm coding in .NET 2.0, you can't use that anymore...
ASKER
remember, this is a console application, not asp.net so you must use System.Configuration.Confi gurationMa nager.AppS ettings("M YConn")
ASKER
my connection string is in a app.config file, not web.config
The way you wrote is the way we write in web applications except for the appsettings...
Did you try that..anyway..bcoz I tried to type in configuration.configuratio nmanager.. it did not have that configurationmanager..so just try it..
Did you try that..anyway..bcoz I tried to type in configuration.configuratio
ASKER
I did try it, it errors out, I just pasted the error did you see it:
Warning   2   'Public Shared ReadOnly Property AppSettings() As System.Collections.Special ized.NameV alueCollec tion' is obsolete: 'This method is obsolete, it has been replaced by System.Configuration!Syste m.Configur ation.Conf igurationM anager.App Settings'
Warning   2   'Public Shared ReadOnly Property AppSettings() As System.Collections.Special
ASKER
this guy had a similar problem...but of course no resolution here:
http://www.dotnetjunkies.com/Forums/ShowPost.aspx?PostID=2769
http://www.dotnetjunkies.com/Forums/ShowPost.aspx?PostID=2769
Sorry...Lets debug carefully..
Do this..
dim conString as string=System.Configuratio n.Configur ationManag er.AppSett ings("MyCo nn")
then
 Dim objConn As SqlConnection = New SqlConnection(constring)
just put a breakpoint and see if you are retrieving the connectionstring
Hope you did this right:appSettings is case sensitive in your app.config file..
Do this..
dim conString as string=System.Configuratio
then
 Dim objConn As SqlConnection = New SqlConnection(constring)
just put a breakpoint and see if you are retrieving the connectionstring
Hope you did this right:appSettings is case sensitive in your app.config file..
ASKER
Here's the app.config (I covered up our servername, etc. of course):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.diagnostics>
    <sources>
      <!-- This section defines the logging configuration for My.Application.Log -->
      <source name="DefaultSource" switchName="DefaultSwitch" >
        <listeners>
          <add name="FileLog"/>
          <!-- Uncomment the below section to write to the Application Event Log -->
          <!--<add name="EventLog"/>-->
        </listeners>
      </source>
    </sources>
    <switches>
      <add name="DefaultSwitch" value="Information" />
    </switches>
    <sharedListeners>
      <add name="FileLog"
         type="Microsoft.VisualBasi c.Logging. FileLogTra ceListener , Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d 50a3a, processorArchitecture=MSIL "
         initializeData="FileLogWri ter"/>
      <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
      <!--<add name="EventLog" type="System.Diagnostics.E ventLogTra ceListener " initializeData="APPLICATIO N_NAME"/> -->
    </sharedListeners>
  </system.diagnostics>
 <connectionStrings>
  <add name="HEDIConn"
     connectionString="Data Source=myservername;Initia l Catalog=mydbname; integrated security=SSPI;persist security info=False; Trusted_Connection=Yes"
     providerName="System.Data. SqlClient" />
 </connectionStrings>
Â
</configuration>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.diagnostics>
    <sources>
      <!-- This section defines the logging configuration for My.Application.Log -->
      <source name="DefaultSource" switchName="DefaultSwitch"
        <listeners>
          <add name="FileLog"/>
          <!-- Uncomment the below section to write to the Application Event Log -->
          <!--<add name="EventLog"/>-->
        </listeners>
      </source>
    </sources>
    <switches>
      <add name="DefaultSwitch" value="Information" />
    </switches>
    <sharedListeners>
      <add name="FileLog"
         type="Microsoft.VisualBasi
         initializeData="FileLogWri
      <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
      <!--<add name="EventLog" type="System.Diagnostics.E
    </sharedListeners>
  </system.diagnostics>
 <connectionStrings>
  <add name="HEDIConn"
     connectionString="Data Source=myservername;Initia
     providerName="System.Data.
 </connectionStrings>
Â
</configuration>
ASKER
If I set breakpoints, how can I see the value being returned without using like a msgbox? Â I look at the compiler but it tells me nothing interesting
ASKER
If I run your code, it's still saying the ConnectionString is not initialized
  Private Function GetFolder(ByVal PartnerID As String) As String
    'MsgBox(PartnerID)
    Dim result As String = ""
    Dim conString As String = System.Configuration.Confi gurationMa nager.AppS ettings("M yConn")
    Dim objConn As SqlConnection = New SqlConnection(conString)
    Dim objComm As New SqlCommand("SELECT FTPFilePath FROM mnt_FileName_Map WHERE PartnerID = @partnerID", objConn)
    objComm.Parameters.Add(New SqlParameter("@PartnerID", PartnerID))
    Try
      objComm.Connection.Open()
      result = objComm.ExecuteScalar()
      MsgBox("Result:" & result)
    Catch ex As Exception
      MsgBox("Error:" & vbCrLf & ex.Message)
    Finally
      If objConn.State = ConnectionState.Open Then objConn.Close()
      objConn.Close()
    End Try
    Return result
  End Function
  Private Function GetFolder(ByVal PartnerID As String) As String
    'MsgBox(PartnerID)
    Dim result As String = ""
    Dim conString As String = System.Configuration.Confi
    Dim objConn As SqlConnection = New SqlConnection(conString)
    Dim objComm As New SqlCommand("SELECT FTPFilePath FROM mnt_FileName_Map WHERE PartnerID = @partnerID", objConn)
    objComm.Parameters.Add(New
    Try
      objComm.Connection.Open()
      result = objComm.ExecuteScalar()
      MsgBox("Result:" & result)
    Catch ex As Exception
      MsgBox("Error:" & vbCrLf & ex.Message)
    Finally
      If objConn.State = ConnectionState.Open Then objConn.Close()
      objConn.Close()
    End Try
    Return result
  End Function
ASKER
defining the connection string right into the module seems to work, however, I stick to best practices, which allows me to manage the connection string in one file (app.config) so I'd rather not have to do it this way:
    Dim conString As String = "Data Source=server;Initial Catalog=dbname; integrated security=SSPI;persist security info=False; Trusted_Connection=Yes"
    Dim objConn As SqlConnection = New SqlConnection(conString)
    Dim conString As String = "Data Source=server;Initial Catalog=dbname; integrated security=SSPI;persist security info=False; Trusted_Connection=Yes"
    Dim objConn As SqlConnection = New SqlConnection(conString)
ASKER
I want to figure this out, why can't I do it the way we've been trying? Â what's the misconnection with the app.config file?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks, I'll assume that's it, if not I'll post a new thread but you're right, that is probably the problem, it's missing!
If you see carefully the name of the connectionstring is HEDIConn not MyConn..