Probleme mit dem Designer
-
Hallo
ich habe ein Problem mit meinem Designer.
Als Fehlermeldung bekomm ich: "Object reference not set to an instance of an object."
Ich muss ein Programm meines ehemaligen Kollegens weiter entwickeln.
Dieses ist wie folgt aufgebaut:in der Program.cs wird die MainWindow instanziert,
des weiteren wird hier ein xml file für die datenbank konfig geladen:public static MainWindow mainform; public static DBConfig dbconf; [STAThread] static void Main(string[] args) { try { SerializationDBConfig.Load(MainWindow.HomeDirectory + "\\Settings\\DB.xml"); } catch { Program.dbconf = new DBConfig(); } Program.mainform = new MainWindow(rights); }
also info hier noch die DBConfig klasse:
namespace AvsSerial.DB { [Serializable] public class DBConfig { public DBConfig() { this.IP = "192.168.4.1"; this.TNSName = "XE"; } public string IP { get; set; } public string TNSName { get; set; } } }
und hier die Load Finktion:
/// <summary> /// Laden der individuellen Geräteeinstellungen. /// Schlägt das Laden fehl, werden die Default-Werte zurückgegeben. /// </summary> /// <param name="file">deren Dateiname</param> public static void Load(string file) { Program.dbconf = new DBConfig(); try { // Deserialize XML file to a new object. using (StreamReader sr = new StreamReader(file, Encoding.Default)) { XmlSerializer x = new XmlSerializer(Program.dbconf.GetType()); Program.dbconf = (DBConfig)x.Deserialize(sr); } } catch (Exception ex) { Debug.Print(""+ex.Message+"\n"); // Die Settings-Instanz ist nicht valid, // nix tun und Default-Werte verwenden } }
so desweiteren wird dann in der Mainform ein UserControl initialisiert:
// uControlSetup1 // resources.ApplyResources(this.uControlSetup1, "uControlSetup1"); this.uControlSetup1.btnReady_Click = null; this.uControlSetup1.Name = "uControlSetup1"; this.uControlSetup1.Load += new System.EventHandler(this.uControlSetup1_Load); // // MainWindow // this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; resources.ApplyResources(this, "$this"); this.Controls.Add(this.panel1); this.Controls.Add(this.uControlSetup1); this.DoubleBuffered = true; this.Name = "MainWindow"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainWindow_FormClosing); this.Leave += new System.EventHandler(this.FormSerial_Leave); this.panel1.ResumeLayout(false); this.ResumeLayout(false); } #endregion private System.Windows.Forms.Panel panel1; private System.Windows.Forms.Button button1; public UControlTnT uControlSetup1;
in UControlTnT wird die DBData initialisiert:
private DbData data = null; public UControlTnT() { data = new DbData(); }
in DbData wird nun die DB Verbindung hergestellt:
public class DbData { public DbData() { initDB(); } public void initDB() { if (_conn == null) { //ACHTUNG: Auskomentieren, wenn man den Designer für das MainWindow benötigt!!! if (!MainWindow.designMode) { dbip = Program.dbconf.IP; dbservice = Program.dbconf.TNSName; } string connString = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" + dbip + ")(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SID=" + dbservice + ")));User Id=user;Password=pw;"; _conn = new OracleConnection(connString); //_oConnState = _conn.State; _oConnState = "Open"; _noConnect = false; _dbAction = false; try { if (_conn.State != ConnectionState.Open) { _conn.Open(); } } catch (Exception Ex) { MessageBox.Show("Error message from Oracle client:\n" + Ex.Message, "Connection error", MessageBoxButtons.OK, MessageBoxIcon.Error); } if (_conn.State == ConnectionState.Open) { string[] settings = {"alter session set nls_sort=german", "alter session set nls_currency='€'", "alter session set nls_date_language=german" }; foreach (string nls in settings) { OracleCommand dbcmd = new OracleCommand(nls, _conn); try { dbcmd.ExecuteNonQuery(); } catch { } } } DataTable test = this.execQuery("select count(*) from user_tables", false); if (test.Rows.Count < 1) { _oConnState = "Close"; _noConnect = true; } } } }
genau hier gibt es das problem:
dbip = Program.dbconf.IP;
dbservice = Program.dbconf.TNSName;Dies wird mir im Designer als Fehler angezeigt.
Wieso?
Mfg Kaladial
-
Hallo,
zur Anzeige der Form im Designer verwendet die IDE den Form-Code. - Durch das anzeigen, wird das Load ausgeführt.
Damit wird dein Programmablauf der im Load steht schon zur Design-Zeit ausgeführt. Prüfe vor Ausführung ob du im Designer bist. Damit solltest du das Problem gelöst kriegen.