将列表框绑定到数据源后为空(SQL 服务器数据库):C#,Windows 表单

Listbox is empty after binding it to a data source (SQL Server database): C#, Windows Forms

每当我将值插入我的表单文本框并将该信息传递到下一个表单时,数据应该绑定到的列表框是空的。我已明确说明前一个表单中的哪些输入值应出现在列表框中,但似乎什么也没有发生。

我基本上是在尝试在整个应用程序的不同时间将数据存储在同一个数据库中。以下是与该问题相关的代码。

属性

public class AccountInfo
{
   public string name { get; set; }
   public string surname { get; set; }
   public string email { get ; set; }
   public string password { get; set; }
   public string dateOfBirth { get; set; }
   public string cardType { get; set; }
   public string cardNumber { get; set; }
   public string expiryDate { get; set; }
   public string ccv { get; set; }
   public string duration { get; set; }

   public string FullInfo
   {
      get
      {
         email = Form1.SetValueForText1;
         return $" {name} {surname} ({ email }) ({dateOfBirth}) ({cardType}) ({cardNumber}) ({expiryDate}) ({duration})";
      }
    }
}

此函数将值插入数据库

public void InsertNewCardDetails(string card_Type, string card_Number, string expiry_date, string _ccv, string _duration)
{
    using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\nauri\source\repos\StylistsApp\Database\UserAccountDatabase.mdf;Integrated Security=True;Connect Timeout=30"))
    {
        List<AccountInfo> accounts = new List<AccountInfo>();
        accounts.Add(new AccountInfo {cardType = card_Type, cardNumber = card_Number, expiryDate = expiry_date, ccv = _ccv, duration = _duration });
        connection.Execute("INSERT INTO[Table] (cardType, cardNumber, expiryDate, ccv, duration) VALUES(@cardType, @cardNumber, @expiryDate, @ccv, @duration)", accounts);
    }
}

将数据绑定到输入字段

namespace StylistsApp
{
    public partial class CardDetails : Form
    {
        public CardDetails()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataAccess db = new DataAccess();
            SetValueForCardType = comboBox1.SelectedItem.ToString(); 
            SetValueForCardNumber = textBox5.Text;
            SetValueForExpiryDate = textBox2.Text;
            SetValueForCCV = textBox3.Text;
            SetValueForDuration = comboBox2.SelectedItem.ToString();
            db.InsertNewCardDetails(comboBox1.SelectedItem.ToString(), textBox5.Text, textBox2.Text, textBox3.Text, comboBox2.SelectedItem.ToString());
            //MessageBox.Show("Account Created Successfully!");

            this.Close();
            Summary summary = new Summary();
            summary.Show();
        }    

        public static string SetValueForCardType = "";
        public static string SetValueForCardNumber = "";
        public static string SetValueForExpiryDate = "";
        public static string SetValueForCCV = "";
        public static string SetValueForDuration = "";
    }
}

正在尝试将上一个表单中的数据绑定到当前表单列表框。如果 this.listBox1.DataSource = null;this.listBox1.Items.Clear(); 被移除,程序抛出一个

System.ArgumentException: 'Items collection cannot be modified when the DataSource property is set.'

上线

this.listBox1.Items.Add(da.GetValue(0).ToString());

代码:

    public partial class Summary : Form
    {
        List<AccountInfo> people = new List<AccountInfo>();
        //private BindingList<string> items = new BindingList<string>();
        public Summary()
        {
            InitializeComponent();
        }

        private void Summary_Load(object sender, EventArgs e)
        {
            SqlConnection sql = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\nauri\source\repos\StylistsApp\Database\UserAccountDatabase.mdf;Integrated Security=True;Connect Timeout=30");
            sql.Open();
            SqlCommand sqa = new SqlCommand("Select cardType, cardNumber, expiryDate, ccv, duration from [Table] where email =@email", sql);
            sqa.Parameters.AddWithValue("@email", Form1.SetValueForText1);        
            SqlDataReader da = sqa.ExecuteReader();
            
            while (da.Read())
            {
                this.listBox1.DataSource = null;
                this.listBox1.Items.Clear(); 
     
                this.listBox1.Items.Add(da.GetValue(0).ToString());
                this.listBox1.Items.Add(da.GetValue(1).ToString());
                this.listBox1.Items.Add(da.GetValue(2).ToString());
                this.listBox1.Items.Add(da.GetValue(3).ToString());
                this.listBox1.Items.Add(da.GetValue(4).ToString());
            }
            sql.Close();
        }
    }

结果如截图所示

有什么想法吗?非常感谢您的帮助!

如果您计划在整个应用程序的不同位置将数据存储在同一个数据库中,请创建一个单独的数据库。它会让你省去头痛。