首页 > 解决方案 > 如何将组合框数据库值链接到文本框?

问题描述

使用我使用的这个数据库并将其链接到组合框,我不知道非组织组合框内容的问题是什么(一些值在行中,其他值带有输入)那么如何解决这个问题?我将组合框值链接到文本框,因此当我从组合框中选择数据库值以在文本框中显示与该值相关的信息时,会出现错误消息(指定的转换无效)。换句话说,当我运行主窗口时,信息不会出现在文本框中。

我使用的数据库。

当我尝试选择一个值(无组织)时,组合框看起来像这样。

MainWindow 桌面应用程序显示。

MainWindow.xaml.cs 代码

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.IO;
using Microsoft.Win32;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Data;

namespace BV_Desktop
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
 

        public MainWindow()
        {
            InitializeComponent();
            fillcombobox();

        }

        private void Window_Loaded(object sender, EventArgs e)
        {
            comboBox1.Items.Insert(0, "--Select--");
            comboBox1.SelectedIndex = 0;
        }
        public void fillcombobox()
        {
            SqlConnection conn = new SqlConnection("Data Source=DESKTOP-7J69E9N;Initial Catalog=DatabaseInfor;Integrated Security=True");
            string sql = "Select * from Tb_Sub";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader myreader;

            try
            {
                conn.Open();
                myreader = cmd.ExecuteReader();
                while (myreader.Read())
                {
                    string sname = myreader.GetString(1);
                    comboBox1.Items.Add(sname);

                }

            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }

        }

        private void comboBox1_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {

            SqlConnection conn = new SqlConnection("Data Source=DESKTOP-7J69E9N;Initial Catalog=DatabaseInfor;Integrated Security=True");
            string sql = "Select * from Tb_Sub where Subcontractor='" + comboBox1.Text + "';";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader myreader;

            try
            {
                conn.Open();
                myreader = cmd.ExecuteReader();
                while (myreader.Read())
                {
                    string clients = myreader.GetInt32(2).ToString();
                    string contactdetails = myreader.GetInt32(3).ToString();
                    string ponum = myreader.GetInt32(4).ToString();
                    string scope = myreader.GetInt32(5).ToString();
                    string projectno = myreader.GetInt32(6).ToString();
                    string zigno = myreader.GetInt32(7).ToString();
                    string amount = myreader.GetInt32(8).ToString();
                    string invoicedate = myreader.GetInt32(9).ToString();
                    string status = myreader.GetInt32(10).ToString();

                    txtClients.Text = clients;
                    txtDetails.Text = contactdetails;
                    txtPO.Text = ponum;
                    txtScope.Text = scope;
                    txtProjectNo.Text = projectno;
                    txtZIG.Text = zigno;
                    txtAmount.Text = amount;
                    txtInvoiceDate.Text = invoicedate;
                    txtStatus.Text = status;


                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }

        }
    }
}

