Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
private void button4_Click(object sender, EventArgs e)
        {
            
            SqlConnection conn = new SqlConnection(this.connetionString);
            string sql = @"SELECT * from amd_err;";
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();         
            sda.Fill(dt);
                      
            this.listBox1.DataSource = result;
            this.listBox1.DataSource = dt;       
            listBox1.DisplayMember = "amd_err";

            //DataSet tblData = new DataSet();
            //string sql = @"SELECT * from amd_err;";
            //SqlConnection conn = new SqlConnection(this.connetionString);
            //SqlDataAdapter sda = new SqlDataAdapter(sql, conn);           
            //sda.Fill(tblData, "amd_err");
            //MessageBox.Show("Rows Count:   !" );           
            //bindingSource1.DataSource = tblData;          
            //dataGridView1.ReadOnly = true;

            //this.dataGridView1.DataSource = bindingSource1;
            //dataGridView1.DataMember = "amd_err";
            //this.dataGridView1.Refresh();


What I have tried:

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

namespace connectDatabase
{
    public partial class Form1 : Form
    {
        private string DBUID;
        private string DBPWD;
        private string DBSVR;
        private string DBNAME;
        private string connetionString;
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void bindingSource1_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            this.DBSVR = DBSname.Text;
            this.DBPWD = DBPass.Text;
            this.DBUID = DBUname.Text;
            this.DBNAME = DBname.Text;
            SqlConnection cnn;
            connetionString = @"Data Source=" + this.DBSVR + ";Initial Catalog=" + this.DBNAME + ";User ID=" +this.DBUID+ ";Password="+ this.DBPWD+ ";";
            cnn = new SqlConnection(connetionString);

            try
            {
                cnn.Open();
            }catch (Exception ex) {
                
                MessageBox.Show("Connection Can't connect  !" + ex.Message);
                return;
            }
            MessageBox.Show("Connection Open  !");
            cnn.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            
            SqlConnection conn = new SqlConnection(this.connetionString);
            string sql = @"SELECT * from amd_err;";
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();         
            sda.Fill(dt);
                      
            this.listBox1.DataSource = result;
            this.listBox1.DataSource = dt;       
            listBox1.DisplayMember = "amd_err";

            //DataSet tblData = new DataSet();
            //string sql = @"SELECT * from amd_err;";
            //SqlConnection conn = new SqlConnection(this.connetionString);
            //SqlDataAdapter sda = new SqlDataAdapter(sql, conn);           
            //sda.Fill(tblData, "amd_err");
            //MessageBox.Show("Rows Count:   !" );           
            //bindingSource1.DataSource = tblData;          
            //dataGridView1.ReadOnly = true;

            //this.dataGridView1.DataSource = bindingSource1;
            //dataGridView1.DataMember = "amd_err";
            //this.dataGridView1.Refresh();
        }
    }
}
Posted
Updated 2-Jul-19 23:43pm
Comments
Sinisa Hajnal 3-Jul-19 2:18am    
What is the problem with the code? What error do you get? Where?
[no name] 3-Jul-19 3:27am    
One of those is probably redundant.

Probably, it's your DisplayMember setting that is wrong - that should be a column name, not the table name:
            string sql = @"SELECT * from amd_err;";
...
            listBox1.DisplayMember = "amd_err";
 
Share this answer
 
C#
//We can make seperated function to load data
private DataTable GetResult(string sqlStatment)
{
  SqlConnection conn = new SqlConnection(this.connetionString);
  SqlDataAdapter sda = new SqlDataAdapter(sqlStatment, conn);
  DataTable dt = new DataTable();         
  sda.Fill(dt);
  return dt
}


C#
this.listBox1.DataSource =this.GetResult("SELECT * from amd_err");
this.listBox1.ValueMember  = "ERROR_ID" //e.g. Column ID NAME;
this.listBox1.DisplayMember= "ERROR_DESC" //e.g. Colum description what user will see;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900