Thread: Connect multiple textbox to the database in WinApp

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

    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:
    [CODE]

    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));

    Controls.Add(textBoxCodeContainer[0][y]);
    }

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

    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
    Location
    United States
    Posts
    9,613
    There is a [noparse] tag if you need to use it.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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 UI...it 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