0

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!

  • The sample you show doesn't match the query you show. In the query you are filtering on the `PACIJENT_JMBG` and `DIJAGNOZAID` columns, but you're not showing us the values of those columns. It seems reasonable that those columns *match* the search parameters you're using. – David Sep 16 '17 at 14:00
  • Not related to your question, but I believe you should see https://stackoverflow.com/q/332365/11683. – GSerg Sep 16 '17 at 14:02
  • Yes but I set this option combobox.**SelectedValue**. I believed it was the way for app to recognize the Value Member over the Display Member shown in the combobox. The value memeber for the first one is PK from another table, which PACIJENT_JMBG is referencing as a FK.The same story is with another combo, DIJAGNOZAID is FK referencing PK from another table, and that PK is Value Member for the second combobox. – Благоје Вуковитцх Sep 16 '17 at 16:16
  • I edited the sql querry, I apologize, the first one was from another form. So Instead first mentioned PACIJENT_JMBG its RAD_JMBG like I just edited – Благоје Вуковитцх Sep 16 '17 at 16:27

1 Answers1

0

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.

Abdullah Dibas
  • 1,499
  • 1
  • 9
  • 13
  • I apologize didn't took the screenshot of the other combobox, but it also contains a values, thats why in my sql querry I put OR, so the search operation could be done either from first OR second combobox. But yes it also contains values, and that are values form coloumn IME_ODELJENJA in the table, you can see it in those screenshots. – Благоје Вуковитцх Sep 16 '17 at 15:54