none
How to create autocomplete combobox with sql database RRS feed

  • Question

  • I am creating a autocomplete combobox from sql database.My Database name is dbusers. My table name is addnewitem.I want to show the code field of that table in the combobox.What should i do?

    Please tell me according to my tablename and database.

    Monday, January 1, 2018 6:40 AM

All replies

  • any answer

    Monday, January 1, 2018 7:27 AM
  • The following is done via OleDb data provider with ms-access as the database. To have this work with SQL-Server, change the data provider to SqlClient, change the connection string from ms-access to SQL-Server.

    https://code.msdn.microsoft.com/Populate-save-remove-items-be87b5c7?redir=0

    The last screenshot shows this working for a ComboBox and a TextBox.


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, January 1, 2018 10:57 AM
    Moderator
  • Hello Sahi1313,

    You just need to set the combobox properties as below.

    And your code should be like as below.

     string connstr = "Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename =\"D:dbusers.mdf\"; Integrated Security = True";
                SqlConnection conn = new SqlConnection(connstr);
    
                string[] restrictions = new string[4] { null, null, "addnewitem", null };
                conn.Open();
                var columnList = conn.GetSchema("Columns", restrictions).AsEnumerable().Select(s => s.Field<String>("Column_Name")).ToList();
                comboBox1.DataSource = columnList; 

    Result

    Best regards,

    Neil Hu


    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, January 2, 2018 8:48 AM
    Moderator
  • Hello Sahil1313,

    Is there any update or any other assistance I could provide? You could mark the helpful reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know.

    Thank you for your understanding and patience!

    Best regards,

    Neil Hu


    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.

    Sunday, January 28, 2018 12:19 PM
    Moderator