Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Populate ComboBox from Database Query (Oracle Database)

Posted on 2011-09-29
7
Medium Priority
?
1,974 Views
Last Modified: 2013-12-14
I need to populate a combobox from an Oracle database query. I have no idea how to begin and I have been "Google-ing" for the past day and a half. The problem that I have (I see it as a problem anyway) is that I need to populate information from an Oracle database versus a SQL Server database. I have already created a DataSet (DataSet1) to the Oracle table that I need to pull the values from. Beyond adding the dataset, I have no idea how to move forward.

I am using Visual Studio 2010 Pro which has the VSTO libraries already loaded. I have no code to show since I don't even know how to start. I am using the C# language and this is my third day so any code samples should be wrtten in such a way that a novice like me can understand.

Any and all help is appreciated.
0
Comment
Question by:Gary Jones
  • 4
  • 3
7 Comments
 
LVL 4

Expert Comment

by:rar3z
ID: 36818656
            DataTable UR_MyDataTableNew = new DataTable();
            SqlConnection UR_MySqlConnectionNew = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ToString());
            SqlCommand UR_MySqlCommandNew = new SqlCommand();
            UR_MySqlCommandNew.Connection = UR_MySqlConnectionNew;
            try
            {
                UR_MySqlConnectionNew.Open();
                UR_MySqlCommandNew.CommandType = CommandType.Text;
                UR_MySqlCommandNew.CommandText = "SELECT Description as NAME, CODE FROM STATE ORDER BY CODE";
                SqlDataAdapter UR_MySqlDataAdapterNew = new SqlDataAdapter(UR_MySqlCommandNew);
                UR_MySqlDataAdapterNew.Fill(UR_MyDataTableNew);
                ddl_state.DataSource = UR_MyDataTableNew;
                ddl_state.DataTextField = UR_MyDataTableNew.Columns[0].ToString();
                ddl_state.DataValueField = UR_MyDataTableNew.Columns[1].ToString();
                ddl_state.DataBind();
            }
            catch
            {
                Lbl_Error_Message.Text = "There was an error while connecting to the server.";
            }
            finally
            {
                UR_MySqlConnectionNew.Close();
                SqlConnection.ClearPool(UR_MySqlConnectionNew);
                UR_MySqlConnectionNew.Dispose();
            }

Open in new window


I am thinking of use the above code with changing the sqlconnection to oracle type.
0
 
LVL 4

Expert Comment

by:rar3z
ID: 36818744
You can get the connection string information from the following site.

Connection Strings for Oracle
0
 
LVL 1

Author Comment

by:Gary Jones
ID: 36892055
rar3z, thank you for the quick reply. My next question is where do I place the block of code. I am working on an Office Workbook Application project. I have placed the code in the ThisWorkbook.cs area but I get a lot of "red squigly lines" under most of the syntax. I suspect that I am missing some assemblies. I moved the code block inside of the ThisWorkbook_Startup class and now many of the "squigly red lines" are gone but I do still have a few others.

Please keep in mind that I am very much a novice when it comes to c# (I'm a VB 6, VBA programmer usually).
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:Gary Jones
ID: 36892201
Another thing is that I am pulling data from an Oracle database, not a SQL Server database. The code appears to be hooked into the SQL Server assemblies versus the Oracle assemblies.

Thanks again.
0
 
LVL 1

Accepted Solution

by:
Gary Jones earned 0 total points
ID: 36893897
I figured out how to pull data from an Oracle database and place the results in a datatable:

Add the Oracle.DataAccess.Client assembly

code: using Oracle.DataAccess.Client;

Then in the ThisWorkbook_Startup class add:

OracleConnection cs = new OracleConnection("User Id=MyUserID;Password=MyPassword;Data Source=OracleSID");
OracleDataAdapter da = new OracleDataAdapter("SELECT COL1, COL2, FROM SCHEMA.TABLENAME", cs);

DataTable dt = new DataTable();

da.Fill(dt);

I just need to figure out how to get the ComboBox named cmbProject to populate. I tried using:

(the code is in the code snippet)

But I get an error saying that cmbProject does not exist in the current context. The cmbProject combo box is in a custom Ribbon named Ribbon1.

Any ideas what I am missing?
for (int i = 0; i < dt.Rows.Count; i++)
            {
                cmbProject.Items.Add(dt.Rows[i]["My_Column_Name"]);
            }

Open in new window

0
 
LVL 1

Author Closing Comment

by:Gary Jones
ID: 36915544
The database portion of the question was figured out by the author. I still need to figure out how to populate the combobox of a ribbon. I will add another question in a more appropriate zone.
0
 
LVL 4

Expert Comment

by:rar3z
ID: 36903707
Try adding the following after your for loop.

cmbProject.DataBind();

Open in new window


Hope this helps.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question