Entering values into the database from "unlimited" dynamic controls
There the dropdown displays 1-10 from where the user selects the number of text fields he wants - if he selects unlimited, it shows him a text field where he enters the exact number of text fields he would like. if it comes in at 40 it shows it 40 text boxes which are created at runtime.
My question is, how can I enter data from 40 - or "no matter how many it is entering" text fields into MS SQL database. There is no way to create a dynamic column name and it would be too tedious and messy. Is there a way to do this?
a source to share
You have a one-to-many relationship between what is on this page and comments or descriptions or whatever. You shouldn't have this modeling in your database like 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
After that, you can use code similar to what Maine wrote to handle things on the front. You just pass the index of the textbox as comment_number.
a source to share
When you create text fields, you can provide them with sequential IDs and then iterate over them, writing the values to your database.
For instance:
/// <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);
}
]
a source to share
Have you considered storing data as an XML field in a database?
If you have 40+ text fields, you can just have a simple schema to store them, like this:
<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 save the schematic somewhere. This is a good way to store dynamic data that can change from record to record.
a source to share