Web Form/SQL Table Structure

S

sck10

Hello,

I am building a web form that will be used to gather information for
marketing plans. The form will have 15 questions which must be answered.
Each question can have large blocks of text. The issue is that the user
will go to the web to enter and update their information over time, so all
the questions need to be on one form. I believe the proper structure for
the table would be:

MarketingRequest
----------------
MktRequest_ID
MktQuestion
MktAnswer


However, since this needs to be on a FormView, should I use the following:

MarketingRequest
----------------
MktRequest_ID
MktQuestion01
MktAnswer01
MktQuestion02
MktAnswer02
MktQuestion03
MktAnswer03
....

The database guys said to use the first table structure, but I can't figure
out how to do this and have all the questions on the FormView. Any
assistance with this would be appreciated.

Thanks, sck10
 
B

Bruno Alexandre

if it's only 15 questions and for a long time

if it was me I go for the 2nd one

if you want to add/remove/modify questions over time then you should come up
with the 1st...

[tblQuestions]
idQuestion [numeric, key]
tQuestion [nvarchar(300)]
tActive [bit] - show this question on the board?
tCreateUser [numeric] - id from the user who created the question
tCreateDate [datetime] - date and time for the creaion
tUpdateUser [numeric] - id from the user who updated
tUpdateDate [datetime] - date and time for the update

[tblMarketingRequest]
idMRequest [numeric, key]
idQuestion [numeric, relationship with [tblQuestions].idQuestion]
tAnswer [text]
tInsertUser [numeric] - id from the user who inserted
tInsertDate [datetime] - date and time for the inserting


to present it in a FormView all you need to do is

"SELECT tQuestion as Question, idQuestion as idQ FROM tblQuestions WHERE
tActive = 1"

when your saving the answers all you need to do is:

"INSERT INTO tblMarketingRequest (idQuestion, tAnswer, tInsertDate,
tInsertUser)
VALUES ( @idQ, @ans, convert(datetime, getdate(), 103 ), @idU )"

if you need any more help, fell free to contact me over email
 
S

Steven Cheng[MSFT]

Hello Steve,

As for database table structure, I also think the first one should be more
reasonable.

In the last message, you said that

=============
However, since this needs to be on a web form, should I use the following:
=============

do you mean you want to display all the questions on the single web page?
If so, there're many means to do this:

1. Use databound template control such as DataList, Repeater to display the
questions through a DataReader or DataSet query from database.

2. Programmtically use code to construct a Table and add rows to display
each questions from Datareader or DataSet

Also, if you're using template databound control(datalist or repeater), it
also easy to submit the page and query new value in each template control
item and update them into backend database. How do you think of this?

Please feel free to let me know if there is any particular concerns here or
anything else you wonder.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

sck10

Thanks Bruno


Bruno Alexandre said:
if it's only 15 questions and for a long time

if it was me I go for the 2nd one

if you want to add/remove/modify questions over time then you should come
up with the 1st...

[tblQuestions]
idQuestion [numeric, key]
tQuestion [nvarchar(300)]
tActive [bit] - show this question on the board?
tCreateUser [numeric] - id from the user who created the question
tCreateDate [datetime] - date and time for the creaion
tUpdateUser [numeric] - id from the user who updated
tUpdateDate [datetime] - date and time for the update

[tblMarketingRequest]
idMRequest [numeric, key]
idQuestion [numeric, relationship with [tblQuestions].idQuestion]
tAnswer [text]
tInsertUser [numeric] - id from the user who inserted
tInsertDate [datetime] - date and time for the inserting


to present it in a FormView all you need to do is

"SELECT tQuestion as Question, idQuestion as idQ FROM tblQuestions WHERE
tActive = 1"

when your saving the answers all you need to do is:

"INSERT INTO tblMarketingRequest (idQuestion, tAnswer, tInsertDate,
tInsertUser)
VALUES ( @idQ, @ans, convert(datetime, getdate(), 103 ), @idU )"

if you need any more help, fell free to contact me over email

--

Bruno Alexandre
"a Portuguese in København, Danmark"



sck10 said:
Hello,

I am building a web form that will be used to gather information for
marketing plans. The form will have 15 questions which must be answered.
Each question can have large blocks of text. The issue is that the user
will go to the web to enter and update their information over time, so
all the questions need to be on one form. I believe the proper structure
for the table would be:

