Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
Hi,
I have multiple column in the datatable.i want second coulmn value and store into viewstate or hidden filed. how to store one column data in a view state.

my code is
EX--
C#
 DataTable _dtblanck = GetLatestDataFromGrid();

_dtblanck = obj.madeAddTable(_dtblanck, Session["LoginName"].ToString());
 ViewState["dourceofgoods"] = _dtblanck;


Here , i my code datatable is saving with all columns. i need to save only one column data in view state.

suppose this three couln is comming from datatable. i need to store only employeename column data into viewstate or hidden filed. how to store one column data.

Emplid Employeename age

1 Raj 25
2 ram 35
3 Rasmi 20
Posted
Updated 14-Jan-14 21:05pm
v4
Comments
Ankur\m/ 15-Jan-14 2:30am    
A column may have many rows. Do you want a particular row's value or all rows in the column?
kamalsekhar 15-Jan-14 3:06am    
i want all row data from that particular column
Ankur\m/ 16-Jan-14 1:04am    
You will store it in comma separated values or something like that if you are storing it in a hidden field as you can only save string in it. For Viewstate you can create a string array and store it. But make sure whatever you are storing isn't very large as it will impact the performance of your site.
Prasaad SJ 15-Jan-14 4:16am    
May I know What r you doing with DataTable.. ? You r displaying the Table in Grid View.. Or using only for the logical opertions..

try like this..

C#
HiddenFieldControlName.Value=  dt.Rows[0]["columnName"].ToString();
 
Share this answer
 
Comments
kamalsekhar 15-Jan-14 2:33am    
Hi Karthik,
I need to store all data which are comming from datatable of particular coulmn . not a single row data.
Karthik_Mahalingam 15-Jan-14 2:50am    
then you can store in an array
Karthik_Mahalingam 15-Jan-14 2:51am    
ViewState["somekey"]= dt.Rows.OfType<datarow>().Select(K=> K["columnName"].ToString()).ToArray();
try this..
kamalsekhar 15-Jan-14 3:04am    
DataTable _dtblanck = GetLatestDataFromGrid();

_dtblanck = obj.MadeAddTable(_dtblanck, Session["LoginName"].ToString());
ViewState["dourceofgoods"] = _dtblanck.Rows.OfType().Select(K => K["mio_source"].ToString()).ToArray();

Here error is giving last line
Karthik_Mahalingam 15-Jan-14 3:15am    
whar error ?
make sure the column name is "mio_source"
Try this

C#
string[] arrayVariable= _dtblanck.AsEnumerable().Select(s => s.Field<string>("LoginName")).ToArray<string>();


Now you have all values of your column in an array do whatever you want to do with it.
 
Share this answer
 
v2
Comments
kamalsekhar 15-Jan-14 3:03am    
How to store this string array value into a hidden filed or view state
Hammad 15-Jan-14 3:12am    
Probably a few methods would work.

1) Serialize the string[] in JSON

This would be fairly easy in .NET using the JavaScriptSerializer class, and avoid issues with delimiter characters. Something like:

string json = new JavaScriptSerializer().Serialize(arrayVariable);

now store it in hidden field like
HiddenFieldControlName.Value=json;

2) Come up with a delimiter that never appears in the strings

Delimit each string with a character such as ||| that will never appear in the string. You can use String.Join() to build this string. Something like:

string str = String.Join("|||", arrayVariable);
HiddenFieldControlName.Value=str;

And then rebuild it like:

string str=HiddenFieldControlName.Value.ToString();
myValues = str.Split(new string[] { "|||" }, StringSplitOptions.RemoveEmptyEntries);

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