Click here to Skip to main content
15,354,954 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add my one of database's column in string[] but fail to do so. I have created string[] with name "Items" and also create a control with name autocompletemenu1. now when I type autocompetemenu1.Items = Database Column then the contents of database column should filled in ITEM string[]. I have tried but it is not working so I just mention some name in it and it is working but it is not taking database column. I don't know why.

This is the method ITEM I am using to take collections. way to write is autocompleteMenu1.Items = Ms Access Database column

What I have tried:

C#
[DefaultValue(null)]
public string [] Items
{
    get
    {
         if (sourceItems == null)
            return null;
        var list = new List<string>();
        foreach (AutocompleteItem item in sourceItems)
            list.Add(item.ToString());
        return list.ToArray();
    }
    set { SetAutocompleteItems(value); }

}
void AutocompleteText2()
{
    autocompleteMenu1.AutcompleteMOD = AutoCompleteMode.Suggest;
    autocompleteMenu1.AutocompleteSRC = AutoCompleteSource.CustomSource;
    AutoCompleteStringCollection coll2 = new AutoCompleteStringCollection();
    this.autocompleteMenu1.Font = new Font("MB SINDHI WEB", 11);
    string connString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;

    using (OleDbConnection con = new OleDbConnection(connString))
    {
        using (OleDbCommand cmd = new OleDbCommand("SELECT  * from userdic", con))
        {
            con.Open();
            OleDbDataReader dr = cmd.ExecuteReader();  //error this line!

            while (dr.Read())
            {

              coll2.Add(dr["Sindhiwrd2"].ToString());        // Sindhiwrd2 coloumns data load into the coll variable
            }

        }

        con.Close();
    }
          autocompleteMenu1.Items = coll2; ///Here error is generated that can not convert String[] to
                                           /// AutoCompleteStringCollection....

}

Here is the string [] that i want to connect with database coloumn named "Sindhiwrd2"
C#
this.autocompleteMenu1.Items = new string[] {
  "Jamal",
  "Javed",
  "Jameel",
  "James",
  "Jamshed",
  "abc",
  "abcd",
  "Ahmed"};
Posted
Updated 1-Dec-16 1:32am
v2
Comments
dbrenth 30-Nov-16 16:19pm
   
You did not include your error messages.
Matt T Heffron 30-Nov-16 17:29pm
   
First, figure out why the cmd.ExecuteReader() is failing. The rest of your difficulty doesn't matter until that works.
Matt T Heffron 30-Nov-16 17:50pm
   
Also, why is Items string[] when the values are stored in sourceItems which appears to be a collection of AutoCompleteItem (which is not a .NET class, so we have no idea what it is!)
What does SetAutoCompleteItems(...) do? (Show the code)

Use "Improve question" above to edit. (Don't put code in the "Comments". It's very difficult to read there.)

1 solution

Put a try/catch block in your code, and that will give you the opportunity to determine where your problem is:
C#
OldDbConnection conn = null;
OleDbCommand cmd = null;
OleDbDataReader reader = null;
string query = "SELECT * FROM userdic";
try
{
    using (conn = new OleDbConnection(connectionstring))
    {
        conn.Open(); // you don't need to call Close() in a using block

        using (cmd = OleDbCommand(query, conn))
        {
            // I know this is the default value, but you can never be too safe
            cmd.CommandType = CommandType.Text;

            reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                // ... do something ...
            }
        }
    }
} // put a breakpoint here in case an exception is NOT thrown.
catch (Exception ex)
{
} // <<< put your breakpoint here

At that point, you can inspect your connection string, the connection object, the command object, and the reader object when the breakpoint is hit.

FWIW, it might be useful to come up with a static class that contains all of your OleDB... calls. I have one each for Sql and Access, and if I ever develop a need for OleDB, it's an easy matter to write one. The benefit of this is that my outward-facing database access is always the same, queries always return a DataSet object, and all I really have to do is use the class that's appropriate for the type of database I'm accessing.
   
v5
Comments
ridoy 1-Dec-16 14:25pm
   
a 5.

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