MainWindow.xaml.cs 的编辑代码

   namespace BV_Desktop
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        SqlConnection con = new SqlConnection("Data Source=DESKTOP-7J69E9N;Initial Catalog=DatabaseInfor;Integrated Security=True");
        SqlCommand cmd;

        public MainWindow()
        {
            InitializeComponent();
            fillcombobox();

        }

        private void Window_Loaded(object sender, EventArgs e)
        {
           
        }
        public void fillcombobox()
        {
            SqlConnection conn = new SqlConnection("Data Source=DESKTOP-7J69E9N;Initial Catalog=DatabaseInfor;Integrated Security=True");
            string sql = "Select * from Tb_Sub";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader myreader;

            try
            {
                conn.Open();
                myreader = cmd.ExecuteReader();
                while (myreader.Read())
                {
                    string sname = myreader.GetString(1).Trim();
                    comboBox1.Items.Add(sname);

                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }

        }

        private void comboBox1_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {

            SqlConnection conn = new SqlConnection("Data Source=DESKTOP-7J69E9N;Initial Catalog=DatabaseInfor;Integrated Security=True");
            string sql = "Select * from Tb_Sub where Subcontractor='" + comboBox1.SelectedItem + "';";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader myreader;

            try
            {
                conn.Open();
                myreader = cmd.ExecuteReader();
                while (myreader.Read())
                {
                    string clients = myreader.IsDBNull(2) ? null : myreader.GetString(2);
                    string contactdetails = myreader.IsDBNull(3) ? null : myreader.GetString(3);
                    string ponum = myreader.IsDBNull(4) ? null : myreader.GetString(4);
                    string scope = myreader.IsDBNull(5) ? null : myreader.GetString(5);
                    string projectno = myreader.IsDBNull(6) ? null : myreader.GetString(6);
                    string zigno = myreader.IsDBNull(7) ? null : myreader.GetInt32(7).ToString();
                    string amount = myreader.IsDBNull(8) ? null : myreader.GetSqlMoney(8).ToString();
                    string invoicedate = myreader.IsDBNull(9) ? null : myreader.GetDateTime(9).ToShortDateString();
                    string status = myreader.IsDBNull(10) ? null : myreader.GetString(10).ToString();


                    txtClients.Text = clients;
                    txtDetails.Text = contactdetails;
                    txtPO.Text = ponum;
                    txtScope.Text = scope;
                    txtProjectNo.Text = projectno;
                    txtZIG.Text = zigno;
                    txtAmount.Text = amount;
                    txtInvoiceDate.Text = invoicedate;
                    txtStatus.Text = status;


                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }

        }
}

这是第 num=17 行的主窗口显示。

标签: c#wpfxaml

解决方案


根据您的代码更改行

string sname = myreader.GetString(1);

string sname = myreader.GetString(1).Trim();

您不能对每列使用 GetInt32,您需要使用 .net(c#) 类型来匹配数据库列的类型,例如您需要键入的 varchar 或 nvarcher 类型列

string contactdetails = myreader.GetString(3);

这是数据类型映射的链接

https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql-server-data-type-mappings

并且您的表格中有空值

所以你也需要检查空值

string contactdetails =  myreader.IsDBNull(3) ? null : myreader.GetString(3);

清空文本框

private void comboBox1_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {

            SqlConnection conn = new SqlConnection("Data Source=DESKTOP-7J69E9N;Initial Catalog=DatabaseInfor;Integrated Security=True");
            string sql = "Select * from Tb_Sub where Subcontractor='" + comboBox1.SelectedItem + "';";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader myreader;

            try
            {

                    txtClients.Text = string.Empty;
                    txtDetails.Text = string.Empty;
                    txtPO.Text = string.Empty;
                    txtScope.Text = string.Empty;
                    txtProjectNo.Text = string.Empty;
                    txtZIG.Text = string.Empty;
                    txtAmount.Text = string.Empty;
                    txtInvoiceDate.Text = string.Empty;
                    txtStatus.Text = string.Empty;

                conn.Open();
                myreader = cmd.ExecuteReader();
                while (myreader.Read())
                {
                    string clients = myreader.IsDBNull(2) ? null : myreader.GetString(2);
                    string contactdetails = myreader.IsDBNull(3) ? null : myreader.GetString(3);
                    string ponum = myreader.IsDBNull(4) ? null : myreader.GetString(4);
                    string scope = myreader.IsDBNull(5) ? null : myreader.GetString(5);
                    string projectno = myreader.IsDBNull(6) ? null : myreader.GetString(6);
                    string zigno = myreader.IsDBNull(7) ? null : myreader.GetInt32(7).ToString();
                    string amount = myreader.IsDBNull(8) ? null : myreader.GetSqlMoney(8).ToString();
                    string invoicedate = myreader.IsDBNull(9) ? null : myreader.GetDateTime(9).ToShortDateString();
                    string status = myreader.IsDBNull(10) ? null : myreader.GetString(10).ToString();


                    txtClients.Text = clients;
                    txtDetails.Text = contactdetails;
                    txtPO.Text = ponum;
                    txtScope.Text = scope;
                    txtProjectNo.Text = projectno;
                    txtZIG.Text = zigno;
                    txtAmount.Text = amount;
                    txtInvoiceDate.Text = invoicedate;
                    txtStatus.Text = status;


                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }

        }

推荐阅读