Changing Connectionstring propertys RT?????

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SurfCoder

    #1

    Changing Connectionstring propertys RT?????

    Scenario: i have a little programm that uses OleDb.connectio n 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(obje ct sender, System.EventArg s e)

    {

    string Serverpath;

    try

    {

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

    // The using statement also closes the StreamReader.

    using(StreamRea der sr = new StreamReader("S erverpath.txt") )

    {

    Serverpath = sr.ReadLine();

    }


    setpath(Serverp ath);




    }

    catch(Exception b)

    {


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

    }

    label8.Text = Data_Connection .ConnectionStri ng;

    }

    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\Visua l Studio
    Projects\Window sApplication3\F orm1.cs(722): Property or indexer
    'System.Data.Ol eDb.OleDbConnec tion.DataSource ' cannot be assigned to -- it
    is read only

    Is there anyone who can help me!!!




  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Changing Connectionstrin g propertys RT?????

    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.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - nicholas.paldin o@exisconsultin g.com

    "SurfCoder" <Surfpicker2000 @yahoo.de> wrote in message
    news:uGEL$d0VDH A.612@TK2MSFTNG P10.phx.gbl...[color=blue]
    > Scenario: i have a little programm that uses OleDb.connectio n to connect[/color]
    to[color=blue]
    > a database... the datasource is C:\programm Files\Database. mdb now i want[/color]
    to[color=blue]
    > 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(obje ct sender, System.EventArg s e)
    >
    > {
    >
    > string Serverpath;
    >
    > try
    >
    > {
    >
    > // Create an instance of StreamReader to read from a file.
    >
    > // The using statement also closes the StreamReader.
    >
    > using(StreamRea der sr = new StreamReader("S erverpath.txt") )
    >
    > {
    >
    > Serverpath = sr.ReadLine();
    >
    > }
    >
    >
    > setpath(Serverp ath);
    >
    >
    >
    >
    > }
    >
    > catch(Exception b)
    >
    > {
    >
    >
    > MessageBox.Show ("File Could not be Read " + b);
    >
    > }
    >
    > label8.Text = Data_Connection .ConnectionStri ng;
    >
    > }
    >
    > 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\Visua l Studio
    > Projects\Window sApplication3\F orm1.cs(722): Property or indexer
    > 'System.Data.Ol eDb.OleDbConnec tion.DataSource ' cannot be assigned to -- it
    > is read only
    >
    > Is there anyone who can help me!!!
    >
    >
    >
    >[/color]


    Comment

    Working...