Results 1 to 10 of 10

Thread: [RESOLVED] [:: Displaying items from Database into ListBox - Please help ::]

  1. #1

    Thread Starter
    Hyperactive Member Trusted's Avatar
    Join Date
    Jun 2010
    Location
    Don't Know, Cause, I am a Newbie :P
    Posts
    269

    Resolved [RESOLVED] [:: Displaying items from Database into ListBox - Please help ::]

    Hello all,

    I want to get items into the ListBox from Database on FormLoad.

    My code is:

    database Code:
    1. Private Sub Models_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.  
    3.         Dim qry As String
    4.         qry = "SELECT model FROM models"
    5.  
    6.         Dim sqlcon As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\My Personal\VB Projects\Mobile Repairing\Mobile Repairing\repairs.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
    7.        
    8.         Dim sqlcmd As New SqlCommand(qry, sqlcon)
    9.         Dim dr As SqlDataReader
    10.  
    11.         sqlcon.Open()
    12.         dr = sqlcmd.ExecuteReader
    13.         dr.Read()
    14.  
    15.         If dr.HasRows Then
    16.             While dr.Read()
    17.                 ModelListBox.Items.Add(ToString)
    18.             End While
    19.         End If
    20.  
    21.         dr.Close()
    22.         sqlcon.Close()
    23.  
    24.     End Sub

    I am already having three models in database that has to be loaded in ListBox on formload.

    Any help will be appreciated.

    Thanks in advance.

    Trusted




    <= Please click on and rate if you like this post

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [:: Displaying items from Database into ListBox - Please help ::]

    try this:

    vb Code:
    1. Dim qry As String
    2. qry = "SELECT model FROM models"
    3.  
    4. Dim sqlcon As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\My Personal\VB Projects\Mobile Repairing\Mobile Repairing\repairs.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
    5.  
    6. Dim sqlcmd As New SqlCommand(qry, sqlcon)
    7. Dim dr As SqlDataReader
    8.  
    9. sqlcon.Open()
    10. dr = sqlcmd.ExecuteReader
    11. dr.Read()
    12.  
    13. If dr.HasRows Then
    14.     While dr.Read()
    15.         ModelListBox.Items.Add(dr.GetString(0))
    16.     End While
    17. End If
    18.  
    19. dr.Close()
    20. sqlcon.Close()

  3. #3

    Thread Starter
    Hyperactive Member Trusted's Avatar
    Join Date
    Jun 2010
    Location
    Don't Know, Cause, I am a Newbie :P
    Posts
    269

    Re: [:: Displaying items from Database into ListBox - Please help ::]

    Hello Paul,

    Thanks a lot for your reply.

    It worked very nicely... BUT... it is missing the first row from database. That means, I have three rows and it is displaying only two rows.

    Trusted




    <= Please click on and rate if you like this post

  4. #4

    Thread Starter
    Hyperactive Member Trusted's Avatar
    Join Date
    Jun 2010
    Location
    Don't Know, Cause, I am a Newbie :P
    Posts
    269

    Re: [:: Displaying items from Database into ListBox - Please help ::]

    Hey it worked...

    I simply commented 'dr.read() on line number 11 above in your code, and it worked perfectly.



    Thanks again,

    Can you please help me with updating ListBox items to the database as I will be adding new items from textbox to Listbox.

    Trusted

    Trusted




    <= Please click on and rate if you like this post

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [:: Displaying items from Database into ListBox - Please help ::]

    Use a DataAdapter to populate a DataTable and then bind that to the ListBox. Add your new items to the DataTable rather than the ListBox. Use the same DataAdapter to save the changes back to the database. Follow the Database FAQ link in my signature for more information on using ADO.NET.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member Trusted's Avatar
    Join Date
    Jun 2010
    Location
    Don't Know, Cause, I am a Newbie :P
    Posts
    269

    Re: [:: Displaying items from Database into ListBox - Please help ::]

    Hello JMC,

    Thank you for your response.

    As I am very new to VS [ as you know that ], this is my first attempt for database, so bad me, I don't understand most of the things and terms.

    But with the help of your above post, I did add using INSERT directly to the database, and it is working fine. My code is as follows:

    vb Code:
    1. Private Sub btnAddModel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddModel.Click
    2.  
    3.         Dim qry As String
    4.         Dim sqlcon As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\My Personal\VB Projects\Mobile Repairing\Mobile Repairing\repairs.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
    5.  
    6.         If TextBox1.Text <> "" Then
    7.             ModelListBox.Items.Add(TextBox1.Text)
    8.  
    9.             qry = "insert into models(model) values('" & TextBox1.Text & "')"
    10.  
    11.             Dim sqlcmd As New SqlCommand(qry, sqlcon)
    12.             sqlcon.Open()
    13.             sqlcmd.ExecuteNonQuery()
    14.             sqlcon.Close()
    15.             TextBox1.Clear()
    16.         Else
    17.             MsgBox("Please enter a Model in the above given box")
    18.         End If
    19.  
    20.     End Sub

    Now I got stuck for deleting a particular row from database.

    Any help and/or any corrections will be appreciated.

    Trusted




    <= Please click on and rate if you like this post

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [:: Displaying items from Database into ListBox - Please help ::]

    If you want to delete one record at a time then the code is basically the same as you have for inserting except the SQL code will change. That said, you should get into good habits now and start using parameters rather than string concatenation to build SQL statements. The resources in the Database FAQ thread provide examples and there's a link in my signature to a blog post dedicated to the topic.

    Rather than editing one record at a time directly in the database, you might want to consider editing the data locally first and then saving as a batch. Again, the Database FAQ resources provide examples, including one in my own CodeBank thread.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Hyperactive Member Trusted's Avatar
    Join Date
    Jun 2010
    Location
    Don't Know, Cause, I am a Newbie :P
    Posts
    269

    Re: [:: Displaying items from Database into ListBox - Please help ::]

    Hi JMC,

    Thanks for your response. My app is deleting a particular row successfully now

    From your signature, I am TRYING to learn SQL statements. If I find any difficulty, I will ask in a new thread. Still every term is new to me, I have to understand it slowly step-by-step. Thanks all for the help.

    Trusted




    <= Please click on and rate if you like this post

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [:: Displaying items from Database into ListBox - Please help ::]

    If this issue is resolved then please use the Thread Tools menu to mark the thread Resolved. Also note that, if you do have SQL questions down the track, they should be asked in the Database Development forum.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Hyperactive Member Trusted's Avatar
    Join Date
    Jun 2010
    Location
    Don't Know, Cause, I am a Newbie :P
    Posts
    269

    Re: [:: Displaying items from Database into ListBox - Please help ::]

    Sure I will,

    But the problem is I tried raising the SQL questions in Database Development Forum, but I get better response in here

    Trusted




    <= Please click on and rate if you like this post

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width