IMS.exe中发生了类型为'System.TypeInitializationException'的未处理异常

时间:2019-06-03 22:34:51

标签: c#

我正在研究库存管理系统。我制作了一个MDI表单,该表单具有一个带有文件的菜单条,该文件具有两个子菜单设置并退出。如果我的连接字符串在我的文档文件夹中退出,则打开登录表单窗口,否则打开设置表单。代码如下。

MDI.cs

namespace IMS
{
    public partial class MDI : Form
    {
        public MDI()
        {
            InitializeComponent();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void MDI_Load(object sender, EventArgs e)
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            if(File.Exists(path+"\\connection"))
            {
                Login log = new Login();
                MainClass.ShowWindow(log, this);
            }
            else
            {
                Setting set = new Setting();
                MainClass.ShowWindow(set, this);
            }

        }

        private void settingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Setting set = new Setting();
            MainClass.ShowWindow(set, this);
        }
    }
}
MDIDesigner.cs

namespace IMS
{
    partial class MDI
    {

        private System.ComponentModel.IContainer components = null;


        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code


        private void InitializeComponent()
        {
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.settingToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.menuStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // menuStrip1
            // 
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fileToolStripMenuItem});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(697, 24);
            this.menuStrip1.TabIndex = 1;
            this.menuStrip1.Text = "myMenu";

            this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.settingToolStripMenuItem,
            this.exitToolStripMenuItem});
            this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
            this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20);
            this.fileToolStripMenuItem.Text = "File";

            this.settingToolStripMenuItem.Name = "settingToolStripMenuItem";
            this.settingToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.S)));
            this.settingToolStripMenuItem.Size = new System.Drawing.Size(151, 22);
            this.settingToolStripMenuItem.Text = "Setting";
            this.settingToolStripMenuItem.Click += new System.EventHandler(this.settingToolStripMenuItem_Click);
            // 
            // exitToolStripMenuItem
            // 
            this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
            this.exitToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.X)));
            this.exitToolStripMenuItem.Size = new System.Drawing.Size(151, 22);
            this.exitToolStripMenuItem.Text = "Exit";
            this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
            // 
            // MDI
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(697, 502);
            this.Controls.Add(this.menuStrip1);
            this.Font = new System.Drawing.Font("Segoe UI", 8F, System.Drawing.FontStyle.Strikeout, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.IsMdiContainer = true;
            this.MainMenuStrip = this.menuStrip1;
            this.Name = "MDI";
            this.Text = "Welcome";
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.Load += new System.EventHandler(this.MDI_Load);
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.MenuStrip menuStrip1;
        private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem settingToolStripMenuItem;
    }
}

Setting.cs

namespace IMS
{
    public partial class Setting : Sample
    {
        public Setting()
        {
            InitializeComponent();
        }