MarketingRequest
----------------
MktRequest_ID
MktQuestion
MktAnswer


However, since this needs to be on a FormView, should I use the
following:

MarketingRequest
----------------
MktRequest_ID
MktQuestion01
MktAnswer01
MktQuestion02
MktAnswer02
MktQuestion03
MktAnswer03
...

The database guys said to use the first table structure, but I can't
figure out how to do this and have all the questions on the FormView.
Any
assistance with this would be appreciated.

Thanks, sck10
 
S

sck10

Hi Steven,

I Have a couple of question about using the first table structure. The main
issue is that the user needs to be able to update their information over
time.

Lets say I have 3 questions (to make this short) that need to be answered,
each of a different control type:
Question 01: DropDownList
Question 02: MultiLine Text Box
Question 03: RadioButtonList

So the Repeater tool output would look something like this:

1. What Region are you operating in?
DropDownList for regions

2. What is the ops plan and revenue projection for this region?
MultiLine TextBox

3. Is the product available today?
0 yes 0 no

So using the Repeater Tool,
1. How would I call the correct control for the question (DropDownList,
RadioButtonList, TextBox)?
2. How do I name the controls on the fly?
3. How and when would I populate the DropDownList with values from a
stored procedure since I don't know what the DDL name is for binding?
4. When do you do the Binding for DropDownList and RadioButtonList
controls

Anyway, thanks for your help...



MarketingRequest
----------------
MktRequest_ID
MktQuestion
MktAnswer


MarketingRequest
----------------
MktRequest_ID
MktQuestion01
MktAnswer01
MktQuestion02
MktAnswer02
MktQuestion03
MktAnswer03
 
S

Steven Cheng[MSFT]

Hi Steve,

Thanks for the followup. If you're using the first table design. Then, we
can consider using a Repeater(or other template databound control) to bind
to a list of all the Main questions(select distinct questinoID from the
table). And also prepare a DataTable which queried all the records from the
Question Table.

In the repeater or DataList, we can use its "ItemDataBound" event to hook
into each items' databinding, and there, we can query the sub
questions(answsers) according to the main questionID from the DataTable I
mentioned before.

And as for the DropDownList, TextBox or ListBox... You can add an
additional field int the table to tell what type of control should be used
to display the sub questions. And in the "ItemDataBound" event, we can
construct the certain type of control dynamically and bind data with it.

Currently I haven't the detailed table structure or test data on hande, if
convenient, you can send me a simple database file and how you want to
display it and I can help create a small demo against the things I
mentioned above.

If you still have interests on this idea, please feel free to let me know.


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

sck10

Hi Steven,

Below is the code to create a table in SQL Server and populate it with 3
questions and their respective control. What I would like to do is populate
the repeater control with the questions and then generate the control.

Also, I need to be able to:
populate the DropDownList and RadioButtonList
Reference the values with a loop for inserting

If you are updating records, how would you set the DropDownList and
RadioButtonList values to the current values?

Thanks again, sck10


