Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
i have module.vb file and i want to get data from textfile...

VB
Dim FILE_NAME As String = Application.StartupPath & "\textfile.txt"
    Dim objReader As New System.IO.StreamReader(FILE_NAME)
    Dim cons1 As String = objReader.ReadLine 'error "declaration expected"
        objReader.Close()
    Dim cons() As String
        cons = cons1.Split(" ") 'error "declaration expected"


Im having an error "Declaration expected". Thanks for the help!
Posted

Without the context it's difficult to be sure, but...is any of this code inside a method? Because if it isn't, then that would cause the errors when you try to use code which doesn't declare a variable, such as:
VB
objReader.Close()

Or
VB
cons = cons1.Split(" ")
 
Share this answer
 
Comments
jerahmeel salayog 14-Nov-13 8:36am    
Dim FILE_NAME As String = Application.StartupPath & "\textfile.txt"
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Dim cons1 As String = objReader.ReadLine '
objReader.Close()
Dim cons() As String
cons = cons1.Split(CChar(" "))

Public dbconn As New SqlClient.SqlConnection("Data Source=" & cons(0) & "; Initial Catalog=" & cons(1) & ";User Id=" & cons(2) & "; Password=" & cons(3) & ";")

ive added the rest of the code.. i want to get connection string info from textfile.. thanks!
To remove syntax errors, I made the following changes.

1. Added a reference to System.Windows.Forms.
2. Added an Import System.Windows.Forms.
3. Added an Import System.IO.
4. Added CChar(" ") to .Split method parameter to cast the parameter to the Char data type.
5. Put the code inside a method named Test.

VB
Imports System.Windows.Forms
Imports System.IO
Module Module1
    Sub Test()
        Dim FILE_NAME As String = Application.StartupPath & "\textfile.txt"
        Dim objReader As New System.IO.StreamReader(FILE_NAME)
        Dim cons1 As String = objReader.ReadLine '
        objReader.Close()
        Dim cons() As String
        cons = cons1.Split(CChar(" "))
    End Sub
End Module
 
Share this answer
 
v4
Comments
jerahmeel salayog 14-Nov-13 8:30am    
thanks! one more favor. how can I get the value of cons()?
Mike Meinz 14-Nov-13 8:42am    
cons is an array of data type String. You can address individual elements using the syntax cons(index) where index is a value from 0 to cons.GetUpperBound(0). See documentation for Array.GetUpperBound for more information.
jerahmeel salayog 14-Nov-13 8:44am    
but how? i have connection string after that test()..

Public dbconn As New SqlClient.SqlConnection("Data Source=" & cons(0) & "; Initial Catalog=" & cons(1) & ";User Id=" & cons(2) & "; Password=" & cons(3) & ";")
Mike Meinz 14-Nov-13 9:19am    
I don't understand your issue. It looks like the statement in your posting just above this reply does create a connection string from the contents of the cons() array.
jerahmeel salayog 14-Nov-13 9:26am    
yes, you're right!.. my connection string is located in module.vb file..

or maybe, you can suggest other way to have connection string info from textfile?

thanks!

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