views:

525

answers:

4

there's a dropdown displaying 1-10 from where the user selects the number of textboxes he wants - if he chooses unlimited, it shows him a textbox wherein he enters the exact number of textboxes he'd like. if he enters 40, it shows him 40 textboxes which are created at runtime.

my question is, how do i enter the data from 40 - or 'whatever number he enters' textboxes into the MS SQL database. there's no way i can create a column name dynamically, and that'd be too tedious and messy. is there any way i can do this?

+2  A: 

When you create the textboxes, you can give them sequential ID's, and then iterate through these on the postback, writing the values to your database.

For example:

/// <Summary>
///  This would be fired when the user enters the number of textboxes
///  the want and click the 'Create Textboxes' button.
/// </Summary>
protected void CreateTextBoxes_Click(object sender, EventArgs e)
{
    // Check how many textboxes the user wants
    int count = Convert.ToInt32(CountTextBox.Text);

    // Generate the number of textboxes requested
    // and add them to the page
    for (int i=0; i < count; i++)
    {
        // Create the textbox
        TextBox textbox = new Textbox();

        // Set the ID so we can access it later
        textbox.ID = "TextBox_" + i;

        // Add the textbox to the panel
        TextBoxPanel.Controls.Add(textbox);
    }
}

/// <Summary>
///  This would be fired when the user has entered values in the textboxes
///  and clicked the Save button to save the values to the database.
/// </Summary>
protected void SaveButton_Click(object sender, EventArgs e)
{
    // Check how many textboxes the user created
    int count = Convert.ToInt32(CountTextBox.Text);

    // Loop through them
    for (int i=0; i < count; i++)
    {
         // Get the TextBox
         TextBox textbox = (TextBox) FindControl("TextBox_" + i);

         // Get the value
         string val = textbox.Text;

         // Insert into DB
         SaveValueToDatabase(val);
    }
]
Mun
+1  A: 

What you have is a 1-to-many relationship between whatever it is that is on that page and the comments or descriptions or whatever. You shouldn't have this modeled in your database as text_box_1, text_box_2, etc. Instead, it should be:

CREATE TABLE Some_Entity
(
    some_entity_id INT NOT NULL,
    CONSTRAINT PK_Some_Entity PRIMARY KEY CLUSTERED (some_entity_id)
)
GO

CREATE TABLE Some_Entity_Comments
(
    some_entity_id INT  NOT NULL,
    comment_number INT  NOT NULL,
    comments  VARCHAR(1000) NOT NULL,
    CONSTRAINT PK_Some_Entity_Comments
       PRIMARY KEY CLUSTERED (some_entity_id, comment_number),
    CONSTRAINT FK_Some_Entity_Comments_Some_Entity
       FOREIGN KEY (some_entity_id) REFERENCES Some_Entity (some_entity_id)
)
GO

Once that's done, you can use code similar to what Mun wrote to handle things on the front end. You'll just pass the text box index as the comment_number.

Tom H.
Thank you Mun and Tom H. Appreciated.
fuz3d
+1  A: 

Have you considered storing the data as an XML field in the database?

If you have 40+ text boxes you could just have a simple schema to store them like:

<YourData>
    <TextBox ID="TextBox1">Value1</TextBox>
    <TextBox ID="TextBox2">Value2</TextBox>
    <TextBox ID="TextBox3">Value3</TextBox>
    ... etc ...
</YourData>

No need for dynamic fields in your DB. You should also store the schema somewhere as well. This is a good way to store dynamic data that can change from record to record.

Kelsey
This is in violation of database normalization. Sometimes that's ok, but i'm not convinced this is a good example of that. Suppose you wanted to search for a textbox that had "Value3". You would have to do a table scan with an expensive LIKE "%Value3%" type where clause. On any but the smallest of datasets this is prohibitive.
TokenMacGuy
I am not sure what database engine that is being used and I am no SQL guru but SQL Server fully supports XML fields and they can be queried directly without the need for what you explained above.
Kelsey
kelsey, your suggestion of using XML schema is good, but i haven't worked on this before, so i've no idea on how to go about storing the data in an XML field.
fuz3d
I am not a db guy so my knowledge on the specifics of it is limited. You can start by looking at some MSDN articles such as:http://msdn.microsoft.com/en-us/library/ms189887.aspx
Kelsey
A: 

Mun, it's giving me the error of Object Reference Not Set at this line:

// Get the value
 string val = textbox.Text;

seems like it cannot find the control of the textbox.

fuz3d