Thread: Connect multiple textbox to the database in WinApp

  1. #1
    Registered User
    Join Date
    Jul 2013
    Kuala Lumpur, Malaysia, Malaysia

    Exclamation Connect multiple textbox to the database in WinApp

    Here is my case:
    I have created 2 form which is login form, which username textbox already connected to the database and it is successfully.
    When user login, it direct to the second form, and in the second form, there are multiple textbox that i have created using Array List, and it has shown.

    Here is my problem:
    I want to connect the textboxes in second form to the database, but it shown nothing when i run the program, i already trace the code which is the error, and the error is on when OleDBDataReader is reading, i dunno what's wrong with my code, in login form it run perfectly, but in the second form, there is an error.

    Here is the image for Login Form:
    Connect multiple textbox to the database in WinApp-capture-png

    Here is the image for Second Form:
    Connect multiple textbox to the database in WinApp-capture_2-png

    Here is the code for Second Form:

    private List<List<TextBox>> textBoxCodeContainer = new List<List<TextBox>>();

    string connectionString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\Archives\Projects\Program\Sell System\Sell System\App_Data\db1.accdb;Persist Security Info=False;");

    //****TextBox for Code****
    for (int y = 0; y <= 16; y++)
    textBoxCodeContainer.Add(new List<TextBox>());
    textBoxCodeContainer[0].Add(new TextBox());
    textBoxCodeContainer[0][y].Size = new Size(100, 50);
    textBoxCodeContainer[0][y].Location = new Point(25, 150 + (y * 25));


    OleDbDataReader dReader;
    OleDbConnection conn = new OleDbConnection(connectionString);
    OleDbCommand cmd = new OleDbCommand("SELECT DISTINCT [Code] FROM [Data] ORDER BY
     ASC", conn);
                 dReader = cmd.ExecuteReader();
                 AutoCompleteStringCollection codesCollection = new AutoCompleteStringCollection();
                 while (dReader.Read())
                 textBoxCodeContainer[0][0].AutoCompleteMode = AutoCompleteMode.Suggest;
                 textBoxCodeContainer[0][0].AutoCompleteSource = AutoCompleteSource.CustomSource;
                 textBoxCodeContainer[0][0].AutoCompleteCustomSource = codesCollection;

    For the Second Form, in the column of code, it supposed to shown the autocomplete like at the Login Form.

    Thank you.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    There is a [noparse] tag if you need to use it.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    There are many ways to do this. I recommend you use binding. Create properties on your data model that are populated from your database. When these properties change fire INotifyPropertyChanged. Bind your textbox to these properties. When a change is made in the will change the data model and when a change is made in the data model it will be reflected in the UI. Note for this to work you must specify two way binding. This is much simpler in WPF since binding is natively supported by xaml but you can also use binding in Windows Forms. It works the same way with both WPF and Windows Forms.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to connect a C program to Oracle Database?
    By Cnewbi in forum C Programming
    Replies: 4
    Last Post: 01-06-2012, 09:15 AM
  2. c my project to connect to the database
    By Polat1256 in forum C Programming
    Replies: 4
    Last Post: 05-19-2010, 04:00 PM
  3. Connect to a sql database
    By Boy67 in forum C++ Programming
    Replies: 4
    Last Post: 08-28-2008, 03:33 PM
  4. Database connect
    By column in forum C++ Programming
    Replies: 4
    Last Post: 02-14-2006, 03:40 AM
  5. How to connect with Oracle database
    By Jone in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2002, 06:09 AM