How to retrieve several rows from database

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 895 Views - Last Post: 12 April 2013 - 05:04 AM Rate Topic: -----

#1 StaticCrazee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 11-April 13

How to retrieve several rows from database

Posted 11 April 2013 - 10:33 AM

Okay, so I've made a new project which allows users to send messages to each other. I've almost got it finished, just a few things need to be cleared up.

I've successfully allowed the user to send the message (via mysql). Now Im at the part where Im supposed to retrieve the message and display it in the form; this is where Im stuck. It'd be easy to do if the user has only 1 message in the database, but what if he had more than 1? How would I retrieve them all and display them in the form. Im using this query:

SELECT ToID FROM Message WHERE (ID LIKE @ID)


What it does is check the database for any messages containing the recipient as the user's ID. If the database does in fact contain any message, then the program will display them in the textboxes. But in the case if more than 1 message, how would the database display them in the form? The form has textboxes for: 1. Sender 2. Subject 3. Date.

This is the query I hoped to use for retrieving the message:

SELECT ID, Title, Body, Date, FromUsername FROM Message WHERE (ID LIKE @ID)


Please help? Any other solutions are fine as long as its easy to understand(im new to vb). Much appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: How to retrieve several rows from database

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8922
  • View blog
  • Posts: 33,445
  • Joined: 12-June 08

Re: How to retrieve several rows from database

Posted 11 April 2013 - 10:36 AM

If you are getting more than one row of data back then single textboxes won't work, right? Perhaps use a grid.
Was This Post Helpful? 0
  • +
  • -

#3 StaticCrazee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 11-April 13

Re: How to retrieve several rows from database

Posted 11 April 2013 - 12:48 PM

Yup, so I was hoping the program could make textboxes automatically and display information in each.. The code for it is:
Controls.Add(New TextBox)
Its a pretty simple code, though I dont know how to use it here to display the messages..

I guess your idea is better though. So how would I use a grid to display results? Im new to vb, so be as simple as possible..
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8922
  • View blog
  • Posts: 33,445
  • Joined: 12-June 08

Re: How to retrieve several rows from database

Posted 11 April 2013 - 01:09 PM

Check out the VB.NET tutorials- there are great examples of database interaction there!

The gist is you use an adapter and a connection object to facilitate the query to the database... fill a dataset object from the adapter, and then set the grid's datasource to said dataset.
Was This Post Helpful? 0
  • +
  • -

#5 StaticCrazee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 11-April 13

Re: How to retrieve several rows from database

Posted 11 April 2013 - 01:44 PM

Alright, where are the vb.net tutorials? Are they on this website? Would really love to see them. Most of the program is dependent on the sql, so I'd have to be fairly familiar with it.

Im using the database object in visual studio so I dont have to use mysql server programs... I've made the query to retrieve the data but when I link it to the listbox this is what I get:
'complex datbinding accepts as a data source either an IList or an IListSource. What's wrong?

Also, on a sidenote, whenever I run my program (not from visual studio) and close it, its still running in the background according to task manager. How would I prevent this?
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8922
  • View blog
  • Posts: 33,445
  • Joined: 12-June 08

Re: How to retrieve several rows from database

Posted 11 April 2013 - 01:48 PM

Quote

Alright, where are the vb.net tutorials? Are they on this website? Would really love to see them. Most of the program is dependent on the sql, so I'd have to be fairly familiar with it.

The big ol' "tutorial" button at the top of every pae.


Quote

Im using the database object in visual studio so I dont have to use mysql server programs... I've made the query to retrieve the data but when I link it to the listbox this is what I get:
'complex datbinding accepts as a data source either an IList or an IListSource. What's wrong?

I have no idea - I haven't seen your code to know what your code is doing.

Quote

Also, on a sidenote, whenever I run my program (not from visual studio) and close it, its still running in the background according to task manager. How would I prevent this?

I have no idea - I haven't seen your code to know what your code is doing.
Was This Post Helpful? 0
  • +
  • -

#7 StaticCrazee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 11-April 13

Re: How to retrieve several rows from database

Posted 11 April 2013 - 02:03 PM

Lol, alright..

Theres not much code though. Just the line you told me to add:

