Hi guys, i created Windows Application form my project and there is a problem that i am facing right now. I already had an array of textboxes, and wanted to connect those database textboxes, however, i cannot get the data because the data that i want to connect is int datatype.

There are 2 Form that i has been created, one is for Login Form, for the Login Form, the database is string, so the autocomplete is working. The second one is Second Form, for the Second Form, the database is int, so the autocomplete is not working.

Please help.

Here is my code for Second Form:

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

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

    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(Convert.ToString(dReader.GetInt32(dReader.GetOrdinal("Code")))); // This line is not working
    }

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

                textBoxCodeContainer[0][y].AutoCompleteMode = AutoCompleteMode.Suggest;
                textBoxCodeContainer[0][y].AutoCompleteSource = AutoCompleteSource.CustomSource;
                textBoxCodeContainer[0][y].AutoCompleteCustomSource = codesCollection;

                dReader.Close();
                conn.Close();

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

edit: **data type for code is number, field size is long integer and the format is 0000.. zero behind replaced with number "1", "2" and so on.
**

edit: ****Complete Code****

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Principal;
using System.Data.OleDb;

namespace Sell_System
{
    public partial class Form2 : Form
    {
        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;");
        private Form1 firstForm;
        private List<List<TextBox>> textBoxCodeContainer = new List<List<TextBox>>();
        private List<List<TextBox>> textBoxQuantityContainer = new List<List<TextBox>>();
        private List<List<TextBox>> textBoxDescContainer = new List<List<TextBox>>();
        private List<List<TextBox>> textBoxSubTotalContainer = new List<List<TextBox>>();
        private List<List<TextBox>> textBoxTotalContainer = new List<List<TextBox>>();

        public Form2()
        {
            InitializeComponent();
        }

        public Form2(Form1 firstForm)
            : this()
        {
            this.firstForm = firstForm;
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            //var int num = 10;

            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(Convert.ToString(dReader.GetInt32(dReader.GetOrdinal("Code"))));
                //codesCollection.Add(Convert.ToString(num);
            }

            if (firstForm.comboBox1.SelectedIndex == 0)
            {
                label1.Text = "Code:";
                label1.Location = new Point(60, 125);
                label2.Text = "Welcome to the Selling System.";
                label2.Location = new Point(600, 0);
                label3.Text = "Quantity:";
                label3.Location = new Point(155, 125);
                label4.Text = "Description:";
                label4.Location = new Point(580, 125);
                label5.Text = "Sub Total on Rp:";
                label5.Location = new Point(1020, 125);
                label6.Text = "Total on Rp:";
                label6.Location = new Point(1210, 125);
                label7.Text = "Total on Rp:";
                label7.Location = new Point(1080, 580);
            }

            else if (firstForm.comboBox1.SelectedIndex == 1)
            {
                label1.Text = "Kode:";
                label1.Location = new Point(60, 125);
                label2.Text = "Selamat datang di Selling System.";
                label2.Location = new Point(600, 0);
                label3.Text = "Banyaknya:";
                label3.Location = new Point(145, 125);
                label4.Text = "Keterangan:";
                label4.Location = new Point(580, 125);
                label5.Text = "Sub Total di Rp:";
                label5.Location = new Point(1020, 125);
                label6.Text = "Total di Rp:";
                label6.Location = new Point(1210, 125);
                label7.Text = "Total di Rp:";
                label7.Location = new Point(1080, 580);
            }

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

                textBoxCodeContainer[0][y].AutoCompleteMode = AutoCompleteMode.Suggest;
                textBoxCodeContainer[0][y].AutoCompleteSource = AutoCompleteSource.CustomSource;
                textBoxCodeContainer[0][y].AutoCompleteCustomSource = codesCollection;

                dReader.Close();
                conn.Close();

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

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

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

            //****TextBox for Description****
            for (int y = 0; y <= 16; y++)
            {
                textBoxDescContainer.Add(new List<TextBox>());
                textBoxDescContainer[0].Add(new TextBox());
                textBoxDescContainer[0][y].Size = new Size(750, 50);
                textBoxDescContainer[0][y].Location = new Point(225, 150 + (y * 25));

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

            //****TextBox for Sub Total****
            for (int y = 0; y <= 16; y++)
            {
                textBoxSubTotalContainer.Add(new List<TextBox>());
                textBoxSubTotalContainer[0].Add(new TextBox());
                textBoxSubTotalContainer[0][y].Size = new Size(175, 50);
                textBoxSubTotalContainer[0][y].Location = new Point(975, 150 + (y * 25));

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

            //****TextBox for Total****
            for (int y = 0; y <= 16; y++)
            {
                textBoxTotalContainer.Add(new List<TextBox>());
                textBoxTotalContainer[0].Add(new TextBox());
                textBoxTotalContainer[0][y].Size = new Size(175, 50);
                textBoxTotalContainer[0][y].Location = new Point(1150, 150 + (y * 25));

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

            //****TextBox for Total All****
            TextBox textBoxAllTotal = new TextBox();
            textBoxAllTotal.Size = new Size(175, 50);
            textBoxAllTotal.Location = new Point(1150, 575);

            Controls.Add(textBoxAllTotal);
        }
    }
}

Recommended Answers

All 8 Replies

Use the ToString() method.
MyTxtBox.Text = MyInt.ToString();
You can even do : MyTxtBox.Text = 42.ToString();//Puts the string "42" into the textbox.

The method that you gave me is when i declare the variable inside the code, and now the problem is the data are inside the database, all of them, and want to connect it. My previous code is working, because the datatype inside the database is string.

That is what I meant, get the ints out of the DB, convert to string, assign to textbox.

i already did that way, but the value keep 0 (or nothing inside it), this is the code that i did:

int numInt = dReader.GetOrdinal("Code");
codesCollection.Add(Convert.ToString(numInt));

could i wrong in some way?

Did yo try thisClick Here?

string someNumberAsString = dReader.GetInt32(dReader.GetOrdinal("Code")).ToString(); is perfectly acceptable.

solved now by myself, the code i did like this:

 string numString = "000" + dReader[0].ToString();
 codesCollection.Add(numString);

Thanks guys for help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.