Changing Connectionstring propertys RT?????

S

SurfCoder

Scenario: i have a little programm that uses OleDb.connection to connect to
a database... the datasource is C:\programm Files\Database.mdb now i want to
change the datasource for the connection while i am reading the datasource
string from a file !!!! Cause the user want to put the database a place i
dont noe yet!!!(e.g a fileserver on the network!)

Code:
private void Form1_Load(object sender, System.EventArgs e)

{

string Serverpath;

try

{

// Create an instance of StreamReader to read from a file.

// The using statement also closes the StreamReader.

using(StreamReader sr = new StreamReader("Serverpath.txt"))

{

Serverpath = sr.ReadLine();

}


setpath(Serverpath);




}

catch(Exception b)

{


MessageBox.Show("File Could not be Read " + b);

}

label8.Text = Data_Connection.ConnectionString;

}

private void setpath(string str)

{

Data_Connection.DataSource = str; // variable from textfile (path)


}


C# says this while i am compiling

C:\Documents and Settings\f28403\My Documents\Visual Studio
Projects\WindowsApplication3\Form1.cs(722): Property or indexer
'System.Data.OleDb.OleDbConnection.DataSource' cannot be assigned to -- it
is read only

Is there anyone who can help me!!!
 
N

Nicholas Paldino [.NET/C# MVP]

SurfCoder,

What you should do is create a new instance of the OleDbConnection,
using the connection string that has the location of the new data. Then,
you can set Data_Connection to this new connection.

Generally speaking, I think it is a better idea to create a new
connection, open it, close it, and then release it, and not have one
connection lying around that you use for everything.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top