im trying to send values fetched from the database through the data contract. since its a list iv defined a list in data contract. but im getting the error mentioned. the data contract is as follows:
[DataContract]
public class a
{
[DataMember]
public string SessionId { get; set; }
[DataMember]
public string StartDate { get; set; }
[DataMember]
public string EndDate { get; set; }
[DataMember]
public string p1 { get; set; }
[DataMember]
public string p2 { get; set; }
[DataMember]
public string p3 { get; set; }
[DataMember]
public string p4 { get; set; }
[DataMember]
public string p5 { get; set; }
[DataMember]
public string p6 { get; set; }
}
[DataContract]
public class b : ReturnValuesBase
{
[DataMember]
public List<a> abc;
}
in my datalayer the function is defined as follows:
public b Search(string SessionId, string StartDate, string EndDate, string a, string b)
{
b abc = new b();
try
{
SqlConnection CnStr = new SqlConnection();
CnStr = new SqlConnection(ConnString);
CnStr.Open();
SqlCommand cmd = new SqlCommand("[dbo].[abc]", CnStr);
SqlParameter prmSQL = new SqlParameter();
cmd.CommandType = CommandType.StoredProcedure;
DateTime SDate = Convert.ToDateTime(StartDate);
DateTime EDate = Convert.ToDateTime(EndDate);
prmSQL = cmd.Parameters.Add(new SqlParameter("@a", SqlDbType.VarChar, 50));
if (CriticalOptions == "")
{
prmSQL.Value = DBNull.Value;
}
else
{
prmSQL.Value = a;
}
prmSQL = cmd.Parameters.Add(new SqlParameter("@StartDate", SqlDbType.DateTime));
prmSQL.Value = SDate;
prmSQL = cmd.Parameters.Add(new SqlParameter("@EndDate", SqlDbType.DateTime));
prmSQL.Value = EDate;
prmSQL = cmd.Parameters.Add(new SqlParameter("@Ma", SqlDbType.VarChar,50));
prmSQL.Value = a;
SqlDataReader DataReader = cmd.ExecuteReader();
DataSet DS = new DataSet();
SqlDataAdapter DataAdapter = new SqlDataAdapter();
DataAdapter.SelectCommand = cmd;
CnStr.Close();
DataAdapter.Fill(DS);
for (int i = 0; i < DS.Tables[1].Rows.Count; i++)
{
DemoSearchList Demo_List = new DemoSearchList();
Demo_List.a=(Convert.ToString(DS.Tables[1].Rows[i]["a"]));
Demo_List.b=(Convert.ToString(DS.Tables[1].Rows[i]["b"]));
Demo_List.c=(Convert.ToString(DS.Tables[1].Rows[i]["c"]));
Demo_List.d=(Convert.ToString(DS.Tables[1].Rows[i]["d"]));
Demo_List.e=(Convert.ToString(DS.Tables[1].Rows[i]["e"]));
Demo_List.f = SessionId;
Demo_List.g = StartDate;
Demo_List.h = EndDate;
a.abc.Add(Demo_List);
}
}
catch (Exception ex)
{
throw ex;
}
return DemoSearchList;
}
im getting the error at this line:
DemoSearchList.DemoSearchLists.Add(Demo_List);
anyone help me out on this
you need to add this line
if(DemoSearchList.DemoSearchLists == null)
DemoSearchList.DemoSearchLists = new List<DemoSearchList>();
before
DemoSearchList.DemoSearchLists.Add(Demo_List);