I need to store generic list in viewstate so that it may be available to use after Postback. Codes are are as follows:
private List<guardian> guardianlist(List<guardian> value, string project, string billtype)
{
string head = null, relationstring = null;
SqlCommand cmd1 = new SqlCommand("select accounthead from accountrelation where project = '" + project + "' AND relationfor ='" + billtype + "' and foraccount ='RATE'", agr);
SqlDataReader dr1 = cmd1.ExecuteReader();
while (dr1.Read())
{
SqlCommand cmd = new SqlCommand("select accounthead, relation from accountrelation where project = '" + project + "' AND relationfor ='" + billtype + "' and foraccount ='RATE' ", agr);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
head = dr[0].ToString().Trim();
relationstring = dr[1].ToString().Trim();
string[] multirelationstring = relationstring.Split(new Char[] { '*', '/', '+', '-', '(', ')' });
List<string> buff = new List<string>(multirelationstring);
for (int i = 0; i < multirelationstring.Length; i++)
{
try
{
decimal x = decimal.Parse(multirelationstring[i]);
buff.Remove(multirelationstring[i]);
}
catch
{
if (multirelationstring[i] == "")
{
buff.Remove(multirelationstring[i]);
//multirelationstring = buff.ToArray();
}
}
}
//var v127 =((buff.Where(a => value.All(b => (b.Rate==billtype) && (a != b.Head)))).ToArray()).Length;
if (buff.Where(a => value.All(b => b.Head != a)).Count() == 0)
{
if (((value.Where(a => a.Head == head)).ToArray()).Length == 0)
{
guardian gt = new guardian();
gt.Head = head;
for (int i = 0; i < buff.Count; i++)
{
relationstring = relationstring.Replace(buff[i],value[value.FindIndex(a => a.Head== buff[i])].Relation);
}
gt.Relation = relationstring;
value.Add(gt);
}
}
}
dr.Dispose();
}
dr1.Dispose();
return value;
}
public class guardian
{
public string Head { get; set; }
public string Relation { get; set; }
}
it does not throw any exception but my "dropdown13" remain blank whereas if I remove "ViewState["listitem1"] = listitem1;" it working fine.
protected void DropDownList16_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList13.ClearSelection();
DropDownList13.Items.Clear();
DropDownList13_SelectedIndexChanged(null, null);
if (DropDownList16.SelectedIndex > 0)
{
List<guardian> listitem1 = new List<guardian>();
if (!DropDownList13.Items.Contains(ListItem.FromString("Select")))
{
DropDownList13.Items.Add("Select");
}
SqlCommand cmd = new SqlCommand("select DISTINCT(accounthead) from accounthead where foraccount='PAYMENT' and not accounthead in (select accounthead from accountrelation where project = '"+DropDownList15.SelectedItem.ToString()+"' and relationfor ='"+DropDownList16.SelectedItem.ToString()+"' and foraccount ='PAYMENT')", agr);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
DropDownList13.Items.Add(dr[0].ToString().Trim());
}
dr.Dispose();
guardian gt = new guardian();
gt.Head = "tsamt";
gt.Relation = "100";
listitem1.Add(gt);
listitem1 = guardianlist(listitem1, DropDownList15.SelectedItem.ToString(), DropDownList16.SelectedItem.ToString().Replace("AMOUNT", "RATE"));
ViewState["listitem1"] = listitem1;
}
}
and I retrieve this list from viewstate in dropdownselectindexchanged and some other places.
My reading says that ViewState doesnt support generics, If you absolutely must store those items then use the non generic ArrayLst, you will have to cast to the right type when reading back
Alternately serialize to json and put that string in viewstate