We help IT Professionals succeed at work.

Fill a listview from a dataset (VB.NET)

Kyle93815
Kyle93815 asked
on
2,710 Views
Last Modified: 2012-06-27
I have a data adapter and a dataset I created.  They get data from an Access 2000 database table.  This part works fine.

I have a listview in detail view, with 4 columns.
     -Code Value
     -Segment ID
     -Ref Des
     -Code Description


I want to fill the listview with the contents of the dataset.  The dataset is named:
     -ds_Codes_Adjust1

-------------------------------

I know it must be simple, but I'm lost.  Any help would be greatly appreciated!


Comment
Watch Question

CERTIFIED EXPERT

Commented:

Author

Commented:
I appreciate the link, but I've already tried that.  I'm using an OleDbDataReader, not an sqldatareader, and I don't know enough to replace the right parts of the code in order to make it work.

I need a solution that uses an OleDbDataReader.

CERTIFIED EXPERT

Commented:
http://www.codeguru.com/mfc/comments/55795.shtml
and then read the comment on the bottom
CERTIFIED EXPERT

Commented:

Author

Commented:
I don't understand the comment on the codeguru example.  It says to change some code, but I don't see that code in the example.

Can you help?

Author

Commented:
Also, the following line doesn't work.  It says "type is not defined".



Dim lvhHelper As ListViewData = New ListViewData


CERTIFIED EXPERT

Commented:
http://www.codeguru.com/mfc/comments/55994.shtml

here is the contents of the Comment

Hi dicky

If you change the code below, myData.GetString(0) to myData.GetValue(0) then your code may work..


Do While myData.Read
                itmListItem = New ListViewItem()
                strTest = IIf(myData.IsDBNull(0), "", myData.GetString(0)) << 

>>myData.GetValue(0))


try this.

eRRO

Author

Commented:
Haha, I know what is says.  I'm sorry, but I don't understand it.  Could you explain?

Author

Commented:
I'm just not understanding.  How many points do I need to offer to have someone use the naming conventions I supplied in the question, and write out some actual code for me?  It's the only way I'm going to learn it, I think.

All of these examples are too different from what I need.  It's my ignorance, I know.  It's just that some examples are for SQL readers and I have to try and convert, and most of them are creating new database connections, adapters, etc.  I already have created my connection object, and the adapter and dataset.

I just need some simple code to take what I have and populate my listview with the columns I described in the original question.  I just don't have the grasp on this stuff to learn without an example using what I know.

Thanks to anybody that can help.
CERTIFIED EXPERT

Commented:
Hi Kyle93815,
I am ready to help you, as I often fill my listviews with data.
Before I start, I do need you to clarify what seems to be a contradiction.
At the start you mention that your dataset is ds_Codes_Adjust1.
You then later say that you are using an OleDBDataReader.

If you have a dataset, it really does not matter how the dataset got filled.
Can I assume that all you want is to fill the listview with the data in the dataset?
Can I assume that the dataset consists of just one table, and that the column names in the table match the column names of the headers of the listview?

Dabas

Author

Commented:
Thanks Dabas.

I'm sorry, I just get confused.  I'm not using a reader.  I have a data adapter I named db_Codes_Adjust, and a dataset I named ds_Codes_Adjust1.

Yes, I just want to fill the listview with the data from the dataset.  And yes, the dataset only has one table.  I wasn't even aware it could contain more.  I have a lot to learn.

My column names are similar to the database names.  The main difference is that I used an underscore for spaces in the database table column headers, and I did not in the listview column headers.

Thanks again.
CERTIFIED EXPERT

Commented:
Kyle:

What is the name you have given to the ListView?

Dabas
CERTIFIED EXPERT

Commented:
Also.

How did you create your Data Adapter and Data Set? In code, or using a .NET wizard?

Dabas

Commented:
Did you check this link of creating a data bound ListView, just as you would do in a datagrid. (This is to create a data bound listview control which you can use across just by providing the datasource to bind with.)

(http://msdn.microsoft.com/vbasic/using/columns/adventures/default.aspx?pull=/library/en-us/dnadvnet/html/vbnet08262002.asp)

kaykay!
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
I may not be able to try that code until tomorrow.

I didn't bind the listview because I want to populate it with different tables from different datasets based on user combo box selection.  There might be a better way that involves binding, but I know very little.  Very very little.



Author

Commented:
Oh, and I created my data adapter by dragging the table from the server explorer toolbar (on the left).  Then I right clicked and "generated dataset".

I suppose that is using a wizard?  

Thanks.
CERTIFIED EXPERT

Commented:
Kyle93815,
Yes, that is using a wizard.
And... I personally hate binding!

Because you used the wizard, make sure you have the following line preceding my code:

        db_Codes_Adjust.Fill(ds_Codes_Adjust1)

Dabas

Author

Commented:
That seems to work!  Great.  I appreciate it.  If I could mooch one more small answer off of this one, I want to use a parameter query before the listview is filled.

I set up the parameter statement in the data adapter, but I don't know how (or where) to set the parameter in code.

The parameter will come from:

txtCodeSearch.text


Thanks again.
CERTIFIED EXPERT

Commented:
Kyle93815:
I am glad that this SIMPLE solution is working for you!
Equally simple, you can write all of the Data Connection, adapter, dataset stuff without the need of the wizard as you did.
It will look neater, be more readable and put you in control of the program.
It also will make it easier if in the future you want to port your code to SQL Server for example, and will want to change from OLE to SQLClient.

As to your parameter question, it depends what you mean by "I set up the parameter statement in the data adapter, but I don't know how (or where) to set the parameter in code."
How did you set up the parameter in the data adapter. By amending the insert and update queries?

Is your objective to use the data in txtCodeSearch and other controls to update the data you are presenting?
I assume you come from a VB6 background, and you are starting to use VB.NET. If that is so, then you are in good company, as that would be the case of many of us here. One of the first things to do is to switch into .NET mode.
VB.NET is not VB7. It is a completely different language. Specially concerning DataBase related issues.
The most significant matter to "switch" to is that .NET is based on DISCONNECTED data. The idea is that once you have filled your dataset, you can make as many changes to it as you like, then update the source in one go once you are done.

To go back to your question. If your DataAdapter's Update query is set up properly, then all you need to do is to amend the relevant entry in your dataset, and whenever updating with your server is appropriate, just use the following command:

db_Codes_Adjust.Update(ds_Codes_Adjust1)

IF what you are after is how to find the relevant record within your dataset, then I recommend you read the help for the DataTable.Rows.Find function. (It is not that simple at first)

As you can see, this is not a "small" question any more! I will be more than willing to continue to help you on this one, but to follow EE's guidelines, it should be a new question, as it does not match your original question nor title.

Dabas

Author

Commented:
Thanks Dabas, I really appreciate your help.  Sorry for the delay in awarding the points, I've had a rough week..ha.

CERTIFIED EXPERT

Commented:
Kyle,
I was wondering where you were.
You must still have a rough week, as you forgot to allocate the points! ;-)

Dabas

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.