Solved

How to use a DataSet as a DataSource for a RadioButtonList

Posted on 2006-07-18
12
510 Views
Last Modified: 2008-01-09
Hi,

I'm using ASP.NET 1.1.

I have a DataSet. When I use GetXml to look at its structure, it look like this:

<NewDataSet>
  <Table>
    <Answers>Answer 1, Question 1, Mod 1</Answers>
    <AnswersID>1</AnswersID>
    <QuestionsID>1</QuestionsID>
    <QuestionsID1>1</QuestionsID1>
    <ModuleID>1</ModuleID>
    <ModuleID1>1</ModuleID1>
  </Table>
  <Table>
    <Answers>Answer 2, Question 1, Mod 1</Answers>
    <AnswersID>2</AnswersID>
    <QuestionsID>1</QuestionsID>
    <QuestionsID1>1</QuestionsID1>
    <ModuleID>1</ModuleID>
    <ModuleID1>1</ModuleID1>
  </Table>
  <Table>
    <Answers>Answer 3, Question 1, Mod 1</Answers>
    <AnswersID>3</AnswersID>
    <QuestionsID>1</QuestionsID>
    <QuestionsID1>1</QuestionsID1>
    <ModuleID>1</ModuleID>
    <ModuleID1>1</ModuleID1>
  </Table>
  <Table>
    <Answers>Answer 4, Question 1, Mod 1</Answers>
    <AnswersID>4</AnswersID>
    <QuestionsID>1</QuestionsID>
    <QuestionsID1>1</QuestionsID1>
    <ModuleID>1</ModuleID>
    <ModuleID1>1</ModuleID1>
  </Table>
</NewDataSet>


How can I specify that I want to use the content of <Answers>?  I have tried a couple of things, but no luck so far.
Thanks,
Jens
0
Comment
Question by:tmccrank
  • 7
  • 3
  • 2
12 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17133559
Here is an example with VB.NET:

Me.RadioButtonList1.DataSource = Me.DataSet1.Tables("Answers")
Me.RadioButtonList1.DataTextField = "Answers"
Me.RadioButtonList1.DataValueField = "AnswersID"
Me.RadioButtonList1.DataBind()

Bob
0
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 17133654
I think you should consider custom databinding classes. Your class can search the database and depending on the result return the collection to be bind in radio button control:

Implementing Custom Data Bindable Classes: CollectionBase
http://www.ondotnet.com/pub/a/dotnet/2003/06/02/collectionbase.html

--Nauman.
0
 

Author Comment

by:tmccrank
ID: 17134110
Thanks Bob, but no luck.

FWIW, when I typed the first line:

Me.rblAnswers.DataSource = Me.dsAnswers.Tables("Answers")

...the intellisence wasn't bringing up dsAnswers.  However, dsAnswers is definitely recognized because dsAnswers.Tables("Answers") doesn't return a build error.

Jens
0
 

Author Comment

by:tmccrank
ID: 17134122
Nauman,

Sounds interesting.  I'd like to exhaust the possibilities the way Bob has gone first, I'm more familiar with how they work.

thanks,
Jens
0
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 17134277
There is one thing that is kinda confusing:

<Answers>Answer 1, Question 1, Mod 1</Answers>

Does it has three answer options or all the items are counted as one answer?

-Nauman.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17135337
Only use Me, if the DataSet is defined at module-level.  If it is defined locally within a method, remove it.

Bob
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:tmccrank
ID: 17138994
Bob,

While debugging, rblAnswers.DataSource = dsAnswers.Tables("Answers") is coming up as "Nothing".

I tried a couple of different things, including:

rblAnswers.DataSource = dsAnswers.Tables(0).Columns(0).ToString

Each letter of the word 'Answers' simply becomes the content of a radio button.   Any other idea??  Thanks


Nauman, I just did that for my own reference; it helps me to check if the DB and databindings are working properly.  The contents will be replaced with 'real' answers once I have this stuff figured out.
0
 

Author Comment

by:tmccrank
ID: 17139004
Nauman, sorry, to answer your question, it all counts as one answer.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 17139017
If there is a post-back, and you are not persisting the DataSet, then it will be lost.  Also, I guessed at the name of the table 'Answers'--is that the correct name for the table?

Bob
0
 

Author Comment

by:tmccrank
ID: 17139252
I looks to me like the name of the table is "Table".  "Answers" is the first column in "Table".  With that in mind, I tried this:

rblAnswers.DataSource = dsAnswers.Tables("Table").Columns("Answers").ToString()

I got the same result as with rblAnswers.DataSource = dsAnswers.Tables(0).Columns(0).ToString, which makes sense seeing as I just used the names of the columns instead of the index.

The DataSet seems to be persisting, if only because radio buttons are populated with A, n, s, w, e, r
0
 

Author Comment

by:tmccrank
ID: 17139614
Bob,

OK, with your help I'm now one step closer to my goal, thanks.  Here's what I have:

                rblAnswers.DataSource = dsAnswers.Tables("Table")
                rblAnswers.DataTextField = "Answers"
                rblAnswers.DataValueField = "AnswersID"

The good news is, the RadioButtonList under each Question is being populated with the content of each <Answers> column.  
BUT each RadioButtonList has all 5 <Answers> for all 5 <Questions> that I have in my testing Database.  This means that each RBL has 25 <Answers>, not 5 <Answers>.

But that's another question that I may put out there.  Thanks again for your help on this one.

Jens
0
 

Author Comment

by:tmccrank
ID: 17139625
It was the fact that I was putting the name of the column where the name of the Table should be.
Thx.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now