14 Replies - 1643 Views - Last Post: 30 March 2013 - 05:32 PM Rate Topic: -----

#1 arrezes  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 58
  • Joined: 17-October 10

Combobox SelectedIndex does not return correct selection

Posted 25 March 2013 - 11:41 PM

I have tried a lot of ways getting this thing right but to no avail. I think my code is right but I don't know why. Let me get this thing straight

I tabulated my combobox by retrieving data from table and dump them into dictionary. this is my code to get a clear picture

 dsDiscipline = FillDataSet("SELECT * FROM discipline", con, "Discipline");    //FillDataSet is my own method to fill data tables with results from queries
                DataRow[] drDiscipline = dsDiscipline.Tables["Discipline"].Select();

                Dictionary<char, char> comboDiscipline = new Dictionary<char, char>();

                int j = 0;
                foreach (DataRow drDisciplineToCombo in drDiscipline)
                {
                    char[] keyValue = (drDisciplineToCombo[0].ToString()).ToCharArray(0, (drDisciplineToCombo[0].ToString()).Length);
                    comboDiscipline.Add(keyValue[0], keyValue[0]);
                    cmbDiscipline.Items.Add(comboDiscipline.ElementAt(j));
                    j++;
                }

                cmbDiscipline.DisplayMember = "Key";
                cmbDiscipline.ValueMember = "Value";
                cmbDiscipline.DataSource = cmbDiscipline.Items;



this combobox visually looks good to me as it appears as what I wanted. But when to get SelectedIndex based on criteria set by the user I ended up getting wrong value. The result always points to -1. this is the code

string sqlJobcardNoRetrieval = 
                    "SELECT jobcard.id, jobcard.jobcard_no, jobcard.location, jobcard.description, jobcard.workcategory_id, " +
                    "workcategory.description AS WorkCategory, workcategory.shoretype, jobcard.system_id," +
                         "jobcard.discipline_id, jobcard.workphase_id FROM jobcard INNER JOIN " +
                         "workcategory ON jobcard.workcategory_id = workcategory.id WHERE (jobcard.id = " + jobcardID + ")";

                DataSet dsJobcardRetrieval = FillDataSet(sqlJobcardNoRetrieval, con, "JobcardRetrieve");
                DataRow[] drJobcardRetrieval = dsJobcardRetrieval.Tables["jobcardRetrieve"].Select();

                foreach (DataRow drJobcardDisplay in drJobcardRetrieval)
                {
                    cmbWorkCategory.SelectedItem = drJobcardDisplay[4];
                    txtSystem.Text = drJobcardDisplay[7].ToString();
                    char[] selected = (drJobcardDisplay[8].ToString()).ToCharArray();
                    
                    int found = cmbDiscipline.Items.IndexOf(selected[0]);   

                    cmbDiscipline.SelectedIndex= found;
                }



what happened was, whilst debugging I came across that char[] selected consists of not only a single character but the values shown for example like this [88 'S']. Therefore found is always -1

what have I done wrong here? pls help thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Combobox SelectedIndex does not return correct selection

#2 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: Combobox SelectedIndex does not return correct selection

Posted 26 March 2013 - 01:08 AM

Combobox.SelectedIndex value of -1 means nothing has been selected. That's correct based on the fact that you're not finding a valid item first. However, you seem surprised that a char array holds more than one character. Surely if its only one character you just want a char? Since you are setting it to equal the contents of the row value at colum 8, it should depend on what's in that column. Take a look at what's there to see if it matches.
Was This Post Helpful? 1
  • +
  • -

#3 arrezes  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 58
  • Joined: 17-October 10

Re: Combobox SelectedIndex does not return correct selection

Posted 26 March 2013 - 08:13 PM

View PostMrShoes, on 26 March 2013 - 04:08 PM, said:

Combobox.SelectedIndex value of -1 means nothing has been selected. That's correct based on the fact that you're not finding a valid item first. However, you seem surprised that a char array holds more than one character. Surely if its only one character you just want a char? Since you are setting it to equal the contents of the row value at colum 8, it should depend on what's in that column. Take a look at what's there to see if it matches.


