PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, November 21, 2022

[FIXED] How to change default value of combo box

 November 21, 2022     c#, combobox, sql, visual-studio     No comments   

Issue

I am trying to make SQL statement based on item selected throw combo box. I want to have default item selected as ID, but now it returns NULL. What am I doing wrong?

private void Win_Shown(object sender, EventArgs e)
{
   myBox.SelectedValue = "ID";
   myBox.SelectedText = "ID";
   myBox.SelectedItem = "ID";

   myBox.Items.Add("ID");
   myBox.Items.Add("Name");
   myBox.Items.Add("Surname");
   myBox.Items.Add("Mobile"); 
}

Then in for SQL statement

MySQL.DisplayAndSearch("SELECT * FROM Data WHERE " + this.myBox.SelectedItem.ToString() + " LIKE '%" + txt_Search.Text + "%'", dataGridView1);

Thank for any help :)


Solution

You are selecting the item before inserting:

private void Win_Shown(object sender, EventArgs e)
{
   // first, add the items   
   myBox.Items.Add("ID");
   myBox.Items.Add("Name");
   myBox.Items.Add("Surname");
   myBox.Items.Add("Mobile"); 

   // now select it
   myBox.SelectedItem = "ID";
}


Answered By - D.Kastier
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing