none
combo box to sql server RRS feed

  • Question

  • which part should i be placing the code to connect? Is 2019 syntax different? It doesn't seem to work how i currently have it. Thanks

    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;

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

            private void Form1_Load(object sender, EventArgs e)
            {
                Dim connection As New SqlConnection("Data Source = DE - IDENTIFIEDDB; Initial Catalog = DE_IDENTIFIED; Integrated Security = True")

                Dim command As New SqlCommand("select distinct provider_name from de_identified_claims", connection)

                Dim adapter As New SqlDataAdapter(command)

                Dim table As New DataTable()

                adapter.Fill(table)

                ComboBox1.DataSource = table

                ComboBox1.DisplayMember = "provider_name"
                ComboBox1.ValueMember = "provider_name"
            }
        }
    }


    Monday, March 23, 2020 1:59 PM

All replies

  • I think that you should place the body of Form_Load into a new project made in Visual Basic, because your form is in C#. Such combination of languages is not allowed. Or convert the code to C#.

    Also make sure that the connection string is correct. (Maybe spaces are not needed in “DE – IDENTIFIEDDB”).

    Monday, March 23, 2020 5:15 PM
  • Hi sm85,

    Thank you for posting here.

    If this project is a C# project, the code should look like this.

            private void Form1_Load(object sender, EventArgs e)
            {
                string connString = @"";
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    using (SqlCommand command = new SqlCommand("select distinct provider_name from de_identified_claims", conn))
                    {
                        SqlDataAdapter adapter = new SqlDataAdapter(command);
                        DataTable dataTable = new DataTable();
                        adapter.Fill(dataTable);
                        comboBox1.DataSource = dataTable; 
                        comboBox1.DisplayMember = "provider_name";
                        comboBox1.ValueMember = "provider_name";
                    }
                }
            }

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 24, 2020 6:21 AM
  • Hi,

    Has your issue been resolved?

    If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, March 31, 2020 8:51 AM
  • Hello,

    Although loading data in the form can work it's better to perform the backend data operations in a separate class. In the following example the connection is made using an NuGet package in my signature.

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using BaseConnectionLibrary.ConnectionClasses;
    
    namespace ComboBoxToComboBoxSqlServer.Classes
    {
        public class DataOperations : SqlServerConnection
        {
            /// <summary>
            /// Create database connection string. If your server name
            /// is not .\SQLEXPRESS then the first line of code
            /// below needs to change to your SQL-Server name.
            ///
            /// This also needs to be done in the class NorthWindEntityCore
            /// in OnConfiguring method of the class NorthWindContext
            /// </summary>
            public DataOperations()
            {
                DatabaseServer = ".\\SQLEXPRESS";
    
                if (Environment.UserName.ToLower().Contains("karen"))
                {
                    DatabaseServer = "KARENS-PC";
                }
    
                DefaultCatalog = "NorthWindAzure";
            }
    
            public DataTable CategoryDataTable()
            {
                var dt = new DataTable();
    
                using (var cn = new SqlConnection(ConnectionString))
                {
                    using (var cmd = new SqlCommand() {Connection = cn})
                    {
                        var selectStatement = 
                            "SELECT CategoryId, CategoryName " + 
                            "FROM Categories ORDER BY CategoryName";
    
                        cmd.CommandText = selectStatement;
                        cn.Open();
                        dt.Load(cmd.ExecuteReader());
                    }
                }
    
                return dt;
            }
        }
    }
    
    

    Form load code to set the DisplayMember of the ComboBox and set the DataSource of the ComboBox.

    namespace ComboBoxToComboBoxSqlServer
    {
        public partial class Form1 : Form
        {
            readonly BindingSource _productBindingSource = new BindingSource();
            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                var ops = new DataOperations();
                CategoryComboBox.DisplayMember = "CategoryName";
                CategoryComboBox.DataSource = ops.CategoryDataTable();
            }
        }
    }
    
    

    There is no reason really to set ValueMember, instead to access the current data for the Selecteditem cast it e.g.

    var categoryIdentifier = ((DataRowView)CategoryComboBox.SelectedItem)
        .Row.Field<int>("CategoryId");

    All of the above is from a larger example, source code and database scripts included..

    https://github.com/karenpayneoregon/filter-combobox-comparision-cs


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, March 31, 2020 10:44 AM
    Moderator