473,761 Members | 5,578 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting database fields (rows) to appear as columns in datagrid

I'm looking at a quick way to get results that are displayed as rows to
display as columns.

I have three tables:-
- The Questions for the survey
- The Results of the survey (Columns are listed as question numbers)
- The Survey Extra Results (As additional questions can be listed into
the Questions table, has 3 columns (a link/id to the survey id, a
link/id to the question id, and the answer the user gave).

I need to list the results of the survey and the extra results for the
survey on one row in a datagrid for each survey.

Any ideas?

Aug 6 '06 #1
4 3077
I currently have the following which basically makes a SQL query for
every single row required and makes a union, however this query can
easily get too complex and when 50+ surveys have been created, it will
start to lagg.

Dim j As Integer
dsSurveys = GetAllSurveys(" Attendee")
For j = 0 To dsSurveys.Table s(0).Rows.Count - 1

SelectQuery = ""
dsQuestions = GetAllQuestionN umbers("Attende es")
If dsQuestions.Tab les.Count <0 Then
If dsQuestions.Tab les(0).Rows.Cou nt <0 Then
Dim i As Integer
For i = 0 To
dsQuestions.Tab les(0).Rows.Cou nt - 1
If
GetColumns(dsQu estions.Tables( 0).Rows(i).Item (0).ToString) <"" Then
SelectQuery +=
GetColumns(dsQu estions.Tables( 0).Rows(i).Item (0).ToString) & " AS [" &
dsQuestions.Tab les(0).Rows(i). Item(0).ToStrin g & EndOfString(i,
CInt(dsQuestion s.Tables(0).Row s.Count) - 1)
Else
SelectQuery += " (SELECT
SurveyExtra.Sur veyExtra_Answer FROM SurveyExtra WHERE SurveyExtra_Gui d
= '{" &
dsSurveys.Table s(0).Rows(j).It em("Survey_Addi tionalQS").ToSt ring & "}'
AND SurveyExtra_Que stionGuid = '{" &
dsQuestions.Tab les(0).Rows(i). Item("Question_ GuID").ToString & "}') AS
[" & dsQuestions.Tab les(0).Rows(i). Item(0).ToStrin g & "] "
If Not i =
(CInt(dsQuestio ns.Tables(0).Ro ws.Count) - 1) Then
SelectQuery += ", "
End If
End If
Next
End If
End If

Query += "SELECT Survey.Survey_I d AS [Survey_Id],
Survey.Survey_G uID AS [Survey_GuID], " & SelectQuery & " FROM Survey
WHERE Survey.Survey_I d = " & dsSurveys.Table s(0).Rows(j).It em(0) & "
AND Survey_Type = 'Attendee'"

If j = (dsSurveys.Tabl es(0).Rows.Coun t - 1) Then
Query += ";"
Else
Query += " UNION "
End If

Next

Aug 7 '06 #2
Hello dallasfreeman,

kinda sounds like what you want is a crosstab query.

-Boo
I currently have the following which basically makes a SQL query for
every single row required and makes a union, however this query can
easily get too complex and when 50+ surveys have been created, it will
start to lagg.

Dim j As Integer
dsSurveys = GetAllSurveys(" Attendee")
For j = 0 To dsSurveys.Table s(0).Rows.Count - 1
SelectQuery = ""
dsQuestions = GetAllQuestionN umbers("Attende es")
If dsQuestions.Tab les.Count <0 Then
If dsQuestions.Tab les(0).Rows.Cou nt <0 Then
Dim i As Integer
For i = 0 To
dsQuestions.Tab les(0).Rows.Cou nt - 1
If
GetColumns(dsQu estions.Tables( 0).Rows(i).Item (0).ToString) <"" Then
SelectQuery +=
GetColumns(dsQu estions.Tables( 0).Rows(i).Item (0).ToString) & " AS [" &
dsQuestions.Tab les(0).Rows(i). Item(0).ToStrin g & EndOfString(i,
CInt(dsQuestion s.Tables(0).Row s.Count) - 1)
Else
SelectQuery += " (SELECT
SurveyExtra.Sur veyExtra_Answer FROM SurveyExtra WHERE SurveyExtra_Gui d
= '{" &
dsSurveys.Table s(0).Rows(j).It em("Survey_Addi tionalQS").ToSt ring & "}'
AND SurveyExtra_Que stionGuid = '{" &
dsQuestions.Tab les(0).Rows(i). Item("Question_ GuID").ToString & "}') AS
[" & dsQuestions.Tab les(0).Rows(i). Item(0).ToStrin g & "] "
If Not i =
(CInt(dsQuestio ns.Tables(0).Ro ws.Count) - 1) Then
SelectQuery += ", "
End If
End If
Next
End If
End If
Query += "SELECT Survey.Survey_I d AS [Survey_Id],
Survey.Survey_G uID AS [Survey_GuID], " & SelectQuery & " FROM Survey
WHERE Survey.Survey_I d = " & dsSurveys.Table s(0).Rows(j).It em(0) & "
AND Survey_Type = 'Attendee'"

If j = (dsSurveys.Tabl es(0).Rows.Coun t - 1) Then
Query += ";"
Else
Query += " UNION "
End If
Next

Aug 7 '06 #3
Thanks for your help

That's a function within Access though, this query has to be a straight
SQL.

or does the Crosstab Query eventually show the SQL behind it

By the way, the number of columns can differ each time, so the query
has to be able to handle x number of additional questions


GhostInAK wrote:
Hello dallasfreeman,

kinda sounds like what you want is a crosstab query.

-Boo
I currently have the following which basically makes a SQL query for
every single row required and makes a union, however this query can
easily get too complex and when 50+ surveys have been created, it will
start to lagg.

Dim j As Integer
dsSurveys = GetAllSurveys(" Attendee")
For j = 0 To dsSurveys.Table s(0).Rows.Count - 1
SelectQuery = ""
dsQuestions = GetAllQuestionN umbers("Attende es")
If dsQuestions.Tab les.Count <0 Then
If dsQuestions.Tab les(0).Rows.Cou nt <0 Then
Dim i As Integer
For i = 0 To
dsQuestions.Tab les(0).Rows.Cou nt - 1
If
GetColumns(dsQu estions.Tables( 0).Rows(i).Item (0).ToString) <"" Then
SelectQuery +=
GetColumns(dsQu estions.Tables( 0).Rows(i).Item (0).ToString) & " AS [" &
dsQuestions.Tab les(0).Rows(i). Item(0).ToStrin g & EndOfString(i,
CInt(dsQuestion s.Tables(0).Row s.Count) - 1)
Else
SelectQuery += " (SELECT
SurveyExtra.Sur veyExtra_Answer FROM SurveyExtra WHERE SurveyExtra_Gui d
= '{" &
dsSurveys.Table s(0).Rows(j).It em("Survey_Addi tionalQS").ToSt ring & "}'
AND SurveyExtra_Que stionGuid = '{" &
dsQuestions.Tab les(0).Rows(i). Item("Question_ GuID").ToString & "}') AS
[" & dsQuestions.Tab les(0).Rows(i). Item(0).ToStrin g & "] "
If Not i =
(CInt(dsQuestio ns.Tables(0).Ro ws.Count) - 1) Then
SelectQuery += ", "
End If
End If
Next
End If
End If
Query += "SELECT Survey.Survey_I d AS [Survey_Id],
Survey.Survey_G uID AS [Survey_GuID], " & SelectQuery & " FROM Survey
WHERE Survey.Survey_I d = " & dsSurveys.Table s(0).Rows(j).It em(0) & "
AND Survey_Type = 'Attendee'"

If j = (dsSurveys.Tabl es(0).Rows.Coun t - 1) Then
Query += ";"
Else
Query += " UNION "
End If
Next
Aug 11 '06 #4
Hello dallasfreeman,

SQL Server 2005 supports the PIVOT and UNPIVOT keywords for crasstab queries.

-Boo
Thanks for your help

That's a function within Access though, this query has to be a
straight SQL.

or does the Crosstab Query eventually show the SQL behind it

By the way, the number of columns can differ each time, so the query
has to be able to handle x number of additional questions

GhostInAK wrote:
>Hello dallasfreeman,

kinda sounds like what you want is a crosstab query.

-Boo
>>I currently have the following which basically makes a SQL query for
every single row required and makes a union, however this query can
easily get too complex and when 50+ surveys have been created, it
will start to lagg.

Dim j As Integer
dsSurveys = GetAllSurveys(" Attendee")
For j = 0 To dsSurveys.Table s(0).Rows.Count - 1
SelectQuery = ""
dsQuestions = GetAllQuestionN umbers("Attende es")
If dsQuestions.Tab les.Count <0 Then
If dsQuestions.Tab les(0).Rows.Cou nt <0 Then
Dim i As Integer
For i = 0 To
dsQuestions.T ables(0).Rows.C ount - 1
If
GetColumns(ds Questions.Table s(0).Rows(i).It em(0).ToString) <""
Then
SelectQuery +=
GetColumns(ds Questions.Table s(0).Rows(i).It em(0).ToString) & " AS ["
&
dsQuestions.T ables(0).Rows(i ).Item(0).ToStr ing & EndOfString(i,
CInt(dsQuesti ons.Tables(0).R ows.Count) - 1)
Else
SelectQuery += " (SELECT
SurveyExtra.S urveyExtra_Answ er FROM SurveyExtra WHERE
SurveyExtra_G uid
= '{" &
dsSurveys.Tab les(0).Rows(j). Item("Survey_Ad ditionalQS").To String &
"}'
AND SurveyExtra_Que stionGuid = '{" &
dsQuestions.T ables(0).Rows(i ).Item("Questio n_GuID").ToStri ng & "}')
AS
[" & dsQuestions.Tab les(0).Rows(i). Item(0).ToStrin g & "] "
If Not i =
(CInt(dsQuest ions.Tables(0). Rows.Count) - 1) Then
SelectQuery += ", "
End If
End If
Next
End If
End If
Query += "SELECT Survey.Survey_I d AS [Survey_Id],
Survey.Survey _GuID AS [Survey_GuID], " & SelectQuery & " FROM Survey
WHERE Survey.Survey_I d = " & dsSurveys.Table s(0).Rows(j).It em(0) & "
AND Survey_Type = 'Attendee'"
If j = (dsSurveys.Tabl es(0).Rows.Coun t - 1) Then
Query += ";"
Else
Query += " UNION "
End If
Next

Aug 14 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2842
by: Alex | last post by:
Hi all, I'm writing a small web application which searches a database based on a date field, and populates a datagrid control with the results. The datagrid control has selection buttons added to it to view additional details about the selected result (a second database query is triggered). I want this second query to pop up in a new window, the way it would if I used "window.open" in javascript. I've added a function in the
4
1543
by: sivashankar | last post by:
hello experts.... -i'm having a datagrid with 10 columns and 4 rows and one submit button. -the fields are eno,ename,eid,and 4 check boxes -after entering values when i clik the submit button the values i entered must passt to next page and displayed in datagrid and at the same time it must be inserted in the database -plz help me...how to do it.
0
1388
by: John Shum | last post by:
I prepare a UserControl that act as a lookup control, once the code is enter in the TextBox, the description will appear in another TextBox outside the UserControl of the same row in a DataGrid via javascript. I attempt to get the client ID of the TextBox (with provided ID of that TextBox as "ext_desc") by the following code inside the Page_Load() event of the UserControl (named Color_Control): for(int i = 0; i < DataGrid1.Items.Count;...
3
9925
by: | last post by:
I wrote a class in VB.NET to export the contents of a datagrid to Excel. It works perfectly on my machine, but it fails on my customers' PCs that have identical versions of Win XP (SP1) and Excel (SP1) installed. The error is: System.Runtime.InteropServices.COMException(0x800A03EC): Exception from HRESULT: 0x800A03EC. at Microsoft.Office.Interop.Excel._Worksheet.Paste(Object Destination, Object Link) at...
3
4279
by: Frustrated Developer via DotNetMonster.com | last post by:
I have posted a couple times on here already and found the user community to be very helpful. I took on a project before I realized how difficult a time I'm having working with a database. Assistance would be greatly appreciated! I am trying to allow certain users to be able to preview a database's contents without being able to update which I've accomplished. I've created a "Preview" button and set the datagrid to READ ONLY. Now I want...
9
3836
by: Wayne Smith | last post by:
I've come up against a major headache that I can't seem to find a solution for but I'm sure there must be a workaround and I would really be grateful of any help. I'm currently building a web site for a small club I belong to and one of the features I would like to include is the ability to allow users to upload image files. unfortunately the servers web root www folder only allows READ and EXECUTE permissions, which makes it...
1
1465
by: dallasfreeman | last post by:
I'm looking at a quick way to get results that are displayed as rows to display as columns. I have three tables:- - The Questions for the survey - The Results of the survey (Columns are listed as question numbers) - The Survey Extra Results (As additional questions can be listed into the Questions table, has 3 columns (a link/id to the survey id, a link/id to the question id, and the answer the user gave).
3
1378
by: =?Utf-8?B?SmliZXkgSmFjb2I=?= | last post by:
Hi: I have an interesting problem that I don't know how to solve. I need to display rows of one of these grid controls on two columns of the grid. So, row1 would appear on row1, column1, and row2 would appear on row1, column2 and row3 would appear on row2, column1 and row4 would appear on row2, column2 and so on. These grids do not allow adding rows dynamically, and that's a shame,
2
3577
by: rustyc | last post by:
Well, here's my first post in this forum (other than saying 'HI' over in the hi forum ;-) As I said over there: ... for a little side project at home, I'm writing a ham radio web site in uby/Rails. I started it in Perl and gave up on Perl as I went from the 'display the database information on the web page' to the 're-display the information from the database and allow the user to update the database using the web page' stage and realized...
1
7312
by: tucson | last post by:
I have a gridview that has a blank row with 2 input fields: file to upload, and a description of the file. They click Add and a new row is added, Remove and the row is removed. The problem is: When a new row is added, I loop through the existing gridview rows, store the data in a dataset, and rebind. In debug mode, I see the values I entered but when I rebind, it's not displayed in the gridview. Here's the code aspx code: ...
0
9788
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8794
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7342
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6623
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5241
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3889
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3481
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2765
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.