that was what i told earlier the value inside the column 8 = [88 's']. this is what make me wondering why it is not a single character 'S'. what is the preceding value 88 for?
Was This Post Helpful? 0
  • +
  • -

#4 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,524
  • Joined: 08-April 09

Re: Combobox SelectedIndex does not return correct selection

Posted 27 March 2013 - 01:06 AM

I think that is the ascii value(every char has ascii value assigned to it), is it something like in attached file?
if it is check this

Attached image(s)

  • Attached Image

Was This Post Helpful? 1
  • +
  • -

#5 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: Combobox SelectedIndex does not return correct selection

Posted 27 March 2013 - 01:14 AM

EDIT: Ignore my previous post here, it was totally wrong.

As pointed out, the array will show a numeric value for each character. The actual character will be the one in single quotes. That means that while your debugger might show 88 's' then when it's passed around it will be the 's' character.

You're checking for the element at index 0 of the selected array. What is that value?

This post has been edited by MrShoes: 27 March 2013 - 06:20 AM

Was This Post Helpful? 0
  • +
  • -

#6 arrezes  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 58
  • Joined: 17-October 10

Re: Combobox SelectedIndex does not return correct selection

Posted 27 March 2013 - 08:47 PM

View PostMrShoes, on 27 March 2013 - 04:14 PM, said:

EDIT: Ignore my previous post here, it was totally wrong.

As pointed out, the array will show a numeric value for each character. The actual character will be the one in single quotes. That means that while your debugger might show 88 's' then when it's passed around it will be the 's' character.

You're checking for the element at index 0 of the selected array. What is that value?


selected array contains this at [0] Attached Image
Was This Post Helpful? 0
  • +
  • -

#7 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: Combobox SelectedIndex does not return correct selection

Posted 28 March 2013 - 01:10 AM

Right, and I'm assuming your combo box cmbDiscipline has an item with the value 'S' (upper case)? If so, what you're doing should work. You could also try checking if that item is in the collection using cmbDiscipline.Items.Contains(selected[0]).

As a side note, you know that you're adding the same item as both Key and Value of your combobox... Is that by design?
Was This Post Helpful? 0
  • +
  • -

#8 arrezes  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 58
  • Joined: 17-October 10

Re: Combobox SelectedIndex does not return correct selection

Posted 28 March 2013 - 01:26 AM

yes, i want it that way. and the table design is having the char as primary key either.

do you have any other efficient alternative ways to do this trick? or perhaps changing the primary key to having incremental number rather than char.

BTW, I will try your suggestion and look whether it works.
Was This Post Helpful? 0
  • +
  • -

#9 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: Combobox SelectedIndex does not return correct selection

Posted 28 March 2013 - 01:38 AM

In principle, the way you're doing it is similar to how I would. I would save overhead on converting from string to char array if only getting the first character - you can use <il>char firstCharacter = myString[0];</il>

To be honest, unless it's very basic, I use Entity Framework for Data Access Layers these days. Turns database tables into .NET objects for you very easily and saves a lot of work.

What I never do is concatenate strings into SQL statements. It's bad practive and can leave your software open to SQL Injection. I'd replace this:
string sqlJobcardNoRetrieval = 
                    "SELECT jobcard.id, jobcard.jobcard_no, jobcard.location, jobcard.description, jobcard.workcategory_id, " +
                    "workcategory.description AS WorkCategory, workcategory.shoretype, jobcard.system_id," +
                         "jobcard.discipline_id, jobcard.workphase_id FROM jobcard INNER JOIN " +
                         "workcategory ON jobcard.workcategory_id = workcategory.id WHERE (jobcard.id = " + jobcardID + ")";



With this:

string sqlJobcardNoRetrieval = 
                    "SELECT jobcard.id, jobcard.jobcard_no, jobcard.location, jobcard.description, jobcard.workcategory_id, " +
                    "workcategory.description AS WorkCategory, workcategory.shoretype, jobcard.system_id," +
                         "jobcard.discipline_id, jobcard.workphase_id FROM jobcard INNER JOIN " +
                         "workcategory ON jobcard.workcategory_id = workcategory.id WHERE (jobcard.id = @jobCardId)";

SqlCommand command = new SqlCommand(sqlJobCardNoRetrieval, connection);
command.Parameters.AddWithValue("@jobCardId", jobCardId);


This post has been edited by MrShoes: 28 March 2013 - 01:39 AM

Was This Post Helpful? 1
  • +
  • -

#10 arrezes  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 58
  • Joined: 17-October 10

Re: Combobox SelectedIndex does not return correct selection

Posted 28 March 2013 - 01:59 AM

I finally nailed this. let me go thru the chronology of my success... :sweatdrop:/>

I put the messagebox to find out what is inside the selected[0] and the result is really char S.

Therefore I adjusted the code here using combobox.findstring and viola...

char[] selected = (drJobcardDisplay[8].ToString()).ToArray();
                    
MessageBox.Show("value inside selected[0] is " + selected[0]);

int found = cmbDiscipline.FindString(selected[0].ToString());

cmbDiscipline.SelectedIndex= found;



thanks for all your help..truly enlightening
Was This Post Helpful? 0
  • +
  • -

#11 arrezes  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 58
  • Joined: 17-October 10

Re: Combobox SelectedIndex does not return correct selection

Posted 28 March 2013 - 02:05 AM

I really do not look at the security side. I will implement this and revise my code. Besides, my program will only serve staff in my office. I don't see any people here that has potential threat to crack my system. :bigsmile:

This post has been edited by arrezes: 28 March 2013 - 02:06 AM

Was This Post Helpful? 0
  • +
  • -

#12 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: Combobox SelectedIndex does not return correct selection

Posted 28 March 2013 - 02:49 AM

Glad you managed to get it fixed. Happy to be of help.
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,117
  • Joined: 05-May 12

Re: Combobox SelectedIndex does not return correct selection

Posted 28 March 2013 - 05:18 AM

View Postarrezes, on 28 March 2013 - 05:05 AM, said:

I really do not look at the security side. I will implement this and revise my code. Besides, my program will only serve staff in my office. I don't see any people here that has potential threat to crack my system. :bigsmile:/>


Until that code gets copied and pasted elsewhere that maybe outfacing and open to attack. At that point, whoever is the manager of the person who did the copy and paste will tell them "if the code currently works, don't change it", assuming that the manager is like most managers who have "manage risk" as one of their job functions. The only time the manager will say "go change the code" will be if a security audit finds the problem, or if somebody manages to hack the system.

I love this cartoon:
Posted Image

I love it for two reasons:
1. It reminds me to sanitize my database inputs.
2. It makes me consider that a big organization will likely not re-write mission critical code at the moment the problem is found when it works 95% of the time. Will that school be scrambling to restore the lost data, or scrambling to fix their code, run it through full regression testing, deploy it, and then restore the lost data?
Was This Post Helpful? 0
  • +
  • -

#14 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: Combobox SelectedIndex does not return correct selection

Posted 28 March 2013 - 06:05 AM

I'll also throw my hat in the ring on the subject. Never use string concatenation for queries when you're involving user input. Security aside, it's just plain hard to read and easy to forget a single-quote, or to include one when you don't need it. Parameters actually make code simpler, not more complicated. And they're just all around better practice. Get used to doing things right the first time, and you won't have to go back and make changes, if you ever get a chance to do so.
Was This Post Helpful? 0
  • +
  • -

#15 arrezes  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 58
  • Joined: 17-October 10

Re: Combobox SelectedIndex does not return correct selection

Posted 30 March 2013 - 05:32 PM

i'll take note of that. thanks for reminding me. sometimes basic principle seems to be ignored in order to achieve a specific result
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1