        private void leftPanel_Paint(object sender, PaintEventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            string s;
            string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            if(ISCb.Checked)
            {
                if(ServerText.Text !=  "" && DatabaseText.Text != "")
                {
                    s = "Data Source = " + ServerText.Text + "; Initial Catalog = " + DatabaseText.Text + "; Integrated Security = True;";
                    File.WriteAllText(path + "\\connection", s);
                    DialogResult dr = MessageBox.Show("Setting Save Successfully......", "Information......", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if(dr == DialogResult.OK)
                    {
                        Login log = new Login();
                        MainClass.ShowWindow(log, this, MDI.ActiveForm);
                    }

                }
                else
                {
                    MessageBox.Show("Please give complete data to continue..........");
                }

            }
            else
            {
                if (ServerText.Text != "" && DatabaseText.Text != "" && UserIdText.Text != "" && PasswordText.Text != "")
                {
                    s = "Data Source = " + ServerText.Text + "; Initial Catalog = " + DatabaseText.Text + "; User ID = " +UserIdText.Text+"; Password = "+ PasswordText.Text+";";
                    File.WriteAllText(path +"\\connection", s);
                    DialogResult dr = MessageBox.Show("Setting Save Successfully......", "Information......", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (dr == DialogResult.OK)
                    {
                        Login log = new Login();
                        MainClass.ShowWindow(log, this, MDI.ActiveForm);
                    }

                }
                else
                {
                    MessageBox.Show("Please give complete data to continue..........");
                }
            }
        }

        private void ISCb_CheckedChanged(object sender, EventArgs e)
        {
            if(ISCb.Checked)
            {
                UserIdText.Enabled = false;
                PasswordText.Enabled = false;
                UserIdText.Text = "";
                PasswordText.Text = "";
            }
            else
            {
                UserIdText.Enabled = true;
                PasswordText.Enabled = true;
            }
        }
    }
}

setting.designer.cs

namespace IMS
{
    partial class Setting
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.label3 = new System.Windows.Forms.Label();
            this.ServerText = new System.Windows.Forms.TextBox();
            this.DatabaseText = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.label5 = new System.Windows.Forms.Label();
            this.UserIdText = new System.Windows.Forms.TextBox();
            this.label6 = new System.Windows.Forms.Label();
            this.PasswordText = new System.Windows.Forms.TextBox();
            this.label7 = new System.Windows.Forms.Label();
            this.ISCb = new System.Windows.Forms.CheckBox();
            this.SaveBtn = new System.Windows.Forms.Button();
            this.leftPanel.SuspendLayout();
            this.panel1.SuspendLayout();
            this.RightPanel.SuspendLayout();
            this.panel2.SuspendLayout();
            this.SuspendLayout();
            // 
            // leftPanel
            // 
            this.leftPanel.Controls.Add(this.SaveBtn);
            this.leftPanel.Controls.Add(this.ISCb);
            this.leftPanel.Controls.Add(this.PasswordText);
            this.leftPanel.Controls.Add(this.label7);
            this.leftPanel.Controls.Add(this.UserIdText);
            this.leftPanel.Controls.Add(this.label6);
            this.leftPanel.Controls.Add(this.textBox3);
            this.leftPanel.Controls.Add(this.label5);
            this.leftPanel.Controls.Add(this.DatabaseText);
            this.leftPanel.Controls.Add(this.label4);
            this.leftPanel.Controls.Add(this.ServerText);
            this.leftPanel.Controls.Add(this.label3);
            this.leftPanel.Size = new System.Drawing.Size(200, 518);
            this.leftPanel.Paint += new System.Windows.Forms.PaintEventHandler(this.leftPanel_Paint);
            this.leftPanel.Controls.SetChildIndex(this.label3, 0);
            this.leftPanel.Controls.SetChildIndex(this.ServerText, 0);
            this.leftPanel.Controls.SetChildIndex(this.label4, 0);
            this.leftPanel.Controls.SetChildIndex(this.DatabaseText, 0);
            this.leftPanel.Controls.SetChildIndex(this.label5, 0);
            this.leftPanel.Controls.SetChildIndex(this.textBox3, 0);
            this.leftPanel.Controls.SetChildIndex(this.panel1, 0);
            this.leftPanel.Controls.SetChildIndex(this.label6, 0);
            this.leftPanel.Controls.SetChildIndex(this.UserIdText, 0);
            this.leftPanel.Controls.SetChildIndex(this.label7, 0);
            this.leftPanel.Controls.SetChildIndex(this.PasswordText, 0);
            this.leftPanel.Controls.SetChildIndex(this.ISCb, 0);
            this.leftPanel.Controls.SetChildIndex(this.SaveBtn, 0);
            // 
            // RightPanel
            // 
            this.RightPanel.Size = new System.Drawing.Size(561, 518);
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(6, 142);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(39, 15);
            this.label3.TabIndex = 2;
            this.label3.Text = "Server";
            // 
            // ServerText
            // 
            this.ServerText.Location = new System.Drawing.Point(6, 160);
            this.ServerText.MaxLength = 50;
            this.ServerText.Name = "ServerText";
            this.ServerText.Size = new System.Drawing.Size(188, 23);
            this.ServerText.TabIndex = 1;
            this.ServerText.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
            // 
            // DatabaseText
            // 
            this.DatabaseText.Location = new System.Drawing.Point(6, 213);
            this.DatabaseText.MaxLength = 50;
            this.DatabaseText.Name = "DatabaseText";
            this.DatabaseText.Size = new System.Drawing.Size(188, 23);
            this.DatabaseText.TabIndex = 2;
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(6, 195);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(55, 15);
            this.label4.TabIndex = 4;
            this.label4.Text = "Database";
            // 
            // textBox3
            // 
            this.textBox3.Location = new System.Drawing.Point(0, 18);
            this.textBox3.Name = "textBox3";
            this.textBox3.Size = new System.Drawing.Size(188, 23);
            this.textBox3.TabIndex = 7;
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(0, 0);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(39, 15);
            this.label5.TabIndex = 6;
            this.label5.Text = "Server";
            // 
            // UserIdText
            // 
            this.UserIdText.Location = new System.Drawing.Point(6, 266);
            this.UserIdText.MaxLength = 30;
            this.UserIdText.Name = "UserIdText";
            this.UserIdText.Size = new System.Drawing.Size(188, 23);
            this.UserIdText.TabIndex = 3;
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(6, 248);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(44, 15);
            this.label6.TabIndex = 8;
            this.label6.Text = "User ID";
            // 
            // PasswordText
            // 
            this.PasswordText.Location = new System.Drawing.Point(6, 316);
            this.PasswordText.MaxLength = 30;
            this.PasswordText.Name = "PasswordText";
            this.PasswordText.Size = new System.Drawing.Size(188, 23);
            this.PasswordText.TabIndex = 4;
            this.PasswordText.TextChanged += new System.EventHandler(this.textBox5_TextChanged);
            // 
            // label7
            // 
            this.label7.AutoSize = true;
            this.label7.Location = new System.Drawing.Point(6, 298);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(57, 15);
            this.label7.TabIndex = 10;
            this.label7.Text = "Password";
            // 
            // ISCb
            // 
            this.ISCb.AutoSize = true;
            this.ISCb.Location = new System.Drawing.Point(6, 355);
            this.ISCb.Name = "ISCb";
            this.ISCb.Size = new System.Drawing.Size(125, 19);
            this.ISCb.TabIndex = 5;
            this.ISCb.Text = "Integrated Security";
            this.ISCb.UseVisualStyleBackColor = true;
            this.ISCb.CheckedChanged += new System.EventHandler(this.ISCb_CheckedChanged);
            // 
            // SaveBtn
            // 
            this.SaveBtn.FlatAppearance.BorderSize = 2;
            this.SaveBtn.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.SaveBtn.Location = new System.Drawing.Point(9, 380);
            this.SaveBtn.Name = "SaveBtn";
            this.SaveBtn.Size = new System.Drawing.Size(179, 37);
            this.SaveBtn.TabIndex = 6;
            this.SaveBtn.Text = "SAVE";
            this.SaveBtn.UseVisualStyleBackColor = true;
            this.SaveBtn.Click += new System.EventHandler(this.SaveBtn_Click);
            // 
            // Setting
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(761, 518);
            this.Name = "Setting";
            this.Text = "Setting";
            this.leftPanel.ResumeLayout(false);
            this.leftPanel.PerformLayout();
            this.panel1.ResumeLayout(false);
            this.RightPanel.ResumeLayout(false);
            this.panel2.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox ServerText;
        private System.Windows.Forms.TextBox PasswordText;
        private System.Windows.Forms.Label label7;
        private System.Windows.Forms.TextBox UserIdText;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.TextBox textBox3;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.TextBox DatabaseText;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.CheckBox ISCb;
        private System.Windows.Forms.Button SaveBtn;
    }
}

登录代码

namespace IMS
{
    public partial class Login : Sample
    {
        public Login()
        {
            InitializeComponent();
        }