ListBox1.DataSource = MessageTableAdapter1.GetMessage(UsersTableAdapter1.GetID(True))


What my query does is retrieves the message from the query UsersTableAdapter1.GetID, which gets the id of the current logged in user (Since the MessageTableAdapter1.GetMessage needs the user id to begin with). Everything's in order but I get the annoying error.

And the code for the whole program is pretty big. This message system is actually part of it. So I cant really show you the code. So generally how does a program still run when I press the 'X' button? Though I have a guess... The me.hide command hides the form from the user, right? So if I just keep switching between forms in the program, the form I close is closed but the original is still hiding I suppose and didnt close yet so its still in the task manager. If this is true, how would you prevent it?
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8922
  • View blog
  • Posts: 33,445
  • Joined: 12-June 08

Re: How to retrieve several rows from database

Posted 11 April 2013 - 02:19 PM

Yes.. 'hide' does not stop t he process it just discontinues the GUI.
Was This Post Helpful? 0
  • +
  • -

#9 StaticCrazee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 11-April 13

Re: How to retrieve several rows from database

Posted 11 April 2013 - 02:26 PM

Okay. So how would I switch between forms while closing the previous form? I dont want it to still run when I close it.

And returning back to the topic, how would I stop the error I showed from reappearing?
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8922
  • View blog
  • Posts: 33,445
  • Joined: 12-June 08

Re: How to retrieve several rows from database

Posted 11 April 2013 - 02:34 PM

Again I have no idea how you are opening your forms, or in what conditions you want to stop the application from processing and not just hide the form.. Think of the main form that loads up as the main trunk of a tree.. any new forms are branches off it. Stopping a branch just affects its local area. If you want to close the intiating form.


Is that error from that solitary line of code you put up? The fix is to not do what ever you think you are doing there.

Read up on what you are trying to do.

Listbox's datasource property
http://msdn.microsof...datasource.aspx

data adapters
http://msdn.microsof...ataadapter.aspx
Was This Post Helpful? 0
  • +
  • -

#11 StaticCrazee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 11-April 13

Re: How to retrieve several rows from database

Posted 11 April 2013 - 03:03 PM

Yes! Looks like the problem was the arraylist. :P Thanks!

Now that its retrieved, it shows all the information in 1 area. I want the information to be formatted like so:
Left hand side is for subject, middle is the sender, right is the date. And when the user clicks the subject, the message appears showing the body. But this is very complicated, couldnt find it in msdn...
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8922
  • View blog
  • Posts: 33,445
  • Joined: 12-June 08

Re: How to retrieve several rows from database

Posted 11 April 2013 - 03:06 PM

I am not following. ARe you talking a grid, a listbox, or what?

Typically the grid has a 'row selected' event you would want to capture and deal with to get your desired effect.
Was This Post Helpful? 0
  • +
  • -

#13 StaticCrazee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 11-April 13

Re: How to retrieve several rows from database

Posted 11 April 2013 - 03:09 PM

What I need is for the data from the table to be formatted. Like the subject, sender and date in different columns. But they appear as rows. I want the rows for different messages, like if more than 1 message was sent. If this does not apply for listbox, then what other things can I user? Hope you understand..
Was This Post Helpful? 0
  • +
  • -

#14 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8922
  • View blog
  • Posts: 33,445
  • Joined: 12-June 08

Re: How to retrieve several rows from database

Posted 11 April 2013 - 03:25 PM

No, I don't understand. So you are indeed using a grid? IS the data all going into one cell, or what?
Was This Post Helpful? 0
  • +
  • -

#15 StaticCrazee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 11-April 13

Re: How to retrieve several rows from database

Posted 11 April 2013 - 03:36 PM

No, Im using a listbox. Its working out fine. But all the data is 'raw'. What I mean is the database just shoved everything into the listbox. I need the data to be formatted so that all the columns from the database are sorted into its appropriate fields, like subject goes into the subject field, sender in the sender field, etc. Because when the data is retrieved from the database, the listbox just displays it, I want to format it. Any ideas? Though I dont think the listbox will work for the formatting, so are there any other tools?

Hope this time you understand :sweatdrop:
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2