简体   繁体   中英

C# Populating Drop Down with SQL Server data

I am trying to populate a dropdown based on a column in a SQL Server table. But when I try different get commands, it only shows blank in the dropdown.

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 System.Data.SqlClient;
using System.Web.UI.WebControls;

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

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();

            con.ConnectionString = @"Data Source= localhost; Initial Catalog= Travel;Integrated Security=True;";

            con.Open();

            SqlDataReader dr;

            SqlCommand sqlCmd = new SqlCommand("SELECT Sites FROM dbo.Sites", con);
            sqlCmd.CommandType = CommandType.Text;

            dr = sqlCmd.ExecuteReader();

            while (dr.Read())
            {
                comboBox1.Items.Add(dr.GetData(Type));
            }
        }
    }
}

I have looked online at a couple of different methods to get the drop-down to populate but each one triggers a different error. I'm trying to use the least complicated code so that I can understand what is happening in the process. I get an error

'Type' is a type, which is not valid in the given context

I have searched for this error, but my Google-Fu has failed me. Any and all help greatly appreciated.

Change your code to explicitly read the Sites data column:

while (dr.Read())
{
   comboBox1.Items.Add(dr["Sites"]);
} 

On a side note:

Typically, for larger applications or ones that will need to be maintained into the foreseeable future, you'd want to separate all the DAL (DataAccessLayer) code into a dedicated Manager or Repository. Simply create a new project dedicated to accessing the DB, all the methods should return are single instances of domain classes or lists of those. Keeping it separate and not having any clue of the Presentation will make your DAL reusable. In addition to that, making your Presentation code SQL free will make things more consistent. Then add a project reference to this DAL assembly from your presentation assembly.

Thanks again Steve, and you as well T McKeown. I will work on the other suggestions in the comments. Here is my revised code so that got the drop-down to work.

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 System.Data.SqlClient;
using System.Web.UI.WebControls;

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            SqlConnection con = new SqlConnection();

            con.ConnectionString = @"Data Source= localhost; Initial Catalog= Travels;Integrated Security=True;";

            con.Open();

            SqlDataReader dr;

            SqlCommand sqlCmd = new SqlCommand("SELECT Sites FROM dbo.Sites", con);
            sqlCmd.CommandType = CommandType.Text;
            dr = sqlCmd.ExecuteReader();
            while (dr.Read())
            {
                comboBox1.Items.Add(dr["Sites"]);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {


        }
    }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM