6 Replies - 6170 Views - Last Post: 12 February 2012 - 06:39 AM Rate Topic: -----

#1 jayroll123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-February 12

How can i populate combobox from database's items?

Posted 11 February 2012 - 08:42 AM

Populate combobox from database item. .I need help. this is my code..
I have a problem with my select statement. i want to populate my textbox with selected items only.
for example "where roomType = Single";
thanks!

this one doesn't work... :helpsmilie:
MySqlDataAdapter da = new MySqlDataAdapter("select roomNo from room where roomType = '"+cboRoomType.Text+"'",connection );


void Fillcombo()
        {
            connection.Open();
            MySqlDataAdapter da = new MySqlDataAdapter("select roomNo from room where roomType = 
	    '"+cboRoomType.Text+"'",connection );
            DataTable dt = new DataTable();
            da.Fill(dt);     
            cboRoomNumber.DataSource = dt;
            cboRoomNumber.DisplayMember = "roomNo";
            cboRoomNumber.SelectedIndex = -1;
        }


Is This A Good Question/Topic? 0
  • +

Replies To: How can i populate combobox from database's items?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: How can i populate combobox from database's items?

Posted 11 February 2012 - 08:47 AM

"It doesn't work" isn't good enough - Read this article

Have you done some basic debugging to confirm things like cboRommType.Text even contains text? That it is case-correct with the data you are searching? That your connection is working?

"Doesn't work" is just so very vague. Exception errors? Wrong data being found. no data being found?

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute. Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Form1 talk to Form2



FAQ (Frequently Asked Questions - Updated Jan 2012

Spoiler

Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: How can i populate combobox from database's items?

Posted 11 February 2012 - 09:11 AM

"select roomNo from room" = works

"select roomNo from room where roomType = '"+cboRoomType.Text+"'" = broken

Maybe you should try a hard-coded test in bewteen before introducing the control.

"select roomNo from room where roomType = 'kitchen'"

Once you can make that much work, THEN worry about adding the additional complication of the combobox value.
Was This Post Helpful? 1
  • +
  • -

#4 jayroll123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-February 12

Re: How can i populate combobox from database's items?

Posted 11 February 2012 - 09:15 AM

I'm sorry sir. .dont read post #3 . .:)
The connection is working sir. .
There's no error. the problem is it doesn't populate the combobox.
When i tried this one. .it populates the combobox


MySqlDataAdapter da = new MySqlDataAdapter("
    select roomNo from room",connection );



and when i tried this it run but it fails to populate

MySqlDataAdapter da = new MySqlDataAdapter("select roomNo from room where roomType =
        '"+cboRoomType.Text+"'",connection );



please help me sir. . thanks . .

thanks a lot sir . . have a nice day . .Godbless .. :) :bigsmile:

This post has been edited by tlhIn`toq: 11 February 2012 - 09:24 AM

Was This Post Helpful? 0
  • +
  • -

#5 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: How can i populate combobox from database's items?

Posted 11 February 2012 - 12:30 PM

I'm not sure you can concatenate strings in the parameter. Try using string.Format():

MySqlDataAdapter da = new MySqlDataAdapter(string.Format("select roomNo from room where roomType = '{0}'", cboRoomType.Text), connection);



Some reference:

string.Format()
Was This Post Helpful? 0
  • +
  • -

#6 jayroll123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-February 12

Re: How can i populate combobox from database's items?

Posted 11 February 2012 - 08:16 PM

It's already working sir. .thanks for your reply. .its not working if i put it in the loader. But it works if i put it on a button; that's my problem now.. it needs a button to work. .it's not automatic. . :mellow:
Was This Post Helpful? 0
  • +
  • -

#7 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: How can i populate combobox from database's items?

Posted 12 February 2012 - 06:39 AM

Oh, you want to automate it?

If you want it to execute on some trigger then take a look at custom events. Or if you want it to execute after some time interval then try using a timer control.

Some Reference:
Quick And Easy Custom Events
Delegates, Lambdas And Events
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1