【问题标题】:Failed to create Component 'PayFee' when I drag the user control to the Main Form将用户控件拖到主窗体时创建组件“PayFee”失败
【发布时间】:2018-06-09 09:41:32
【问题描述】:

我正在使用 PayFeeClass 类来连接用户控件数据库,然后在 UserControl 中调用该类(即“PayFee.cs”),但它会引发错误:

这是我的“PayFee.cs”代码

using SSC_LIBRARY.Classes;
using System;
using System.Windows.Forms;

namespace SSC_LIBRARY
{

public partial class PayFee : UserControl
{


    public PayFee()
    {
        InitializeComponent();
    }
        PayFeeClass pfc = new PayFeeClass();

    public void Clear()
    {
        t1.Text = "";
        t2.Text = "";
        t3.Text = "";
        t4.Text = "";
        t5.Text = "";
        t6.Text = "";

    }
    private void PayFee_Load(object sender, EventArgs e)
    {

    }

    private void B2_Click(object sender, EventArgs e)
    {
        pfc.Fee_ID = t1.Text;
        pfc.Student_ID = t2.Text;
        pfc.ChallanNo = t3.Text;
        pfc.Amount = t4.Text;
        pfc.Due_date = t5.Value.Date;
        pfc.Fee_type = t6.selectedValue;

        bool success = pfc.Insert(pfc);
        if (success == true)
        {
            MessageBox.Show("Record Inserted");
            Clear();

        }
        else
        {
            MessageBox.Show("Record not Added. Try again!");
        }
    }

    private void t6_onItemSelected(object sender, EventArgs e)
    {

    }
}
}

这是我的 PayFeeClass 代码:

using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Data;
using System.Configuration;
using System.Windows.Forms;
namespace SSC_LIBRARY.Classes
{
class PayFeeClass
{
    public string Fee_ID { get; set; }
    public string Student_ID { get; set; }
    public string ChallanNo { get; set; }
    public string Amount { get; set; }
    public DateTime Due_date { get; set; }
    public string Fee_type { get; set; }

    static string myconnstr = 
ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;

    public DataTable Select()
    {
        //Step1: Database connection
        MySqlConnection conn = new MySqlConnection(myconnstr);
        DataTable dt = new DataTable();
        try
        {
            //Step2: Writing SQL Query
            string sql = "SELECT * FROM fee";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
            conn.Open();
            adapter.Fill(dt);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            conn.Close();
        }
        return dt;
    }
    public bool Insert(PayFeeClass pfc)
    {
        bool isSuccess = false;
        MySqlConnection conn = new MySqlConnection(myconnstr);
        try
        {
            string sql = "INSERT INTO fee  (Fee_ID, Student_ID, ChallanNo, 
Amount, Due_date, Fee_type) VALUES (@Fee_ID, @Student_ID, @ChallanNo, 
@Amount, @Due_date, @Fee_type)";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@Fee_ID", pfc.Fee_ID);
            cmd.Parameters.AddWithValue("@Student_ID", pfc.Student_ID);
            cmd.Parameters.AddWithValue("@ChallanNo", pfc.ChallanNo);
            cmd.Parameters.AddWithValue("@Amount", pfc.Amount);
            cmd.Parameters.AddWithValue("@Due_date", pfc.Due_date);
            cmd.Parameters.AddWithValue("@Fee_type", pfc.Fee_type);


            conn.Open();
            int rows = cmd.ExecuteNonQuery();
            if (rows > 0)
            {
                isSuccess = true;
            }
            else
            {
                isSuccess = false;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            conn.Close();
        }
        return isSuccess;

    }
}
}

但是当我将用户控件拖到主窗体时,它给出了错误

【问题讨论】:

    标签: c# mysql visual-studio


    【解决方案1】:

    引发异常的原因是 PayFeeClass.cs 中的以下行:

    static string myconnstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
    

    Visual Studio 使用的 app.config 的 ConnectionStrings 部分(注意,不是您的应用程序使用的部分) 没有connstr 的连接字符串条目

    您有两种解决方法: 第一个选项,将设置连接字符串属性的行移到您从 Form 类调用的单独的 Initialise 函数中。

    第二个选项是检查 UserControl 是否处于设计模式。

    把它放在你的控件构造函数中,只在运行时而不是设计时检索连接字符串。

    bool isDesignMode = (LicenseManager.UsageMode == LicenseUsageMode.Designtime) || DesignMode;
    
    if (!isDesignMode)
    {
        myconnstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
    }
    

    【讨论】:

    • 但是我在哪里粘贴您提供的代码?因为我把它绑起来了,但它现在给出了不同的错误!
    • @HamzaKhan 把它放在你的控件构造函数中,只在运行时而不是设计时检索连接字符串
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多