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:
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:
So that would be it, I appreciate any reply!
I think the problem is caused by the
WHERE
statement of yourSELECT
query: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 yourWHERE
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.