Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Best practice for creating multiple columns in dataset?

Posted on 2008-06-25
4
Medium Priority
?
296 Views
Last Modified: 2013-12-16
Hi
I have a table in a dataset that must contain a lot of columns. The data is answers from a questionnaire - and every question has 10 answers. Like so ...

Column name 1: Question1_00
Column name 2: Question1_03
Column name 3: Question1_05
Column name 4: Question1_06
Column name 5: Question1_07
Column name 6: Question1_08
Column name 7: Question1_09
Column name 8: Question1_10
Column name 9: Question1_11
Column name 10: Question1_13

Same  goes for question 2, question 3 and so on ... I tried doing this using enums - like the code below. It works, but doesn't seem like a greate idea. Any help?

enum Grade13
{
    grade00,
    grade03,
    grade05,
    grade06,
    grade07,
    grade08,
    grade09,
    grade10,
    grade11,
    grade13
}
 
 
// ----------
 
dsSummery = new DataSet();
DataTable dtSummery = new DataTable("Answers");
dtSummery.Columns.Add(new DataColumn("ID"));
 
foreach (int grade in Enum.GetValues(typeof(Grade13)))
{
    dtSummery.Columns.Add(new DataColumn("Question1_" + grade.ToString()));
}
 
dsSummery.Tables.Add(dtSummery);

Open in new window

0
Comment
Question by:Balltown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 4

Expert Comment

by:Joni Kettunen
ID: 21863874
Why can't your DataSet contain multiple tables instead of multiple columns?

For example with two tables: Question and Answer you could make it lot easier.

Table Question with fields QuestionId and QuestionText
Table Answer with fields QuestionId, OrderNbr, AnswerText

You can link QuestionId fields if you want to, but in simple scenario you'd just create counter starting from one. First question would have QuestionId 1 and QuestionText.

According to QuestionId you would add rows to Answer table.
1st answer would have QuestionId 1, OrderNbr 1, and AnswerText.
2nd answer would have QuestionId 1, OrderNbr 2, and AnswerText.
...

Does this sound like a feasible solution to your problem?
0
 

Author Comment

by:Balltown
ID: 21864004
Hmm maybe theres is a misunderstanding. I'm only working on the answers - the questions doesn't matter right now. Maybe it's my naming of the columns that confuses... "Question1_XX" is the answer to question 1. A snip of the "Answers"-table would look like:

Subject ; Question1_00 ; Question1_03 ; Question1_05 ;
Math ; 5 ; 1 ;  6 ;
English ; 1 ; 0 ; 2 ;
Geo ; 0 ; 4 ; 3 ;

... As if: Five people voted "Question1_00" in Math and zero people voted "Question1_03" in English.

I could do a for-loop:
for (int i=0; i <=10; i++)
{
    dtSummery.Columns.Add(new DataColumn("Question1_" + i));
}
But then I wouldn't get the correct naming of the column as shown in my first post.
0
 
LVL 4

Accepted Solution

by:
Joni Kettunen earned 500 total points
ID: 21864086
If you are looking for just performance there are things to avoid. One thing is to avoid string catenation.
For example

string badString = "foo" + "bar"; // Creates three string "foo", "bar" and "foobar".

// Worst
string s = "";
for (int i=0; i<10; i++)
{
  s = s + i.ToString(); // creates new string in memory for each loop. Very slow when i < 1000.
}

Answer is to use StringBuilder when there are lots of catenations or in simple case String.Format.

One example to your case could be:

But to your question.

// Just numbers in string to avoid unnecessary cast
string[] answerNbrArray = { "00", "03", "05", "06", "07", "08", "09", "10", "11", "13" };
 
// Iterating through questions
for (int qNbr = 0; qNbr < 5; qNbr++)
{
    // Iterating through answers
    for (int aNbr = 0; aNbr < answerNbrArray.Length; aNbr++)
    {
        string colName = String.Format("Question{0}_{1}", qNbr, answerNbrArray[aNbr]);
        // Assing the colName    
        dtSummery.Columns.Add(new DataColumn(colName));
    }
}

Open in new window

0
 

Author Comment

by:Balltown
ID: 21864258
Thanks Valkoturkki. Of course that's it.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

721 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