Click here to Skip to main content
15,886,078 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi guys, i am new in c# and want to ask:

i already had a database that filled up with numbers, and want to show it on my form as string. How do i call the value of that numbers from int to string? When user type "0" which is start of the number, it will display to the screen the numbers that start from "0" based on my database

Here is my code so far:

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

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())
            {
                // I must fill this with what?
            }

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


Thank you
Posted
Comments
Jignesh Khant 6-Aug-13 3:04am    
string v1 = dReader[3].ToString();

here your dReader[3] is that int value comming from db.
Member 10191939 6-Aug-13 3:24am    
i already did like this, but it is only works when i show to message box, not to autostringcompletion:

string numString = dReader[0].ToString();
MessageBox.Show(numString);

it has successfully work, but when i change the "MessageBox.Show" to "codesCollection.Add(dReader.GetString(0)), the windows forms completely blank, but the program runs
Jameel VM 6-Aug-13 3:12am    
what format you filled up the numbers?
Member 10191939 6-Aug-13 3:20am    
my numbers is 1 to 10, but before the number, i put the format 0000, so it will become 0001, 0002 and so on

Hope this are what you expect
C#
string number="";
while (dReader.Read())
{
   number ="000"+ dReader["Code"].ToString();
}

Hope this helps
 
Share this answer
 
v3
This code:
C#
string numString = "000" + dReader["Code"].ToString();

If the value is 2, you will get 0002.
But if the value is 200, you will get 000200, which I think is not what you want.
If the value is 200, the outcome should be 0200.
You can use PadLeft() to solve this:
C#
while (dReader.Read())
{
    string number = dReader["Code"].ToString().PadLeft(4, '0'));
}
 
Share this answer
 
Thanks guys, i already solved this myself

This is the code:
<pre lang="c#">string numString = "000" + dReader["Code"].ToString();
                codesCollection.Add(numString);
 
Share this answer
 
Comments
adriancs 6-Aug-13 11:36am    
See my answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900