How to preform search operation from combobox using data/value member in Visual Studio 2012 C#

1k views Asked by At

I am at beginner level at this kind of programming so I believe that someone of you will know how to resolve this problem I'm having.

I have a small project in Visual Studio that is connected with Oracle Database 11g Express, and I want to perform a search operation on particular table from database and display it at this form in my Visual Studio project. So I have two comboboxes filled with Displayed Members over Data Members. Data members are usually foreign keys in tables, such as ID etc., and display members that are showing in these comboboxes are Name and Surname usually, of course connected with those IDs I mention before.
So I want to select these Display Members and over them to search all the data from particular at.
This is a query for select button in my form:

OracleConnection con = new OracleConnection("DATA SOURCE=localhost:1521/XE;PERSIST SECURITY INFO=True;USER ID=BAYE;PASSWORD=blagojica");
con.Open();

OracleDataAdapter dr = new OracleDataAdapter("SELECT *  FROM zaposlenja where RAD_JMBG='" + comboBox1.SelectedValue + "' or ODELJENJEID='"+comboBox2.SelectedValue+"'", con);
DataTable dt = new DataTable();
dr.Fill(dt);
dataGridView2.DataSource = dt;
con.Close();
this.dataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;`

And I have this table, where ime_pacijenta is name, prezime_pacijenta is surname, and PRETRAGA is this search button:

image

And when I select one item from combobox it should write out just that one record. Instead it shows me another record that has nothing to do with this one. It also happens when I select any other value from combobox:

image

So that would be it, I appreciate any reply!

1

There are 1 answers

1
Abdullah Dibas On

I think the problem is caused by the WHERE statement of your SELECT query:

OracleDataAdapter dr = new OracleDataAdapter("SELECT PACIJENT_JMBG, IME_PACIJENTA, PREZIME_PACIJENTA, NAZIV, OPIS,  NAZIVDIJAGNOZE, DIJAGNOZAID FROM TERAPIJE WHERE PACIJENT_JMBG='"+comboBox1.SelectedValue+"' or DIJAGNOZAID='"+comboBox2.SelectedValue+"'", con);

You should not include in your WHERE statement any combox that doesn't have a value. In the image you are showing; the second combobox doesn't have a value but it's still considered in the query, which in turn will affect the result. You should rewrite your WHERE statement part if at least one of the comboboxes doesn't have a value or you can just make your comboboxes have values by default.

NOTE: You should be sure whether you want to use ORing or ANDing in the WHERE statement, since using ORing may return you multiple records.