0

i created Windows Application form my project and there is a problem that i am facing right now. The problem is: 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.

My Question is: How do i convert int datatype to string datatype?

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);
        }
    }
}
5
  • I cant believe for one second you have bothered to google this before posting a question on here... Commented Jul 31, 2013 at 12:45
  • You say that the line ` codesCollection.Add(Convert.ToString(dReader.GetInt32(dReader.GetOrdinal("Code"))));` is not working. What is not working here (assuming the Code field is an integer and not null) Commented Jul 31, 2013 at 12:45
  • @Steve: i mean, not working is the autocomplete for int datatype is not working, that code when i trace gave me autos (so it is means there is no error) right? Commented Jul 31, 2013 at 12:48
  • The AutoCompleteStringCollection requires strings not ints. So I still cannot understand where is your problem. If you start typing a digit in your textbox, the autocomplete works or not? Commented Jul 31, 2013 at 12:56
  • no, the autocomplete is not works when i type a digit in my textbox, but when i change the code to: "OleDbCommand cmd = new OleDbCommand("SELECT DISTINCT [Username] FROM [Member] ORDER BY [Username] ASC", conn); <-- this is the table called Member where inside of it are string type and change this code too to: "CodesCollections.Add(drReader.getString(0)), the autocomplete for string type is working Commented Jul 31, 2013 at 13:47

3 Answers 3

1

My Question is: How do i convert int datatype to string datatype?

string s = Convert.ToString(yourIntvalue);
Sign up to request clarification or add additional context in comments.

13 Comments

i already tried that code, and it is not working, and my value is on database, so how do i fill up that (yourintvalue)
why not yourIntValue.ToString() ?
codesCollection.Add(Convert.ToString(dReader.GetOrdinal("Code")));
@EhsanUllah: no, i already tried the code that you gave me, before i post on this forum, and it is not working either
@FuhansPujiSaputra what does dReader.GetOrdinal("Code") returns you?
|
0

use

 dReader.GetString("Code");

So

while (dReader.Read())
{
    codesCollection.Add(dReader.GetString("Code"));
}

OleDbDataReader.GetString

Gets the value of the specified column as a string.

1 Comment

it gave me several errors, which are: "cannot convert string type to int type", and "the best overloaded method for "dbdatareader.getstring(int) has some invalid arguments"
0

If you are going to use CODE as string in all you code, why not changing it from the query?

"SELECT DISTINCT CStr([Code]) FROM [Data] ORDER BY [Code] ASC"

ACCESS supports some VBscript so CStr would do the trick. Donig this way, the DBReader will treat CODE as string

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.