Click here to Skip to main content
15,897,226 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi as the questions states id like to know if its possible to show data fom a sql table in a list box? and if you Link a List Box with Database and show values in a textbox if selected in ListBox?
Posted
Updated 2-Aug-15 22:29pm
v2

 
Share this answer
 
Comments
Maciej Los 3-Aug-15 5:23am    
5ed!
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DataSet Ds = new DataSet();
          

            string strConnectionString = "Data Source=localhost,3601;Network   
            Library=DBMSSOCN;Initial Catalog=empdb;User ID=myusername;Password=mypwd;";
            SqlConnection objconnection = new SqlConnection(strConnectionString);
            using (SqlCommand cmd = new SqlCommand("SELECT  [empcode] FROM [empdb]", 
            objconnection))
            {
                using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                {
                    adapter.Fill(Ds);
                }
            }

            var empList = Ds.Tables[0].AsEnumerable().Select(dataRow =>      
            dataRow.Field<int>("empcode")).ToList();
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox1.DataSource = empList;

          
            comboBox1.SelectedIndex = 0;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox1.Text = comboBox1.SelectedItem.ToString();
        }

      
           
    }
}
 
Share this answer
 
Comments
jamesmc1535 3-Aug-15 5:42am    
thanx but i have 2 follow up questions
!1!- Library = DBMSSOCN; Initial Catalog = staff; User ID = myusername; Password = mypwd; "; - i get a hell lot of errors there
!2!-empcode what do i replace it with coz empdb i figured i replace with my database name
sreeyush sudhakaran 4-Aug-15 2:11am    
Initial Catalog is your database name , User ID is Database UserName and Password is your database password ,Field<type>(fieldname of table) syntax of empcode is the field name of table you want to display in dropdownlist followed by field type in database. and write connectionstring in a line see how to write connection string in .net from https://www.connectionstrings.com/sql-server/
jamesmc1535 4-Aug-15 2:42am    
thanks alot
jamesmc1535 3-Aug-15 5:48am    
the name library does not exist in the current context, using System.Linq; < i dont have that and when i type it it doesnt work , shud i add it as a refrence?
sreeyush sudhakaran 4-Aug-15 2:12am    
Add reference of linq , Linq library will work in .NET 3.5 and upper

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