将用户控件拖到主窗体时,未能创建组件“payfee”

t2a7ltrp  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(255)

我正在使用一个类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;

    }
}
}

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

c6ubokkw

c6ubokkw1#

由于payfeeclass.cs中的以下行引发了异常:

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

visual studio使用的app.config的connectionString部分(请注意,不是应用程序使用的部分)没有连接字符串项 connstr 有两个选项可以解决此问题:第一个选项,将设置连接字符串属性的行移到单独的位置 Initialise 从窗体类调用的函数。
第二个选项是检查usercontrol是否处于designmode。
把它放在控件构造函数中,只在运行时而不是设计时检索连接字符串。

bool isDesignMode = (LicenseManager.UsageMode == LicenseUsageMode.Designtime) || DesignMode;

if (!isDesignMode)
{
    myconnstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
}

相关问题