Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I have problem to save multiple fields(ex:-TextBox,RadioButton,CheckBox) in one column of DataTable.

OR shall i use another DataTable.?

code is as below.
C#
if (ViewState["CurrentTable"] != null)
            {
                DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                DataRow drCurrentRow = null;
                if (dtCurrentTable.Rows.Count > 0)
                {
                    for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                    {
                        TextBox TextBoxName = (TextBox)grvFbuilder.Rows[rowIndex].Cells[1].FindControl("tbFid");
                        TextBox TextBoxAge = (TextBox)grvFbuilder.Rows[rowIndex].Cells[2].FindControl("tbFEname");
                        TextBox TextBoxAddress = (TextBox)grvFbuilder.Rows[rowIndex].Cells[3].FindControl("tbFAname");

                        DropDownList ddlTables = (DropDownList)grvFbuilder.Rows[rowIndex].Cells[4].FindControl("ddlTables");
                        DropDownList ddlColumns = (DropDownList)grvFbuilder.Rows[rowIndex].Cells[4].FindControl("ddlColumns");
                        DropDownList ddlShowFields = (DropDownList)grvFbuilder.Rows[rowIndex].Cells[4].FindControl("ddlShowFields");
                        TextBox TextDdlFields = (TextBox)grvFbuilder.Rows[rowIndex].Cells[4].FindControl("tbDdlFields");

                        //RadioButtonList RBLGender = (RadioButtonList)grvFbuilder.Rows[rowIndex].Cells[4].FindControl("RBLGender");
                        DropDownList ddlFType = (DropDownList)grvFbuilder.Rows[rowIndex].Cells[5].FindControl("ddlFType");
                        drCurrentRow = dtCurrentTable.NewRow();
                        drCurrentRow["RowNumber"] = i + 1;

                        dtCurrentTable.Rows[i - 1]["Col1"] = TextBoxName.Text;
                        dtCurrentTable.Rows[i - 1]["Col2"] = TextBoxAge.Text;
                        dtCurrentTable.Rows[i - 1]["Col3"] = TextBoxAddress.Text;
                        //dtCurrentTable.Rows[i - 1]["Col4"] = RBLGender.SelectedValue;

                        dtCurrentTable.Rows[i - 1]["Col4"] = ddlTables.SelectedItem.Text;
                        dtCurrentTable.Rows[i - 1]["Col4"] = ddlColumns.SelectedItem.Text;
                        dtCurrentTable.Rows[i - 1]["Col4"] = ddlShowFields.SelectedValue;
                        dtCurrentTable.Rows[i - 1]["Col4"] = TextDdlFields.Text;

                        dtCurrentTable.Rows[i - 1]["Col5"] = ddlFType.SelectedValue;
                        //BindDdlTables(rowIndex);
                        rowIndex++;
                    }
                    dtCurrentTable.Rows.Add(drCurrentRow);
                    ViewState["CurrentTable"] = dtCurrentTable;

                    grvFbuilder.DataSource = dtCurrentTable;
                    grvFbuilder.DataBind();

                    TextBox txn = (TextBox)grvFbuilder.Rows[rowIndex].Cells[1].FindControl("tbFid");
                    txn.Focus();

                    
                    // txn.Focus;
                }
            }
Posted
Updated 9-Jun-15 1:24am
v3
Comments
Michael_Davies 9-Jun-15 8:26am    
Why? Add a column for each field..

If you must then concatenate the fields (JSON, XML, a method of your own) to store and de-concatenate when you read them back but you will have to handle the data manually, plus it will require more storage that separate columns.

You are not reasoning why one should put multiply values in one cell, but even without that it is clear that this code will not do it...
C#
dtCurrentTable.Rows[i - 1]["Col4"] = ddlTables.SelectedItem.Text;
dtCurrentTable.Rows[i - 1]["Col4"] = ddlColumns.SelectedItem.Text;
dtCurrentTable.Rows[i - 1]["Col4"] = ddlShowFields.SelectedValue;
dtCurrentTable.Rows[i - 1]["Col4"] = TextDdlFields.Text;

As a developer you have to understand that these four lines are updating the very same cell, so the final value will be the result of the last line of the four...
To concatenate values you may do something like this:
C#
dtCurrentTable.Rows[i - 1]["Col4"] = ddlTables.SelectedItem.Text;
dtCurrentTable.Rows[i - 1]["Col4"] += ddlColumns.SelectedItem.Text;
dtCurrentTable.Rows[i - 1]["Col4"] += ddlShowFields.SelectedValue;
dtCurrentTable.Rows[i - 1]["Col4"] += TextDdlFields.Text;

It is obvious that this approach totally unacceptable as the values are mixed without any possible way to retrieve them...
And now we come to the why! Why?! There is no reason to do such a thing you asked for...
1. If the data for display, than you are creating a pre-defined look-and-feel that can not be changed according to the medium on which it displayed
2. If the data for computation, than the concatenated data can not be used anymore as it lost it's original meaning of the part...
It is a pure case of the 'The whole is greater much less than the sum of its parts'...
 
Share this answer
 
Comments
The Doer 9-Jun-15 23:05pm    
Isn't it be useful if they delimit it by delimiter?
Kornfeld Eliyahu Peter 10-Jun-15 2:29am    
Absolutely not...
If you want to access (read/write) for any reason ONE of those data you will have to split/join the ALL values - a total waste of time...
SQL was designed to hold tabular data, and it is very good at that! Use it as intended to get the most out of it!
The Doer 15-Jun-15 4:15am    
I agree it not useful to store all values in a single column . But as per the question if suppose one has to save in single column then they must delimit.

These types of questions might ask in an interview to test the reasoning of one to solve this by hook or by crook
what you want to store all the controls value in a single column?

Preferred way is to store in individual column .

Still if this is your custom try then you can add it easily by separating each by a delimiter-

dtCurrentTable.Rows[i - 1]["Col1"] = TextBoxName.Text + ";" + ddlTables.SelectedItem.Text + ";" + ddlShowFields.SelectedValue ;

Hope it helps.
 
Share this answer
 
Comments
Merajuddin Ansari 10-Jun-15 4:05am    
thanks it helped me.
Kornfeld Eliyahu Peter 10-Jun-15 5:37am    
I do not care too much for your solution, but let me warn you a last time (and I have almost 20 years of experience in DB design/use):
Do not do it!!!
Kornfeld Eliyahu Peter 10-Jun-15 5:43am    
You missed me totally...
I do not care for you accepting my answer or not, not even a bit, but the proposal in this solution and your acceptance of it is what bothering me...
This kind of solution is just wrong...
Merajuddin Ansari 10-Jun-15 5:50am    
i want to say sorry for my mistake.
Merajuddin Ansari 10-Jun-15 5:44am    
by mistake i rejected peter.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900