简体   繁体   中英

How to read Xml file before execute Form_Load?

I have an C# application and use a xml file to set the connection string to my sql Database. The database fills datagridviews with tableadapters. I want the connection string to be set before the datagridview is filled because i have a Event of CellValueChanged of the DataGridView.

My code as it stands for the From_Load now is:

xmldoc.Load("D:\\XML\\paths.xml");
            XmlNode node = xmldoc.DocumentElement.SelectSingleNode("/paths/sqlconnection");
            sqlconnect = node.InnerText;
            cn = new SqlCeConnection("Data Source=" + sqlconnect);


            // TODO: Diese Codezeile lädt Daten in die Tabelle "database1DataSet.Raum". Sie können sie bei Bedarf verschieben oder entfernen.
            this.raumTableAdapter1.Fill(this.database1DataSet.Raum);
            // TODO: Diese Codezeile lädt Daten in die Tabelle "database1DataSet.Firma". Sie können sie bei Bedarf verschieben oder entfernen.
            this.firmaTableAdapter1.Fill(this.database1DataSet.Firma);
            // TODO: Diese Codezeile lädt Daten in die Tabelle "database1DataSet.Kunde". Sie können sie bei Bedarf verschieben oder entfernen.
            this.kundeTableAdapter1.Fill(this.database1DataSet.Kunde);
            // TODO: Diese Codezeile lädt Daten in die Tabelle "database1DataSet.Ansprechperson". Sie können sie bei Bedarf verschieben oder entfernen.
            this.ansprechpersonTableAdapter1.Fill(this.database1DataSet.Ansprechperson);

But I get an error that the connection string is not set, at the time the CellValueChanged.

My XML File is set up like this:

<?xml version="1.0" encoding="UTF-8"?>
- <paths>
    <sqlconnection>D:\\BDTWelcome - Kopie 2.0 fixed\\BDTWelcome\\Database1.sdf</sqlconnection>
    <ExcelVorlagen>D:\\BröExcelVorlagen</ExcelVorlagen>
  </paths>

It would be awesome if you could tell me where my mistake is.

Go to the Programm.cs file of your winfows forms project. It will look like:

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

The relevant part is the last line of the Main() . Here the form gets created. You need to load your XML file before the constructor of the form is called. A possible code could look like:

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    xmldoc.Load("D:\\XML\\paths.xml");
        XmlNode node = xmldoc.DocumentElement.SelectSingleNode("/paths/sqlconnection");
        sqlconnect = node.InnerText;
        cn = new SqlCeConnection("Data Source=" + sqlconnect);

    Application.Run(new Form1(XmlNode));
}

Pass the loaded XmlNode (or whatever you want) to the form throught it´s constructor. Therefore you have to add a constructor in your Form1.designer.cs file and handle the passed data.

I have a program doing nearly the same. I establish my connection like this:

/// <summary>
    /// Datenbank wird geöffnet.
    /// </summary>
    /// <param name="sCon">Verbindungsstring.</param>
    /// <returns>Erfolgreich?</returns>
    public bool OpenDBConnect(string sCon)
    {
        bool bOK = false;

        try
        {
            _conn = new SqlConnection(sCon);
            _sConn = sCon;
            _conn.Open();
            bOK = true;
        }
        catch (Exception ex)
        {
            _Logger.Log(Properties.EnglishStringResource.ConnectionFailed + ex.Message);                
            bOK = false;
        }
        return bOK;
    }


After that you have to tell you dataset to use this connection of course, otherwise there will be no data in the dataset and you will get the error that the connectionstring is not set for your dataset! It want´s to change the data in your grid (because propably you bind it to the grid), but can´t get any data because of the missing connection.

Setting the connectionstring of a designer made dataset can be done like this:

[ApplicationNamespace].Properties.Settings.Default["ConnectionString"] = newconstr;

The solution which worked for me is to simply create a function which reads the xlm file and sets the connection string right before the connection string is used for the first time.

public void readconnectionstring()
        {
            xmldoc.Load("D:\\XML\\paths.xml");
            XmlNode node = xmldoc.DocumentElement.SelectSingleNode("/paths/sqlconnection");
            sqlconnect = node.InnerText;
            cn = new SqlCeConnection("Data Source=" + sqlconnect);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM