i have a private sub that reads 4 lines of data from a text file, and then
uses whats read and stores them in varibles. see below

Dim FileName As String = configfileV
        Dim TextFromTheFile As String ' Whole text
        Dim Lines() As String ' File splitted to lines
        Dim LineSeparator() As String = {Environment.NewLine} ' Line separator 'character'

  ' Use My namespace
        If My.Computer.FileSystem.FileExists(FileName) Then
            ' Read the file   
            TextFromTheFile = My.Computer.FileSystem.ReadAllText(FileName)
            ' Get lines. I used StringSplitOptions that removes empty lines  
            Lines = TextFromTheFile.Split(LineSeparator, System.StringSplitOptions.RemoveEmptyEntries)
            ' Now you have Lines() array ready  
            ServerV = Lines(0).Substring(Lines(0).IndexOf("="c) + 1)
            databaseV = Lines(1).Substring(Lines(1).IndexOf("="c) + 1)
            usernameV = Lines(2).Substring(Lines(2).IndexOf("="c) + 1)
            passwordV = Lines(3).Substring(Lines(3).IndexOf("="c) + 1)

            ' The file not found error  
            MessageBox.Show("Config file does not exist", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            If vbOK Then End
        End If
        ' checks to see if the date format is valid, if not end the project
        If IsDate(datestr) Then
            MsgBox("Licence Error", MsgBoxStyle.Critical, "Invalid Licence format")
        End If

        'checks the date againts the text file
        'Me.txtday.Text = datestr
        expiredateV = datestr

        'checks the licence date agains the licence file
        If Today.Date = expiredateV Then MsgBox("Renew Required In 1 Day Contact Tecnical Support")
        If Today.Date > expiredateV Then Endsubscrition()

this all works fine.
i uses this varibles to build up a sql connection string.

Public ConnectionString As String = "Data Source=" & ServerV & ";Initial Catalog=" & databaseV & ";Persist Security Info=True;User ID=" & usernameV & ";Password=" & passwordV & ""

when running this code

Public Function searchname() As DataView
        Dim SelectQry = "SELECT * FROM donerdetails where name Like'" & Me.txtname.Text & "%'"
        Dim SampleSource As New DataSet
        Dim TableView As DataView
            Dim SampleCommand As New SqlCommand()
            Dim SampleDataAdapter = New SqlDataAdapter()
            SampleCommand.CommandText = SelectQry
            SampleCommand.Connection = Connection
            SampleDataAdapter.SelectCommand = SampleCommand
            TableView = SampleSource.Tables(0).DefaultView
        Catch ex As Exception
            Throw ex
        End Try
        Return TableView
    End Function

i get a error to say... Login failed for user ".

if i manual populate the varibles in the code like so it works fine.

Public ServerV As String = Nothing "localhost"
    Public databaseV As String = Nothing  "Giftaid"
    Public usernameV As String = Nothing  "mbish"
    Public passwordV As String = Nothing "mbish"

sorry for all the code but thought it best so you can understand the full process, hope someone can help.. thanks in advance

Recommended Answers

All 10 Replies

sorry meant

Public ServerV As String = "localhost"   
 Public databaseV As String = "Giftaid"  
  Public usernameV As String =  "mbish"   
 Public passwordV As String =  "mbish"

Try to use the VBCrLf constant as the separator, or, use debugger to watch the values of the variables when they are read from the file.

how do i use this? sorry i am very new to programming

You have to change 1 line of code:

Lines = TextFromTheFile.Split(VbCrLf, System.StringSplitOptions.RemoveEmptyEntries)

i get the following error

Well, i have this code i use for my settings, is in spanish but might be no problems to change variable names:
To save settings:

Public Sub GuardarConfiguraciones()
        Dim sw As New StreamWriter("M:\MineriaDatos\configuracion.ini")
        sw.WriteLine(String.Format("<serverBDs>{0}</serverBDs>", _serverBDs))
        sw.WriteLine(String.Format("<BaseDatosServer>{0}</BaseDatosServer>", _BaseDatosServer))
        sw.WriteLine(String.Format("<userServer>{0}</userServer>", _userServer))
        sw.WriteLine(String.Format("<passwordServer>{0}</passwordServer>", _passwordServer))
        If SucursalCAC Then
        End If
    End Sub

To read settings:

Public Sub LeerConfiguraciones()
        Dim sr As New StreamReader("M:\MineriaDatos\configuracion.ini")
        Dim contenido As String = sr.ReadToEnd
        _serverBDs = contenido.Substring(contenido.IndexOf("<serverBDs>") + "<serverBDs>".Length)
        _serverBDs = _serverBDs.Substring(0, _serverBDs.IndexOf("</serverBDs>"))
        _BaseDatosServer = contenido.Substring(contenido.IndexOf("<BaseDatosServer>") + "<BaseDatosServer>".Length)
        _BaseDatosServer = _BaseDatosServer.Substring(0, _BaseDatosServer.IndexOf("</BaseDatosServer>"))
        _userServer = contenido.Substring(contenido.IndexOf("<userServer>") + "<userServer>".Length)
        _userServer = _userServer.Substring(0, _userServer.IndexOf("</userServer>"))
        _passwordServer = contenido.Substring(contenido.IndexOf("<passwordServer>") + "<passwordServer>".Length)
        _passwordServer = _passwordServer.Substring(0, _passwordServer.IndexOf("</passwordServer>"))
        conectionstring = String.Format("Data Source='{0}'; Initial Catalog='{1}';User id='{2}'; Password='{3}'", _serverBDs, _BaseDatosServer, _userServer, _passwordServer)
    End Sub

The settings are saved in a XML format.
Let me know if this solve your problem.

Dont forget to type this before class definition:

Imports System.IO

can you post a copy of your xml file too please?

sorry your ini file

thanks for your help, i am getting the same outcome with this as my current import i will start a new thread.

The file is writen as this:


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.