8 Replies - 48858 Views - Last Post: 13 May 2013 - 04:57 AM Rate Topic: -----

#1 yOyie   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 12-May 13

How to get values from listview and display it to textbox using sql

Posted 12 May 2013 - 08:21 AM

How to get values from listview and display it to textbox using sql database ?
and then i can update the items in listview using a button..then refresh the listview
after i click the button...


tnx for the help in advance..im just a newbie in vb.net



 if listview1.selecteditems.count > 0 then 
 TextBox1.Text = .Items(.SelectedIndices(0)).Text
 TextBox2.Text = .Items(.SelectedIndices(0)).SubItems(1).Text
End If

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: How to get values from listview and display it to textbox using sql

#2 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: How to get values from listview and display it to textbox using sql

Posted 12 May 2013 - 09:37 AM

Not sure where you're at with this but here is a tutorial on accessing an SQL database

I would start there and then we can give you more specific help.

The code you supplied would seem to fill the textboxes as you want so I'm wondering if you have tried the reverse to get any changes in the textbox back into the listview.

Basically, you're saying that the item you have selected should be put in the textbox. Look at how that works and do it in reverse. From there the tutorial should help you with the basics of inserting and updating a database enough to get more specific in your questions.
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15357
  • View blog
  • Posts: 61,575
  • Joined: 12-June 08

Re: How to get values from listview and display it to textbox using sql

Posted 12 May 2013 - 09:42 AM

Have you looked into database interactions?

Flip through our tutorials and you should find great help.

Example:

OleDb Basics in VB.Net

A Really Simple Database Create a Database using Access & VB.net Express 2008
Was This Post Helpful? 0
  • +
  • -

#4 yOyie   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 12-May 13

Re: How to get values from listview and display it to textbox using sql

Posted 12 May 2013 - 05:37 PM

View PostCharlieMay, on 12 May 2013 - 09:37 AM, said:

Not sure where you're at with this but here is a tutorial on accessing an SQL database

I would start there and then we can give you more specific help.

The code you supplied would seem to fill the textboxes as you want so I'm wondering if you have tried the reverse to get any changes in the textbox back into the listview.

Basically, you're saying that the item you have selected should be put in the textbox. Look at how that works and do it in reverse. From there the tutorial should help you with the basics of inserting and updating a database enough to get more specific in your questions.


If ListView1.SelectedItems.Count > 0 Then
            conn.Open()
            With com
                .Connection = conn
                .CommandText = "SELECT * FROM ItemTBL "
                .CommandType = CommandType.Text
            End With
            rdr = com.ExecuteReader
            While rdr.Read
                AddTXT.Text = rdr("Stock")
            End While
            rdr.Close()
            conn.Close()


thats my code..i did manage to click an item from listview in display it to textbox but my problem is...it only displays the last record...why is that ?
Was This Post Helpful? 1
  • +
  • -

#5 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: How to get values from listview and display it to textbox using sql

Posted 12 May 2013 - 05:55 PM

so the reader may contain multiple rows from your query depending on the match

SELECT * FROM ItemTBL has no WHERE clause to limit the results so it is pulling every row in your table.

Now, you are putting the field in textbox so with each iteration (loop) through the reader you are setting the text = to the field of the current row. In this case that field is Stock. And since the computer can loop through these records at a high rate of speed it appears that only the last result is showing, when in fact, it is being set each time.

If you were to say... add this field to a listbox which holds multiple items, you would see all the Stock values in your table in that list.

You can either limit your query with a WHERE clause or use a container that holds multiple results.

So now that you have gotten this far, what exactly are you trying to achieve so that we can help you with a more precise solution?

I guess, I should add that I see what you're doing in your first post, but my question is why not add that information to the listview?

This post has been edited by CharlieMay: 12 May 2013 - 05:58 PM

Was This Post Helpful? 0
  • +
  • -

#6 yOyie   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 12-May 13

Re: How to get values from listview and display it to textbox using sql

Posted 12 May 2013 - 06:37 PM

Because I want to update the item in listview using the textbox by clicking the update button..now i'm stuck with this thing..hehe

Attached image(s)

  • Attached Image

This post has been edited by GunnerInc: 12 May 2013 - 07:52 PM
Reason for edit:: Quote be gone

Was This Post Helpful? 0
  • +
  • -

#7 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: How to get values from listview and display it to textbox using sql

Posted 12 May 2013 - 06:50 PM

OK, I guess, my thoughts are:

Run the query and populate the listview with the Brand and Description.

Upon selecting an item in the listview, the two textboxes would be populated with the columns.

The user changes the text in the textbox and clicks update.

The listview is updated from the textboxes

The database is updated with the changes.

So, when you populate the listview. I would suggest you add the Primary Key to the listviewitem's .Tag property. This will be invisible to the user but you can use it to ensure you are updating the proper row.

This, of course, would require an update statment to be applied using the textbox values along with the focusedItem.Tag property of the listview as the values for the update statement.

Something like:

UPDATE yourTable SET Brand = @brand, Description = @Desc WHERE YourPrimaryKeyField = @ID

Then you would set those parameters with

TxtBrand.Text
txtDescription.Text
YourListView.FocusedItem.Tag

All names of controls are assumed.

Execute that update statement and upon success, set the focuseditem columns from the new entries of the textboxes.

Just an FYI the update example I gave uses parameters as placeholders for the data entered by the user. It is a secure method and should be used instead of concatenating values.

This post has been edited by CharlieMay: 12 May 2013 - 06:59 PM

Was This Post Helpful? 0
  • +
  • -

#8 yOyie   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 12-May 13

Re: How to get values from listview and display it to textbox using sql

Posted 12 May 2013 - 07:42 PM

 If ListView1.SelectedItems.Count > 0 Then
            conn.Open()
            With com
                .Connection = conn
                .CommandText = "SELECT * FROM ItemTBL "
                .CommandType = CommandType.Text
            End With
            rdr = com.ExecuteReader
            While rdr.Read
                AddTXT.Text = rdr("Stock")
            End While
            rdr.Close()
            conn.Close()


yeah..but how can i display the item in the listview to the textbox ? my coding doesn't work.. :helpsmilie:/>/>

*** EDIT ***
No need to quote post above your reply.

This post has been edited by GunnerInc: 12 May 2013 - 07:52 PM
Reason for edit:: Quote be gone

Was This Post Helpful? 0
  • +
  • -

#9 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: How to get values from listview and display it to textbox using sql

Posted 13 May 2013 - 04:57 AM

OK, Here is where I think you're getting confused.

The database code above should be used to fill the listview with the items from your query that you want to display in the list.

You could do this on Form_Load so that when the form opens, the listview gets populated.


Then use the SelectedIndexChanged event with the code you originally supplied to take what the user selects and put it into the textboxes.

From there, you would create and execute an update statement to get the information in the database updated to the users changes using your update button.

Then reverse the code you used in your original post to set the listview items to what the user put in the textbox.

Another method that works well for getting the information that is selected in the listview is FocusedItem

ListView1.FocusedItem.Text
ListView1.FocusedItem.Subitems(index).Text

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1