Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have tried posting this question on MSDN forums, however, i had no success.

I am trying to make a C# Windows Forms Application for a business that will manage its employees. The application uses an Access 2010 Database. The edit Employee page is what I am having trouble with. I want the Employees to be able to be selected from a combobox in the format of:

FirstName LastName, EmployeePosition

The database name is EmployeeInformation.accdb
The fields that I want from the Database are:

- FirstName
- LastName
- EmployeePosition

The name of the Combo Box is cboSelectEmp


The current code i am using to try and get this data is

C#
private void LoadDataToCbo()
        {
            string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=EmployeeInformation.accdb";
           // string query = @"SELECT FirstName from Employees";
            string query =
                @"SELECT EmpID, LastName + ', ' + FirstName + ' (' + EmployeePosition + ')' as Name FROM Employees";
            OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
            DataTable source = new DataTable();
            dAdapter.Fill(source);
            cboSelectEmp.DataSource = source;
            //cboSelectEmp.ValueMember = "FirstName";
            //cboSelectEmp.DisplayMember = "FirstName";

            cboSelectEmp.DisplayMember = "Name";
            cboSelectEmp.ValueMember = "EmpID";

        }
Posted
Comments
Dave Kreskowiak 11-Dec-13 18:34pm    
So what's the problem?
[no name] 11-Dec-13 18:35pm    
it doesn't work
it throws exceptions all over the place.
sorry i forgot to mention this in the question
Dave Kreskowiak 11-Dec-13 18:36pm    
You also forgot to mention the exception messages!!!
[no name] 11-Dec-13 18:37pm    
i just posted it
[no name] 11-Dec-13 18:37pm    
it throws an exception on dAdapter.Fill(Source);

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: No value given for one or more required parameters.

sorry i forgot to mention this in the question.

I fixed it using this code:
C#
private void LoadDataToCbo()
       {
           string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=EmployeeInformation.accdb";
           string query =
                          @"SELECT (FirstName + ' ' + LastName + ', ' + EmployeePosition) as FirstName from Employees";
           OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
           DataTable source = new DataTable();
           dAdapter.Fill(source);
           cboSelectEmp.DataSource = source;
           cboSelectEmp.DisplayMember = "FirstName";


       }


it looks like it was the ValueMember property was the thing that was messing with me.

Thanks everyone for all your help
 
Share this answer
 
Try This:

C#
string query =
               @"SELECT EmpID, FirstName as [Name], LastName , EmployeePosition   FROM Employees";
 
Share this answer
 
v2
Comments
[no name] 11-Dec-13 19:39pm    
That gives an exception

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: No value given for one or more required parameters.
Sudhakar Tillapudi 11-Dec-13 19:44pm    
i think name is reserved word in access try to encose it in square brackets [], see my edited answer.
[no name] 11-Dec-13 19:51pm    
Using that query still gives the exception
Check this line in documentation under remarks This overload of the OleDbDataAdapter constructor uses the selectConnectionString parameter to set the SelectCommand property. However, it does not open the connection. You still must explicitly open the connection.

http://msdn.microsoft.com/en-us/library/2f8y4737(v=vs.110).aspx

I think that is your problem


Try this
C#
string query =
               @"SELECT EmpID, FirstName as [Name], LastName , EmployeePosition   FROM Employees";
using (var odb = new OleDbConnection(connectionString))
                       {
                           odb.Open();
                           if (odb.State == ConnectionState.Open)
                           {
                               var dset = new DataTable();
                               var adapter = new OleDbDataAdapter();
                               var newCmd = new OleDbCommand(query, odb)
                                   {
                                       CommandType =  CommandType.Text
                                   };
                               adapter.SelectCommand = newCmd;
                               adapter.Fill(dset);
                               odb.Close();
                             
                           }
}
 
Share this answer
 
v2
Comments
[no name] 11-Dec-13 22:03pm    
Again this throws an exception on adapter.Fill(dset);
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: No value given for one or more required parameters.
Try query string in solution2 by Sudhakar Tillapudi
 
Share this answer
 
hey nattyman its easy task

write your code in catch block and then debug now do necessary changes it will solve
 
Share this 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