GO
/****** Object: Table [dbo].[MyTies] Script Date: 08/26/2006 21:37:17
******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[StockPicker](
[StockPicker_ID] [int] IDENTITY(1,1) NOT NULL,
[Question] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[ControlType] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
CONSTRAINT [PK_StockPicker] PRIMARY KEY CLUSTERED
(
[StockPicker_ID] ASC
) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF



INSERT INTO StockPicker (Question, ControlType)
VALUES ('Stock Name', 'TextBox')

INSERT INTO StockPicker (Question, ControlType)
VALUES ('Industry Type', 'DropDownList')

INSERT INTO StockPicker (Question, ControlType)
VALUES ('Overvalued', 'RadioButtonList')
 
S

Steven Cheng[MSFT]

Hi Steve,

After some research against the table data and the requirement here, I
found that it is quite hard to do all the things dynamically, especially
for the different kind of controls. Here you've given me three
ControlTypes, they're TextBox, DropDownList and RadioButtonList, how many
ones will you have in the real project? This will determine whether my
solution here is workable(it won't quite work if there're many different
kinds of controls).

Currently, in template databound control, such as Repeater or DataList... ,
we can dynamically modify and customize some existing controls in the
ItemTemplate during databinding period, however, we can not dynamically
create and add new controls at that time. This is because all the dynamic
controls added in databinding will lost in sequential postback. One way
to resolve this is predefined all the possible controls in the template and
dynamically choose to show and hide the proper one.

Also, for updating, we can loop through all the controls in the template
databound control's item collection and get reference to those sub controls
in each item and pickup the updated value from them. Here is a simple test
page demonstrate the funcionality (use the test database data you provided
in the last message):

==============aspx ========================
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:CLRTestDBConnectionString %>"
SelectCommand="SELECT [StockPicker_ID], [Question],
[ControlType] FROM [StockPicker]">
</asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Repeater ID="Repeater1" runat="server"
DataSourceID="SqlDataSource1" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<br /><hr /><br />
<table
style="width:100%;border-style:solid;border-width:2pt;border-color:Black"
cellpadding="0" cellspacing="0">
<tr>
<td>
Question: <asp:Label ID="lblQuestion" runat="server"
Text='<%# Eval("Question")%>'></asp:Label><br />
<asp:TextBox ID="txt" runat="server"></asp:TextBox>
<asp:DropDownList ID="lst" runat="server">
</asp:DropDownList>
<asp:RadioButtonList ID="rlst" runat="server">
</asp:RadioButtonList>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
<br />
<br />
<asp:Button ID="btnUpdate" runat="server" Text="Submit Update"
OnClick="btnUpdate_Click" /></div>
</form>
</body>
</html>

==========code behind====================
public partial class dataaccess_RepeaterPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Repeater1_ItemDataBound(object sender,
RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
PlaceHolder holder = e.Item.FindControl("phItem") as
PlaceHolder;

string ctype = DataBinder.Eval(e.Item.DataItem, "ControlType")
as string;
int qid = (int)DataBinder.Eval(e.Item.DataItem,
"StockPicker_ID");

Label lbl = e.Item.FindControl("lblQuestion") as Label;
TextBox txt = e.Item.FindControl("txt") as TextBox;
DropDownList lst = e.Item.FindControl("lst") as DropDownList;
RadioButtonList rlst = e.Item.FindControl("rlst") as
RadioButtonList;


lbl.Attributes["qid"] = qid.ToString();
lbl.Attributes["ctype"] = ctype;

switch (ctype)
{
case "TextBox":

txt.Text = "Questions...........";
txt.Visible = true;
lst.Visible = false;
rlst.Visible = false;

break;
case "DropDownList":

lst.Items.Add("item1.......");
lst.Items.Add("item2.......");
lst.Items.Add("item3.......");

txt.Visible = false;
lst.Visible = true;
rlst.Visible = false;

break;
case "RadioButtonList":

rlst.Items.Add("item1.........");
rlst.Items.Add("item2.........");
rlst.Items.Add("item3.........");

txt.Visible = false;
lst.Visible = false;
rlst.Visible = true;

break;
}
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in Repeater1.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType ==
ListItemType.AlternatingItem)
{
Label lbl = item.FindControl("lblQuestion") as Label;

int qid = int.Parse(lbl.Attributes["qid"]);
string ctype = lbl.Attributes["ctype"];


string newvalue = null;

switch (ctype)
{
case "TextBox":

TextBox txt = item.FindControl("txt") as TextBox;
newvalue = txt.Text;

break;
case "DropDownList":

DropDownList lst = item.FindControl("lst") as
DropDownList;
newvalue = lst.SelectedValue;

break;
case "RadioButtonList":

RadioButtonList rlst = item.FindControl("rlst") as
RadioButtonList;
newvalue = rlst.SelectedValue;

break;
}


Response.Write("<br/>questionID: "+ qid + ", new value: " +
newvalue);
}
}
}
}
======================================


Please feel free to let me know if you have anything unclear or any other
ideas on this.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

sck10

Thanks Steven, appreciate the help on this. This should get me started.


Steven Cheng said:
Hi Steve,

After some research against the table data and the requirement here, I
found that it is quite hard to do all the things dynamically, especially
for the different kind of controls. Here you've given me three
ControlTypes, they're TextBox, DropDownList and RadioButtonList, how
many
ones will you have in the real project? This will determine whether my
solution here is workable(it won't quite work if there're many different
kinds of controls).

Currently, in template databound control, such as Repeater or DataList...
,
we can dynamically modify and customize some existing controls in the
ItemTemplate during databinding period, however, we can not dynamically
create and add new controls at that time. This is because all the dynamic
controls added in databinding will lost in sequential postback. One way
to resolve this is predefined all the possible controls in the template
and
dynamically choose to show and hide the proper one.

Also, for updating, we can loop through all the controls in the template
databound control's item collection and get reference to those sub
controls
in each item and pickup the updated value from them. Here is a simple
test
page demonstrate the funcionality (use the test database data you provided
in the last message):

==============aspx ========================
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:CLRTestDBConnectionString %>"
SelectCommand="SELECT [StockPicker_ID], [Question],
[ControlType] FROM [StockPicker]">
</asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Repeater ID="Repeater1" runat="server"
DataSourceID="SqlDataSource1" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<br /><hr /><br />
<table
style="width:100%;border-style:solid;border-width:2pt;border-color:Black"
cellpadding="0" cellspacing="0">
<tr>
<td>
Question: <asp:Label ID="lblQuestion" runat="server"
Text='<%# Eval("Question")%>'></asp:Label><br />
<asp:TextBox ID="txt" runat="server"></asp:TextBox>
<asp:DropDownList ID="lst" runat="server">
</asp:DropDownList>
<asp:RadioButtonList ID="rlst" runat="server">
</asp:RadioButtonList>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
<br />
<br />
<asp:Button ID="btnUpdate" runat="server" Text="Submit Update"
OnClick="btnUpdate_Click" /></div>
</form>
</body>
</html>

==========code behind====================
public partial class dataaccess_RepeaterPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Repeater1_ItemDataBound(object sender,
RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
PlaceHolder holder = e.Item.FindControl("phItem") as
PlaceHolder;

string ctype = DataBinder.Eval(e.Item.DataItem, "ControlType")
as string;
int qid = (int)DataBinder.Eval(e.Item.DataItem,
"StockPicker_ID");

Label lbl = e.Item.FindControl("lblQuestion") as Label;
TextBox txt = e.Item.FindControl("txt") as TextBox;
DropDownList lst = e.Item.FindControl("lst") as DropDownList;
RadioButtonList rlst = e.Item.FindControl("rlst") as
RadioButtonList;


lbl.Attributes["qid"] = qid.ToString();
lbl.Attributes["ctype"] = ctype;

switch (ctype)
{
case "TextBox":

txt.Text = "Questions...........";
txt.Visible = true;
lst.Visible = false;
rlst.Visible = false;

break;
case "DropDownList":

lst.Items.Add("item1.......");
lst.Items.Add("item2.......");
lst.Items.Add("item3.......");

txt.Visible = false;
lst.Visible = true;
rlst.Visible = false;

break;
case "RadioButtonList":

rlst.Items.Add("item1.........");
rlst.Items.Add("item2.........");
rlst.Items.Add("item3.........");

txt.Visible = false;
lst.Visible = false;
rlst.Visible = true;

break;
}
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in Repeater1.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType ==
ListItemType.AlternatingItem)
{
Label lbl = item.FindControl("lblQuestion") as Label;

int qid = int.Parse(lbl.Attributes["qid"]);
string ctype = lbl.Attributes["ctype"];


string newvalue = null;

switch (ctype)
{
case "TextBox":

TextBox txt = item.FindControl("txt") as TextBox;
newvalue = txt.Text;

break;
case "DropDownList":

DropDownList lst = item.FindControl("lst") as
DropDownList;
newvalue = lst.SelectedValue;

break;
case "RadioButtonList":

RadioButtonList rlst = item.FindControl("rlst") as
RadioButtonList;
newvalue = rlst.SelectedValue;

break;
}


Response.Write("<br/>questionID: "+ qid + ", new value: " +
newvalue);
}
}
}
}
======================================


Please feel free to let me know if you have anything unclear or any other
ideas on this.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no
rights.
 
S

Steven Cheng[MSFT]

Hi Steve,

I'm glad that the code is of assistance. If you meet any further problem,
please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top