Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datasource for text file

Status
Not open for further replies.

prrm333

Programmer
Apr 14, 2003
97
US
I have a datasource set up to work hard coded for the path and I would like to have it set up dynamically to the application.startuppath When I set the path to a variable I get the error:

'variable_name' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.

My connection string code is as follows for the datasource:
const string CONNECTION_STRING = @"Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=c:\; Extended Properties=""text;HDR=YES;"";";

What do I need to do to get this to work?
 
How did you write it when you used "variable_name"?

------------------------------------------------------------------------------------------------------------------------
"As for the bureacratic, politically-correct, mollycoddling, asinine, Romper Room antics of...clients and management, just read up on Dilbert. It's not funny - it's a training manual."
- Mike
 
I'm using the following to actually write to the file:

FileStream file = new FileStream(fileName), FileMode.Append, FileAccess.Write);
// Create a new stream to write to the file
StreamWriter sw = new StreamWriter(file);
// Write a string to the file
sw.Write(txtDate.Text + (",") etc,etc;
// Close StreamWriter
sw.Close();
// Close file
file.Close();

I was attempting to replace the path I want for the datasource with a variable and not have to hard code it.
 
I understand. What I'm thinking is happening is that you may be hard-coding the term "variable_name" into your code instead of the variable itself. That's why I asked to see the code you were using to reference the variable_name.

------------------------------------------------------------------------------------------------------------------------
"As for the bureacratic, politically-correct, mollycoddling, asinine, Romper Room antics of...clients and management, just read up on Dilbert. It's not funny - it's a training manual."
- Mike
 
I'm using the following for the datasource and setting the file name:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
const string CONNECTION_STRING= "Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=c:\; Extended Properties=""text;HDR=YES;"";";
const string FILE_NAME = @"test99.txt";
const string QUERY = "SELECT * FROM " + FILE_NAME;

I was trying everything to replace the hardcoded c:\ above and possibly use the application.startuppath instead.

Then I'm using the following to call it for loading it to the datagridview:

string connectionString = string.Format(CONNECTION_STRING, FILE_NAME);
OleDbDataAdapter adapter = new OleDbDataAdapter(QUERY, connectionString);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
dataGridView1.DataSource = dataTable;

Finally I use the following to save a new row to the file:

FileStream file = new FileStream(fileName), FileMode.Append, FileAccess.Write);
// Create a new stream to write to the file
StreamWriter sw = new StreamWriter(file);
// Write a string to the file
sw.Write(txtDate.Text + (",") etc,etc;
// Close StreamWriter
sw.Close();
// Close file
file.Close();

 
So when you're using the application.startuppath, do you write it like this (or whatever the proper syntax may be since I'm not sure off the top of my head):
Code:
const string CONNECTION_STRING= "Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=[COLOR=red]" + application.StartUpPath + "[/color]; Extended Properties=""text;HDR=YES;"";";
or like this:
Code:
const string CONNECTION_STRING= "Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=[COLOR=red]application.StartUpPath[/color]; Extended Properties=""text;HDR=YES;"";";

------------------------------------------------------------------------------------------------------------------------
"As for the bureacratic, politically-correct, mollycoddling, asinine, Romper Room antics of...clients and management, just read up on Dilbert. It's not funny - it's a training manual."
- Mike
 
put the connection string in the config file and reference the ConfigurationManager.ConnectionStrings["[key]"]. to create the connection. then you just change the config file between boxes (local, test, staging, prod, etc).

for more info goole "reference connection string from web.confg" (or app.config).

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
My eventual goal is to use a configuration file but for now I plan to try and stick to the data source method. Either way the syntax for the 2 different data source connections gives me error most of which is that it says the application.startuppath is not a valid path.
 
I looked and tried everything I could and I can't seem to get either a string with a path or the application.startuppath to replace the hardcoded directory path I want into the datasource. Unless I can update the code to include a dynamic path that I can set outside of the datasource syntax I'm going to leave it hardcoded for now.

Any other ideas would be appreciated.
 
Code:
<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="Default" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;
            Data Source=c:\; Extended Properties=&qt;text;HDR=YES;&qt"/>
  </connectionStrings>
</configuration>
Code:
ConnectionSettings settings = ConfigurationManager.ConnectionStrings["Default"];
OleDbConnection connection = new OleDbConnection(settings.ConnectionString);

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
After experimenting the last few weeks I thought I would attempt to solve this one more time. I have put the actual connection string into a string variable. However when I try to run the code I get the following error:

A field initializer cannot reference the non-static field, method, or property 'NetCalc.Form1.filePath

The code I am using is:

public Form1()
{
InitializeComponent();
}

1. const string FILE_NAME = @"\test99.txt";
2. string filePath = Application.StartupPath + FILE_NAME;
3. string test99 = "@Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + filePath + "; Extended Properties=\"\"text;HDR=YES;\"\";";
4. const string CONNECTION_STRING = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source= c:\calc; Extended Properties=""text;HDR=YES;"";";
5. const string QUERY = "SELECT * FROM " + FILE_NAME;

The error is on the "filePath" of the #3.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top