Home > Enterprise >  How to enter item from datatable into textbox
How to enter item from datatable into textbox

Time:03-10

First C# form app for me. I can write it in POSH, but I'm trying to graduate and understand C#. I know I have a long way to go, but if someone could help explain this, I would be grateful.

I have a simple SQL table with the following: CLIENT, INPATH, OUTPATH, LIMITERS (PK) LIMITERS have Unique Values For each CLIENT, INPATH and OUTPATH are the same. I am trying to create a form that does the following:

Display a listbox with distinct CLIENT -Working

--- The rest is not working ---

On ListBox Click:

Fill TextBox with INPATH for selected CLIENT

Fill TextBox with OUTPATH for selected CLIENT

Fill ListBox with LIMITERS for selected CLIENT

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Data.SqlClient;




namespace DSI_SFTP_FrontEnd
{
    public partial class ClientMain : Form
    {
        private SqlDbConnect con;
       

        public ClientMain()

        {
            InitializeComponent();
            con = new SqlDbConnect();
            con.SqlQuery("Select Distinct CLIENT from Client_Inf");
            clientListBoxValues.Items.Clear();
            foreach (DataRow dr in con.QueryEx().Rows)
            {
                clientListBoxValues.Items.Add(dr[0].ToString());
                
            }
            


        }

        private void clientListBoxValues_SelectedIndexChanged(object sender, EventArgs e)
        {
            string text = clientListBoxValues.GetItemText(clientListBoxValues.SelectedItem);
            if (text != null)


            {
                InitializeComponent();

                string sql = $"Select INPATH, OUTPATH, LIMITERS from Client_Inf where CLIENT = '{text}'";

                con = new SqlDbConnect();
                con.SqlQuery(sql);
                DataTable dataTable = con.QueryEx();

                {
                    inPathTextBoxValue.Text = dataTable.Rows[0]["INPATH"].ToString();
                    outPathTextBoxValue.Text = dataTable.Rows[0]["OUTPATH"].ToString();
                }
                foreach (DataRow dr in dataTable.Rows)
                {
                    limitersListBoxValues.Items.Add(dr[2].ToString());
                }

                
                
            }

        }

        private void createNewbutton_Click(object sender, EventArgs e)
        {
            NewClientForm frm = new NewClientForm();
            frm.ShowDialog();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}

SqlDbConnect.cs

using System.Data;

namespace DSI_SFTP_FrontEnd
{
    public class SqlDbConnect
    {
        private SqlConnection _con;
        public SqlCommand _cmd;
        private SqlDataAdapter _da;    
        private DataTable _dt;

        public SqlDbConnect()
        { 
        _con=new SqlConnection("Server=<SERVER>;Database=<DB>;Trusted_Connection=True;TrustServerCertificate=True;");   
            _con.Open();
        }

        public void SqlQuery(string queryText) 
        {
            _cmd= new SqlCommand(queryText, _con);
        }

        public DataTable QueryEx() 
        {
            _da = new SqlDataAdapter(_cmd);
            _dt= new DataTable();
            _da.Fill(_dt);
            return _dt;
        }


        public void NonQueryEx() 
        {
            _cmd.ExecuteNonQuery();
        }


    }
}

CodePudding user response:

The problem is that you are calling InitializeComponent() in clientListBoxValues_SelectedIndexChanged. InitializeComponent() creates the controls, .i.e., the TextBoxes and ListBoxes, etc. The effect is that you are creating another set of those controls every time the selected index of the ListBox changes.

InitializeComponent() must be called once in the constructor ClientMain() and nowhere else!

  • Related