Question Dynamically Saving my data from dynamically created controls.

Joined
Mar 6, 2013
Messages
5
Programming Experience
1-3
Hi All


I am new to programming. I have created dynamic addition of controls(textbox, combobox) to my form. Now i have to save the data in the dynamically created controls in ms sql database. Please tell me how to assign it some variable or how to save it to my MS SQL Server.


Thanks in Advance
Luxme
 
Why exactly have you added controls at run time? There are legitimate reasons to do so but it sounds suspiciously like that's not the best option in your case. I'm guessing that you have created multiple sets of controls with one for each record. Not a good idea. There are three ways that that is usually handled:

1. Use one set of controls and view one record at a time. As you navigate through the data you show a different record in the controls each time.
2. Use a DataGridView. It can display all the data at the same time and each row in the grid is a effectively a set of controls to display and edit one record.
3. Use a DataRepeater. You create a template with a set of controls to handle one record and the repeater control handles creating multiple instances of it for multiple records.

In each case, you simply query your database an populate a DataTable and then save the changes from that DataTable back to the database using the same data adapter. In between, you bind the DataTable to which every control(s) you're using and then you need no code at all to manage loading the data from the table into the controls or getting the user input back into the table. Data-binding handles the flow of data in both directions.
 
Thanks a lot. I did not know that I had other options. I will try with Datagrid View or DataRepeater.
Thank u very much. I will update how it works after I am done.
 
Hi,
I have to get inputs from user. I don't know the no of inputs. The no is based on production. It may differ from 1 to 5. So is it possible to use DataGridView or DataRepeater. Suggest some examples of how to use it and which is best to use. ( Once the no of inputs is decided, then the user has to enter 22 records for each set. )

Thanks in Advance
Luxme
 
One option would be to create a DataTable with the appropriate number of columns and rows and then bind it to a DataGridView. If you set to False the properties that all the user to add and delete rows in the grid then they can't change the number of rows. Once they've entered all the data you can validate as required and then save from the DataTable.
 
It's a control. You add it like any other control. As for how to configure it, the first thing to do would be to read the class documentation. I've never used it myself but, if I was going to, that's what I would do and then, if I needed to, I'd look for examples online. You're just as capable of doing that as I am. You may not be as capable as I am at understanding what you find, but that doesn't mean that you shouldn't try. At least if you do that then you'll be able to ask specific questions.
 
Back
Top