【问题标题】:Get data from database and add it into text-box C# wpf从数据库中获取数据并将其添加到文本框 C# wpf
【发布时间】:2020-07-31 12:18:26
【问题描述】:

为什么我运行程序时文本框中没有任何内容?我在课堂上有我的方法,任何人都可以给我任何提示我可以解决这个问题。

MainWin 代码

   *List<Card> cards = new List<Card>();

    DBManager dB = new DBManager();
    public MainWindow()
    {
        Card card = new Card();

        InitializeComponent();
        dB.GetCards(cards);
        txtQuestion.Text = card.Question;
        lblCardsList.ItemsSource = cards;
        GetRandomCards();
        DisplayCardQuestion();

    }

    private void GetRandomCards()
    {
        Card card = new Card();
        Random random = new Random();
        int rndCard = random.Next(1, 100);
        for (int i = 0; i < rndCard; i++)
        {
           cards.Add(card);
        }


    }
    private void DisplayCardQuestion()
    {
        Card card = new Card();

            lblCardNum.Content =card.CardID;
            txtQuestion.Text = card.Question;
            txtTitle.Text = card.Title;

       }*

【问题讨论】:

    标签: c# sql wpf textbox sqldatareader


    【解决方案1】:

    我假设dB.GetCards(cards) 返回一个List&lt;Card&gt;。如果是这种情况,请改用cards = dB.GetCards(cards);

    此外,在您的 GetRandomCard 方法中,它会将相同的 card 添加到您的 cards List rndCard 次。如果您尝试从cards 中选择随机卡片,您应该重新考虑您的逻辑。

    【讨论】:

    • 我试过cards = dB.GetCards(cards);但没有工作
    • 这里面有太多的未知数。例如,DBManagerCard 类实际上做了什么,它们实际上是否正确实例化?我不是要求您在这里回答这个问题,而是要求您在 Visual Studio 中对其进行调试并设置断点以逐步执行您编写的每个方法并验证实际发生的情况。祝你好运!
    猜你喜欢
    • 2016-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 2013-02-03
    • 1970-01-01
    • 2018-04-25
    相关资源
    最近更新 更多