        private void loginbtn_Click(object sender, EventArgs e)
        {
            HomeScreen obj = new HomeScreen();
            MainClass.ShowWindow(obj, this, MDI.ActiveForm);
        }
    }
}

login.designer.cs
namespace IMS
{
    partial class Login
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.label3 = new System.Windows.Forms.Label();
            this.UsernameText = new System.Windows.Forms.TextBox();
            this.PasswordText = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.loginbtn = new System.Windows.Forms.Button();
            this.RightPanel.SuspendLayout();
            this.panel2.SuspendLayout();
            this.panel1.SuspendLayout();
            this.leftPanel.SuspendLayout();
            this.SuspendLayout();
            // 
            // leftPanel
            // 
            this.leftPanel.Controls.Add(this.loginbtn);
            this.leftPanel.Controls.Add(this.PasswordText);
            this.leftPanel.Controls.Add(this.label4);
            this.leftPanel.Controls.Add(this.UsernameText);
            this.leftPanel.Controls.Add(this.label3);
            this.leftPanel.Controls.SetChildIndex(this.panel1, 0);
            this.leftPanel.Controls.SetChildIndex(this.label3, 0);
            this.leftPanel.Controls.SetChildIndex(this.UsernameText, 0);
            this.leftPanel.Controls.SetChildIndex(this.label4, 0);
            this.leftPanel.Controls.SetChildIndex(this.PasswordText, 0);
            this.leftPanel.Controls.SetChildIndex(this.loginbtn, 0);
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(14, 171);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(60, 15);
            this.label3.TabIndex = 1;
            this.label3.Text = "Username";
            // 
            // UsernameText
            // 
            this.UsernameText.Location = new System.Drawing.Point(12, 189);
            this.UsernameText.MaxLength = 30;
            this.UsernameText.Name = "UsernameText";
            this.UsernameText.Size = new System.Drawing.Size(170, 23);
            this.UsernameText.TabIndex = 2;
            // 
            // PasswordText
            // 
            this.PasswordText.Location = new System.Drawing.Point(12, 233);
            this.PasswordText.MaxLength = 30;
            this.PasswordText.Name = "PasswordText";
            this.PasswordText.Size = new System.Drawing.Size(170, 23);
            this.PasswordText.TabIndex = 4;
            this.PasswordText.UseSystemPasswordChar = true;
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(12, 215);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(57, 15);
            this.label4.TabIndex = 3;
            this.label4.Text = "Password";
            // 
            // loginbtn
            // 
            this.loginbtn.FlatAppearance.BorderSize = 2;
            this.loginbtn.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.loginbtn.Location = new System.Drawing.Point(13, 274);
            this.loginbtn.Name = "loginbtn";
            this.loginbtn.Size = new System.Drawing.Size(169, 35);
            this.loginbtn.TabIndex = 5;
            this.loginbtn.Text = "Login";
            this.loginbtn.UseVisualStyleBackColor = true;
            this.loginbtn.Click += new System.EventHandler(this.loginbtn_Click);
            // 
            // Login
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(761, 496);
            this.Name = "Login";
            this.Text = "Login";
            this.RightPanel.ResumeLayout(false);
            this.panel2.ResumeLayout(false);
            this.panel1.ResumeLayout(false);
            this.leftPanel.ResumeLayout(false);
            this.leftPanel.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button loginbtn;
        private System.Windows.Forms.TextBox PasswordText;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.TextBox UsernameText;
        private System.Windows.Forms.Label label3;
    }
}

MainClass代码

namespace IMS
{
    class MainClass
    {
        public static string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        public static string s = File.ReadAllText(path + "\\connection");
        public static SqlConnection con = new SqlConnection(s);
        public static DialogResult ShowMsg(string msg, string heading, string type)
        {
            if(type == "success")
            {
                return MessageBox.Show(msg, heading, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                return MessageBox.Show(msg, heading, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public static void ShowWindow(Form openwin, Form clswin, Form MDIwin)
        {
            clswin.Close();
            openwin.MdiParent = MDIwin;
            openwin.WindowState = FormWindowState.Maximized;
            openwin.Show();
        }
        public static void ShowWindow(Form openwin, Form MDIwin)
        {

            openwin.MdiParent = MDIwin;
            openwin.WindowState = FormWindowState.Maximized;
            openwin.Show();
        }

    }
}

0 个答案:

没有答案