how to save printer name configuration in appSettings from PrinterSettings looping in combobox in vb.net

37 Views Asked by At

I'm trying to save printer name configuration in appSettings from PrinterSettings looping in combobox in vb.net

but there is an error Object reference not set to an instance of an object.

in this line code _GeneralSettings.name_printer = cmbPrinter.Text

maybe you are wrong in my code

and if there is something simple in the implementation of the code below please guide me

Thanks

Imports System.Configuration
Imports System.Drawing.Printing
Imports System.IO
Imports System.Windows.Forms

Public Class Form1
    Private _GeneralSettings As GeneralSettings = Nothing
    Private Sub SaveLocalSettings()
        Dim appConfigFile = String.Format("{0}\howtoSavePrinternameconfig.exe.config", Utils.GetAppPath())
        _GeneralSettings.name_printer = cmbPrinter.Text
        Dim TypesofPrinters = PrinterType.InkJet
        If rdoJenisPrinterDotMatrix.Checked Then
            TypesofPrinters = PrinterType.DotMatrix
        ElseIf rdoJenisPrinterMiniPOS.Checked Then
            TypesofPrinters = PrinterType.MiniPOS
        End If
        _GeneralSettings.Printer_Type = TypesofPrinters
        ' save info printer
        AppConfigHelper.SaveValue("printerName", _GeneralSettings.name_printer, appConfigFile)
        AppConfigHelper.SaveValue("PrinterType", Convert.ToString(CInt(Math.Truncate(TypesofPrinters))), appConfigFile)

    End Sub
    Private Sub LoadPrinter()
        For Each printer In PrinterSettings.InstalledPrinters
            cmbPrinter.Items.Add(printer)
        Next printer
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadPrinter()
    End Sub

    Private Sub btnsave_Click(sender As Object, e As EventArgs) Handles Btnsave.Click
        SaveLocalSettings()
    End Sub
End Class
Public Enum PrinterType
    InkJet = 1
    DotMatrix = 2
    MiniPOS = 3
End Enum
Public Class GeneralSettings
    Public Property name_printer As String
    Public Property Printer_Type As PrinterType
End Class
Public Module Utils
    Public Function GetAppPath() As String
        Return Directory.GetCurrentDirectory()
    End Function
End Module
Public Module AppConfigHelper
    Private Const SECTION_NAME As String = "appSettings"
    Private Function IsSectionExist(ByVal sectionName As String, ByVal appSetting As AppSettingsSection) As Boolean
        Dim keyCount = appSetting.Settings.AllKeys.Where(Function(key) key = sectionName).Count()
        Return keyCount > 0
    End Function
    Public Sub SaveValue(ByVal sectionName As String, ByVal value As String, ByVal appConfigFile As String)
        Dim configFileMap = New ExeConfigurationFileMap()
        configFileMap.ExeConfigFilename = appConfigFile

        Dim configuration = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None)
        Dim section = CType(configuration.GetSection(SECTION_NAME), AppSettingsSection)

        Try
            If IsSectionExist(sectionName, section) Then
                section.Settings(sectionName).Value = value
            Else
                section.Settings.Add(sectionName, value)
            End If
            configuration.Save(ConfigurationSaveMode.Modified, False)
            ConfigurationManager.RefreshSection(SECTION_NAME)
        Catch
        End Try
    End Sub
End Module
0

There are 0 best solutions below