Tìm lỗi WPF connect SQL server data

2cb281e610d9b6552714506b3090e4394e284908_2_690x371
dạ cho em hỏi code bên dưới sao kết quả chọn từ combobox ( lấy từ sql data)
hiện lên textbox không khớp ạ ?? thank mọi người!!!
code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
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.Data.SqlClient;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace comboboxapp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public SimpleMath Formular { get; set; }
        public MainWindow()
        {
            Formular = new SimpleMath()
            {
                Txtcode = 0,
                Txtpieces = 0,
                Txtlayers = 0,
                Txtproductionpieces = 0,
                Txtseccond = 0,



            };
            InitializeComponent();
            DataContext = Formular;
            fillcombobox();
        }


        private void MainWindow_Load(object sender, EventArgs e)
        {

        }
        public void fillcombobox()
        {
            SqlConnection con = new SqlConnection("Data Source=LEAN-22\\SQLEXPRESS;Initial Catalog=LUAT;Integrated Security=True");
            string sql = " select * from comboboxnew ";
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataReader myreader;
            try
            {
                con.Open();
                myreader = cmd.ExecuteReader();
                while (myreader.Read())
                {
                    string sname = myreader.GetInt32(0).ToString();
                    comboBox1.Items.Add(sname);

                }

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

            }
        }



        private void ComboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=LEAN-22\\SQLEXPRESS;Initial Catalog=LUAT;Integrated Security=True");
            string sql = " select * from comboboxnew where code = '"  +comboBox1.Text+  "';";
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataReader myreader;
            try
            {
                con.Open();
                myreader = cmd.ExecuteReader();
                while (myreader.Read())
                {
                    string code = myreader.GetInt32(0).ToString();
                    string pieces = myreader.GetInt32(1).ToString();
                    string layers = myreader.GetInt32(2).ToString();
                    string productionpieces = myreader.GetInt32(3).ToString();
                    string seccond = myreader.GetInt32(4).ToString();
                    txtcode.Text = code;
                    txtpieces.Text = pieces;
                    txtlayers.Text = layers;
                    txtproductionpieces.Text = productionpieces;
                    txtseccond.Text = seccond;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public class SimpleMath : INotifyPropertyChanged
        {
            private int txtcode;

            public int Txtcode
            {
                get { return txtcode; }
                set
                {
                    txtcode = value;
                    OnPropertyChanged("A");
                    OnPropertyChanged("Txtcode");

                }
            }
            private int txtpieces;

            public int Txtpieces
            {
                get { return txtpieces; }
                set
                {
                    txtpieces = value;
                    OnPropertyChanged("A");
                    OnPropertyChanged("Txtpieces");

                }
            }
            private int txtlayers;

            public int Txtlayers
            {
                get { return txtlayers; }
                set
                {
                    txtlayers = value;
                    OnPropertyChanged("A");
                    OnPropertyChanged("Txtlayers");

                }
            }
            private int txtproductionpieces;

            public int Txtproductionpieces
            {
                get { return txtproductionpieces; }
                set
                {
                    txtproductionpieces = value;
                    OnPropertyChanged("A");
                    OnPropertyChanged("Txtproductionpieces");

                }
            }
            private int txtseccond;

            public int Txtseccond
            {
                get { return txtseccond; }
                set
                {
                    txtseccond = value;
                    OnPropertyChanged("A");
                    OnPropertyChanged("Txtseccond");

                }
            }
        

            public double A => txtcode - txtlayers;

            public event PropertyChangedEventHandler PropertyChanged;
            public void OnPropertyChanged([CallerMemberName()] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }

        }
    }
}

Bạn phải nói vấn đề gặp phải chứ!!! ~~~

có ghi đầy đủ mà anh !

không khớp là không khớp như thế nào? Bạn phải đưa ra mong muốn là gì thì mn mới chỉ được.

dạ như trên table sql server, chọn code cùng hàng 1( muốn hiển thị nội dung hàng 1), nhưng nó lại hiển thị hàng thứ 2.
hiển thị thông tin rất ngẫu nhiên không cùng thông tin trong combobox ?

như trên hình combobox chọn nội dung hàng thứ 4, như các textbox lại là nội dung hàng thứ 1.

bạn nên truyền vào name của column đừng dùng index

3 Likes

dạ em chưa hiểu chổ này a có thể nói rỏ hơn ko ạ ?

em đang truyền từ name của column đó anh

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?