Enum Ploblem alway value how? - c#

public enum FrameStatus
{
NotReport = 0,
NormalStatus = 1,
NotNormalstatus = 2
}
but alway FrameStatus.NormalStatus how?
public FrameStatus FrameReportStatus(int Framid, string Timebet)
{
foreach (FrameCam fc in al)
{
if (fc.Timebet == Timebet && fc.IdFrame == Framid)
{
if ((int)fc.status == 1) fc.status = FrameStatus.NormalStatus;
else if ((int)fc.status == 2) fc.status = FrameStatus.NotNormalstatus;
else fc.status = FrameStatus.NotReport;
return fc.status;
}
}
return FrameStatus.NotReport;
}
my complete classs
class FrameCam
{
private ArrayList al = new ArrayList();
public string strConnect;
public FrameStatus status = FrameStatus.NormalStatus;
public string Timebet;
public int IdFrame;
public FrameCam()
{
}
public FrameCam(string st, string bt)
{
strConnect = st;
Timebet = bt;
LoadtoList();
}
public FrameStatus GetFramStatus(int Framid, string timebet)
{
foreach (FrameCam fc in al)
{
if (Framid == fc.IdFrame && timebet == fc.Timebet)
{
return fc.status;
}
}
return FrameStatus.NotReport;
}
private void LoadtoList()
{
SqlConnection conn = null;
SqlDataReader sr = null;
try
{
string query =
"SELECT * FROM FrameReport WHERE convert(varchar, GETDATE(), 101) = convert(varchar, DateTimeSign, 101) AND TimeSignBeetWeen='" +this.Timebet+"'";
conn = new SqlConnection(this.strConnect);
conn.Open();
SqlCommand sc = new SqlCommand();
sc.CommandText = query;
sc.Connection = conn;
sr = sc.ExecuteReader();
while (sr.Read())
{
FrameCam fc = new FrameCam();
fc.Timebet = sr["TimeSignBeetWeen"].ToString();
fc.IdFrame = (int)sr["IdFrame"];
if ((int)sr["Status"] == (int)FrameStatus.NormalStatus)
{
status = FrameStatus.NormalStatus;
}
if ((int)sr["Status"] == (int)FrameStatus.NotNormalstatus)
{
status = FrameStatus.NotNormalstatus;
}
else status = FrameStatus.NotReport;
al.Add(fc);
}
}
catch (Exception)
{
}
finally
{
if (sr != null) sr.Close();
if (conn != null) conn.Close();
}
}
public FrameStatus FrameReportStatus(int Framid, string Timebet)
{
foreach (FrameCam fc in al)
{
if (fc.Timebet == Timebet && fc.IdFrame == Framid)
{
if ((int)fc.status == 1) fc.status = FrameStatus.NormalStatus;
else if ((int)fc.status == 2) fc.status = FrameStatus.NotNormalstatus;
else fc.status = FrameStatus.NotReport;
return fc.status;
}
}
return FrameStatus.NotReport;
}
}

You're not assigning anything to fc.Status within LoadToList and the initialize for FrameCam's 'status' field is "FrameStatus.NormalStatus". If you update the code in LoadToList to assign to fc.status (instead of this.status, as is shown here) then it should work as you expect.
As a side note, LoadToList should be a static method, which would have mitigated this problem.

Use a debugger. Step through the code.
The code is not taking the path you think it is taking.
If you do not know how to step through code in a debugger, you MUST learn.
This is not an optional skill for a computer programmer.

Related

how can I add or update values with select count (*) and parameters SQL table and c#?

I'm trying to update information in the SQL table with this code using c# language, the first section is to count all data. the second section is to run my update Query/function
the event is : private void mlnk_ADD_Click(object sender, EventArgs e)
string SARL;
SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM [employe] WHERE CODE = #CODE OR FULLNAME =
#FULLNAME OR BIRTHDATE = #BIRTHDATE OR BIRTHPLACE = #BIRTHPLACE OR ADDRESS = #ADDRESS
OR NCCP = #NCCP OR PHONE = #PHONE OR JOB = #JOB OR SARL =#SARL", napster.myConn);
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("#CODE", mtb_CODE.Text);
cmd.Parameters.AddWithValue("#FULLNAME", mtb_FULLNAME.Text);
cmd.Parameters.AddWithValue("#BIRTHDATE", mdtp_BIRTHDATE.Text);
cmd.Parameters.AddWithValue("#BIRTHPLACE", mtb_BIRTHPLACE.Text);
cmd.Parameters.AddWithValue("#ADDRESS", mtb_ADDRESS.Text);
cmd.Parameters.AddWithValue("#NCCP", mtb_NCCP.Text);
cmd.Parameters.AddWithValue("#PHONE", mtb_PHONE.Text);
cmd.Parameters.AddWithValue("#JOB", mtb_JOB.Text);
if (mrb_LOCATERR.Checked == true)
{ SARL = "LOCA-TERR"; }
if (mrb_LUDAR.Checked == true)
{ SARL = "LUDAR"; }
if (mrb_ECORA.Checked == true)
{ SARL = "ECORA"; }
cmd.Parameters.AddWithValue("#SARL", SARL);
int UserExist = (int)cmd.ExecuteScalar();
here I am trying to insert the data into my table after checking that all data not exist to avoid the duplicate
if (UserExist > 0)
{
this.Alert("the information already exists.", frmAlert.alertTypeEnum.Error);
}
else
{
if (mrb_LOCATERR.Checked == true)
{ SARL = "LOCA-TERR"; }
if (mrb_LUDAR.Checked == true)
{ SARL = "LUDAR"; }
if (mrb_ECORA.Checked == true)
{ SARL = "ECORA"; }
if (mtb_CODE.Text != "" && mtb_FULLNAME.Text != "" && mdtp_BIRTHDATE.Text != "" && mtb_BIRTHPLACE.Text != "" && mtb_JOB.Text != "" && mtb_PHONE.Text != "")
{
// (update_employee) is public void for insert data into [employe] table
napster.update_employee(mlnk_ID.Text, mtb_CODE.Text, mtb_FULLNAME.Text, mdtp_BIRTHDATE.Text, mtb_BIRTHPLACE.Text, mtb_ADDRESS.Text, mtb_JOB.Text, mtb_PHONE.Text, SARL, mtb_NCCP.Text);
mlnk_ID.Text = "";
mtb_CODE.Text = "";
mtb_FULLNAME.Text = "";
mdtp_BIRTHDATE.Text = "";
mtb_BIRTHPLACE.Text = "";
mtb_ADDRESS.Text = "";
mtb_JOB.Text = "";
mtb_PHONE.Text = "";
mtb_NCCP.Text = "";
mrb_LOCATERR.Checked = true;
this.Alert("information updated.", frmAlert.alertTypeEnum.Info);
}
else
{
this.Alert("information not updated.", frmAlert.alertTypeEnum.Error);
}
}
Use stored procedure its more cleanner and you can define the output and the input parameteres ( exemple :select cout(*) into output_parameter where ....).
and use switch statement, There is a lot of IF in your code.

Check each List item if Null

I am quite poor at writing code, hence the question. Below I am adding items into a SharePoint List from a DB while checking if any of the fields contains DBNulls. Rather than having to perform the check for each column like I am below, can someone help me to put it in a loop if possible? I would be forever grateful
using (OdbcConnection connection = new OdbcConnection())
{
connection.ConnectionString = "dsn=abc;uid=xyz;pwd=yuo;DataSource=aaa";
connection.ConnectionTimeout = 100;
connection.Open();
OdbcCommand command_donor = new OdbcCommand("Select * From vw_SP_aaa_bbb", connection);
try
{
using (OdbcDataReader reader = command_donor.ExecuteReader())
{
while (reader.Read())
{
var obj0 = reader.GetValue(48);
var obj1 = reader.GetValue(0);
var obj2 = reader.GetValue(33);
var obj3 = reader.GetValue(47);
var obj4 = reader.GetValue(42);
var obj5 = reader.GetValue(42);
ListItem oListItem_aaa = oList_aaa .AddItem(itemCreateInfo);
if (obj0 == null || obj0.Equals(DBNull.Value))
{ oListItem_aaa["Title"] = ""; }
else
{ oListItem_aaa["Title"] = reader.GetString(48).ToString(); }
if (obj1 == null || obj1.Equals(DBNull.Value))
{ oListItem_aaa["aaa_x0020_ID"] = ""; }
else
{ oListItem_aaa["aaa_x0020_ID"] = reader.GetString(0).ToString(); }
if (obj2 == null || obj2.Equals(DBNull.Value))
{ oListItem_aaa["Excluded_x0020_By"] = ""; }
else
{ oListItem_aaa["Excluded_x0020_By"] = reader.GetString(33).ToString(); }
if (obj3 == null || obj3.Equals(DBNull.Value))
{ oListItem_aaa["Excluded_x0020_On"] = ""; }
else
{ oListItem_aaa["Excluded_x0020_On"] = reader.GetDateTime(47).ToString("MM/dd/yyyy"); }
if (obj4 == null || obj4.Equals(DBNull.Value))
{ oListItem_aaa["Reason"] = ""; }
else
{ oListItem_aaa["Reason"] = reader.GetString(42).Substring(50, reader.GetString(42).ToString().Length-50); }
if (obj5 == null || obj5.Equals(DBNull.Value))
{ oListItem_aaa["Publish"] = ""; }
else
{ oListItem_aaa["Publish"] = reader.GetString(42).Substring(50, reader.GetString(42).ToString().Length - 50); }
oListItem_aaa.Update();
context.ExecuteQuery();
}
}
}
catch (Exception exception)
{
Console.WriteLine("The Error is:" + exception);
Console.ReadLine();
}
}
Use Short IIF like
oListItem_aaa["Excluded_x0020_By"] = (obj1 == null || obj1.Equals(DBNull.Value)) ? "" : reader.GetString(0).ToString();
or
var obj0 = (reader.GetValue(48) != DBNull.Value && !String.IsNullOrEmpty(Convert.ToString(reader.GetValue(48)))) ? reader.GetValue(48) : "");
in one line. Then you dont need to use the if then else's... described also in MSDN at Operator ?: (C#-Referenz).

ExcelReader, first row getting skipped

I am developing a small plugin for an ERP System, that is reading from Excel file, and inserting into the database (SQL). For som reason with this current sheet, the first row is getting skipped. I tried working around the issue, and when a SQL error is thrown, when i force the program to insert something that is not valid, the row appear in the SQL database.
Can anyone see what i am doing wrong, because i feel like i have tried everything.
Customer Class
class Customer
{
public int ActNo {get;set;}
public int CustNo { get; set; }
public string DelPri { get; set; }
public int CustPrg3 = 22;
public string Nm { get; set; }
public int Gr6 = 5;
public int CreDt { get; set; }
public string CreUsr { get; set; }
public Customer(int ActNo,int CustNo, string DelPri, string Nm, int CreDt){
this.ActNo = ActNo;
this.CustNo = CustNo;
this.DelPri = DelPri;
this.CustPrg3 = 22;
this.Nm = Nm;
this.Gr6 = 5;
this.CreUsr = "Excel";
this.CreDt = CreDt;
}
}
The Reader
public class ExcelData
{
string _path;
public ExcelData(string path)
{
_path = path;
}
public IExcelDataReader getExcelReader()
{
// ExcelDataReader works with the binary Excel file, so it needs a FileStream
// to get started. This is how we avoid dependencies on ACE or Interop:
FileStream stream = File.Open(_path, FileMode.Open, FileAccess.Read);
// We return the interface, so that
IExcelDataReader reader = null;
try
{
if (_path.EndsWith(".xls"))
{
reader = ExcelReaderFactory.CreateBinaryReader(stream);
}
if (_path.EndsWith(".xlsx"))
{
reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
}
return reader;
}
catch (Exception)
{
throw;
}
}
public IEnumerable<string> getWorksheetNames()
{
var reader = this.getExcelReader();
var workbook = reader.AsDataSet();
var sheets = from DataTable sheet in workbook.Tables select sheet.TableName;
return sheets;
}
public IEnumerable<DataRow> GetSecondSheetData(bool firstRowIsColumnNames = true)
{
var reader = this.getExcelReader();
reader.IsFirstRowAsColumnNames = firstRowIsColumnNames;
return reader.AsDataSet().Tables[0].AsEnumerable();
}
}
Using the Reader, and adding to customer Array
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
path = openFileDialog1.FileName;
int maxActNo = 0;
int maxCustNo = 0;
var excelData = new ExcelData(path);
var albums = excelData.GetSecondSheetData();
List<Customer> customers = new List<Customer>();
Customer testCust = new Customer(1, 1, "Test", "Test", Convert.ToInt32(DateTime.Now.ToString("yyyyMMdd")));
customers.Add(testCust);
SqlDataReader rdr = cmd.ExecuteReader();
rdr.Read();
maxActNo = Convert.ToInt32(rdr["HighestActNo"]);
maxCustNo = Convert.ToInt32(rdr["HighestCustNo"]);
rdr.Close();
foreach (var row in albums)
{
if (row.ItemArray.Length == 8
&& row.ItemArray[0].ToString() != "Dato"
//&& row.ItemArray[0].ToString().Contains(convertedDate)
&& row.ItemArray[1].ToString() != "Varenummer"
&& row.ItemArray[2].ToString() != "Varenavn"
&& row.ItemArray[3].ToString() != "Kundekonto"
&& row.ItemArray[4].ToString() != "Navn"
&& row.ItemArray[5].ToString() != "Antall"
&& row.ItemArray[6].ToString() != "Antall"
&& row.ItemArray[7].ToString() != "Enhet"
//&& row.ItemArray[0].ToString() != ""
//&& row.ItemArray[1].ToString() != ""
//&& row.ItemArray[2].ToString() != ""
//&& row.ItemArray[3].ToString() != ""
//&& row.ItemArray[4].ToString() != ""
//&& row.ItemArray[5].ToString() != ""
//&& row.ItemArray[6].ToString() != ""
//&& row.ItemArray[7].ToString() != ""
)
{
Customer cust = new Customer(
maxActNo,
maxCustNo,
row[3].ToString(),
row[4].ToString(),
Convert.ToInt32(DateTime.Now.ToString("yyyyMMdd")));
if (customers[customers.Count() - 1].DelPri != row[3].ToString())
{
customers.Add(cust);
maxActNo++;
maxCustNo++;
}
}
}
customers.RemoveAt(0);
ImportController controller = new ImportController();
controller.insertCustomerIfNotExist(customers);
button2.Enabled = false;
}
}
I have chosen not to show the SQL, since i know that the probleb is not in the QUERY, it is somewhere in my if statements that the row is getting sorted, out yet i have tried for hours and figured i needed a hint.
Appreciate any suggestions
For anyone seeing this i figured out why my code was not running like i Expected.
There are no errors in the code above, i was blind to my insert loop which started from index 1, instead of index 0.
Closing this question, but thanks to anyone who read, or tried to answer.
for (int i = 1; i < customers.Count; i++)
Needed to be
for (int i = 0; i < customers.Count; i++)
in the insert function.

I have a scenario I am unable to understand, regarding the session variables

Note:-The Question pertains to ASP.NET and C#.
I have 3 functions
GetDataBySearchCriteria.
GetData.
GetGridData.
The first function is called when page is loaded. The second function gets called when the user makes a change on page and and presses a btn (GetData) where I set the Session Variable which holds the Search Criteria Data as GridData Object. The Third function is called by the second function to get data as a GridData Object where GridData is a user defined class. Later in the calling function it is converted to json object which is used to render a grid table(jqgrid).
Problem:- We maintain the session in order to facilitate a temporary search criteria holder which we use when the intended user navigates from one page to another in the same module. In my code I set the Session variable only when user presses GetData and according to the current requirement I had to put hard code changes to change the data I get from session for particular pages. In doing so the Session Data is automatically changing. I am unable to figure out why. please help.
Here's the code :-
[System.Web.Services.WebMethod]
public static string GetDataBySearchCriteria(int ReportID) // called when page is loaded
{
if (SessionVariables.RoleID == 0)
{
return "null";
}
try
{
SetProperties(ReportID);
if (!CheckAuthorization(ReportID)) // write ! before statement after testing if its not written
{
return "null";
}
else
{
#region else part
HttpContext.Current.Session["ReportID"] = ReportID;
if (HttpContext.Current.Session["SalesUserSession"] != null)
{
SetProperties(ReportID);
GridData gd = new GridData();
gd = HttpContext.Current.Session["SalesUserSession"] as GridData;
if (pagetitle.ToLower() == "top referring practices")
{
gd.RowArea = "Referring Practice";
gd.order = "Month13#desc";
gd.SelectedCpt = "";
gd.SelectedLocation = "";
gd.SelectedModality = "";
gd.SelectedReferringPractice = "";
gd.SelectedPhysicians = "";
//gd.direction = "desc";
}
else if (pagetitle.ToLower() == "top referring physicians")
{
gd.RowArea = "Physician";
gd.order = "Month13#desc";
gd.SelectedCpt = "";
gd.SelectedLocation = "";
gd.SelectedModality = "";
gd.SelectedReferringPractice = "";
gd.SelectedPhysicians = "";
//gd.direction = "desc";
}
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(GetGridData(isVolume: gd.IsVolume, pageNumber: 1, pageSize: gd.PageSize, rowArea: gd.RowArea, cpt: gd.SelectedCpt, referringPhysician: gd.SelectedPhysicians, referringPractice: gd.SelectedReferringPractice, speciality: gd.SelectedSpeciality, modalityFilter: gd.SelectedModality, locationFilter: gd.SelectedLocation, practice: gd.SelectedPractice,order:gd.order,orderBy:gd.orderBy,direction:gd.direction,orderBySummary:gd.orderBySummary,OrderBySummaryCol:gd.orderBySummaryCol,ShowSubTotalForAllGroup: gd.ShowSubTotalForAllGroup));//,direction:gd.direction,orderBySummary:"",OrderBySummaryCol:""));
}
else
{
SqlParameter[] parameters = new SqlParameter[3]; // Error Line
parameters[0] = new SqlParameter("#UserId", SqlDbType.Int);
parameters[0].Value = SessionVariables.UserID;
parameters[1] = new SqlParameter("#RoleID", SqlDbType.Int);
parameters[1].Value = SessionVariables.RoleID;
parameters[2] = new SqlParameter("#ReportTitle", SqlDbType.NVarChar);
// Below Code Added on 11/05/2015 (dd/mm/yyyy) for Testing Purposes
//if (pagetitle == "Top Referring Practices" || pagetitle == "Top Referring Physicians")
//{
// //pagetitle = "Trending Report";
//}
// Modification Ends 11/05/2015
parameters[2].Value = pagetitle;
GridData gridData = new GridData();
DataSet ds = SqlHelper.ExecuteDataset(sqlConnectionString, CommandType.StoredProcedure, "Sales_GetDataByDefaultSearch", parameters);
DataTable Datadt = ds.Tables[1];
if (ReportID == 1 && ds.Tables.Count > 3)
{
//holidayList = ds.Tables[3].AsEnumerable().Select(r=>r.Field<string>("NonWorkingDays")).ToArray();
}
StringBuilder sbcol = new StringBuilder();
sbcol.Append("[");
foreach (DataColumn column in Datadt.Columns)
{
sbcol.Append("\"").Append(GetPivotColumnName(column.ColumnName)).Append("\",");
}
sbcol.Remove(sbcol.Length - 1, 1).Append("]");
StringBuilder sbcolModel = new StringBuilder();
sbcolModel.Append("[");
string[] rowAreaFields = ds.Tables[0].Rows[0]["RowArea"].ToString().Split(',');
//string rowArea;
//rowArea = string.Join(",", rowAreaFields);
foreach (DataColumn column in Datadt.Columns)
{
//if (rowAreaFields.Contains(column.ColumnName.Trim()) && column.ColumnName != null)
if (rowAreaFields.Any(s => column.ColumnName.Trim().Contains(s)))
{
sbcolModel.Append("{\"name\":\"").Append(GetPivotColumnName(column.ColumnName.Trim())).Append("\",\"index\":\"").Append(GetPivotColumnName(column.ColumnName.Trim())).Append("\",\"classes\":\"colstyle\",\"align\":\"left\",\"sortable\":true,\"frozen\":true},");
}
//else if (holidayList.Contains(column.ColumnName.Trim()) && column.ColumnName != null)
//{
// sbcolModel.Append("{\"name\":\"").Append(GetPivotColumnName(column.ColumnName.Trim())).Append("\",\"index\":\"").Append(GetPivotColumnName(column.ColumnName.Trim())).Append("\",\"classes\":\"holidaystyle\",\"align\":\"right\",\"width\":\"70px\"},");
//}
else if (column.Ordinal >= Datadt.Columns.Count - NoofSummaryColumns)
{
if (PosNegSummaryValues.Contains(Datadt.Columns.Count - column.Ordinal))
{
sbcolModel.Append("{\"name\":\"").Append(GetPivotColumnName(column.ColumnName.Trim())).Append("\",\"index\":\"").Append(GetPivotColumnName(column.ColumnName.Trim())).Append("\",\"classes\":\"PosNegValue\",\"align\":\"right\",\"sortable\":true,\"width\":\"70px\"},");
}
else
{
if (column.ColumnName.Contains(" Actual"))
{
sbcolModel.Append("{\"name\":\"").Append(GetPivotColumnName(column.ColumnName.Trim())).Append("\",\"index\":\"").Append(GetPivotColumnName(column.ColumnName.Trim())).Append("\",\"classes\":\"SummaryColumns\",\"align\":\"right\",\"sortable\":true,\"width\":\"70px\"},");
}
else
{
sbcolModel.Append("{\"name\":\"").Append(GetPivotColumnName(column.ColumnName.Trim())).Append("\",\"index\":\"").Append(GetPivotColumnName(column.ColumnName.Trim())).Append("\",\"classes\":\"SummaryColumns\",\"align\":\"right\",\"sortable\":true,\"width\":\"120px\"},");
}
}
}
else
{
sbcolModel.Append("{\"name\":\"").Append(GetPivotColumnName(column.ColumnName.Trim())).Append("\",\"index\":\"").Append(GetPivotColumnName(column.ColumnName.Trim())).Append("\",\"align\":\"right\",\"width\":\"70px\"},");
}
}
sbcolModel.Remove(sbcolModel.Length - 1, 1);
sbcolModel.Append("]");
JavaScriptSerializer serializer = new JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> drow;
foreach (DataRow dr in Datadt.Rows)
{
drow = new Dictionary<string, object>();
foreach (DataColumn col in Datadt.Columns)
{
drow.Add(GetPivotColumnName(col.ColumnName), dr[col]);
}
rows.Add(drow);
}
StringBuilder sbjsonRows = new StringBuilder();
sbjsonRows.Append(serializer.Serialize(rows));
StringBuilder json = new StringBuilder();
json.Append("{").Append("\"rows\":").Append(sbjsonRows.ToString().Trim()).Append("}");
json.ToString();
string[] GroupAreaSortOrderArray;
string[] SummarySortOrderArray;
foreach (DataRow dr in ds.Tables[0].Rows)
{
gridData.isReportDefault = dr["IsDefault"] == DBNull.Value ? false : Convert.ToBoolean(dr["IsDefault"]);
gridData.isAllReportDefault = dr["IsAllReportDefault"] == DBNull.Value ? false : Convert.ToBoolean(dr["IsAllReportDefault"]);
gridData.IsVolume = dr["IsVolume"] == DBNull.Value ? true : Convert.ToBoolean(dr["IsVolume"]);
gridData.RowArea = dr["RowArea"] == DBNull.Value ? "Modality" : GetRowAreaForPivot(Convert.ToString(dr["RowArea"]));
gridData.TitleName = dr["TitleName"] == DBNull.Value ? "" : Convert.ToString(dr["TitleName"]);
gridData.SelectedPractice = dr["PracticeFilter"] == DBNull.Value ? "" : Convert.ToString(dr["PracticeFilter"]);
gridData.SelectedModality = dr["ModalityFilter"] == DBNull.Value ? "" : Convert.ToString(dr["ModalityFilter"]).Replace("'","");
gridData.SelectedLocation = dr["LocationFilter"] == DBNull.Value ? "" : Convert.ToString(dr["LocationFilter"]);
gridData.SelectedCpt = dr["CptFilter"] == DBNull.Value ? "" : Convert.ToString(dr["CptFilter"]).Replace("'","");
gridData.SelectedPhysicians = dr["ReferringPhysicianFilter"] == DBNull.Value ? "" : Convert.ToString(dr["ReferringPhysicianFilter"]);
gridData.SelectedReferringPractice = dr["ReferringPracticeFilter"] == DBNull.Value ? "" : Convert.ToString(dr["ReferringPracticeFilter"]);
gridData.SelectedSpeciality = dr["SpecialityFilter"] == DBNull.Value ? "" : Convert.ToString(dr["SpecialityFilter"]);
gridData.PageSize = dr["PageSize"] == DBNull.Value ? 30 : Convert.ToInt32(dr["PageSize"]);
gridData.ScheduledReport = dr["IsReportScheduled"] == DBNull.Value ? false : Convert.ToBoolean(dr["IsReportScheduled"]);
gridData.Daily = dr["Daily"] == DBNull.Value ? false : Convert.ToBoolean(dr["Daily"]);
gridData.Weekly = dr["Weekly"] == DBNull.Value ? -1 : Convert.ToInt32(dr["Weekly"]);
gridData.Monthly = dr["Monthly"] == DBNull.Value ? 0 : Convert.ToInt32(dr["Monthly"]);
gridData.SelectedPracticeName = dr["PracticeName"] == DBNull.Value ? "" : Convert.ToString(dr["PracticeName"]);
gridData.ShowSubTotalForAllGroup = dr["ShowSubTotalForAllGroup"] == DBNull.Value ? "False" : Convert.ToString(dr["ShowSubTotalForAllGroup"]);
//remap the db column to its corresponding Pivot column here
GroupAreaSortOrderArray = Convert.ToString(dr["GroupAreaSortOrder"]).Split('#');
SummarySortOrderArray = Convert.ToString(dr["SummarySortOrder"]).Split('#');
gridData.order = dr["order"] == DBNull.Value ? "" : Convert.ToString(dr["order"]);
if (GroupAreaSortOrderArray[0] != "")
{
gridData.orderBy = GroupAreaSortOrderArray[0] == null ? "" : MapDbColumnsToPivot(GroupAreaSortOrderArray[0], Datadt);
gridData.direction = GroupAreaSortOrderArray[1] == null ? "" : MapDbColumnsToPivot(GroupAreaSortOrderArray[1], Datadt);
}
if (SummarySortOrderArray[0] != "")
{
gridData.orderBySummary = SummarySortOrderArray[0] == null ? "" : MapDbColumnsToPivot(SummarySortOrderArray[0], Datadt);
gridData.orderBySummaryCol = SummarySortOrderArray[1] == null ? "" : MapDbColumnsToPivot(SummarySortOrderArray[1], Datadt);
}
//gridData.orderBy = gridData.order == "" ? "" :gridData.order.Split('#')[0];
//gridData.direction = gridData.direction==""?"" :gridData.order.Split('#')[1];
//gridData.Date = dr["Date"] == DBNull.Value ? "" : Convert.ToDateTime(dr["Date"]).ToString("yyyy-MM-dd");
}
gridData.page = 1;
gridData.total = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(ds.Tables[2].Rows[0][0]) / Convert.ToDouble(ds.Tables[0].Rows[0]["PageSize"])));
gridData.records = Convert.ToInt32(ds.Tables[2].Rows[0][0]);
gridData.col = sbcol.ToString();
gridData.colModel = sbcolModel.ToString();
gridData.rows = sbjsonRows.ToString().Trim();
return serializer.Serialize(gridData);
}
#endregion
//}
}
}
catch (Exception ex)
{
Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(ex));
throw;
}
}
[System.Web.Services.WebMethod]
public static string GetData(bool isVolume, int pageNumber, int pageSize, string rowArea, string specialityFilter, string modalityFilter, string locationFilter, string CptFilters, string referringpracticeFilter, string referringphysiciansFilter, int ReportID, string practiceFilter, string order, string orderBy, string direction, int SearchTitleID, string orderBySummary, string orderBySummaryCol, string ShowSubTotalForAllGroup)
{
try
{
if (HttpContext.Current.Session["RoleID"] == null || SessionVariables.RoleID == 0 || SessionVariables.UserID == 0)
{
return "null";
}
else
{
if (practiceFilter == null || practiceFilter == "")
{
practiceFilter = "1";
}
System.Collections.SortedList SystemSearchTitleIDs = new System.Collections.SortedList();
SystemSearchTitleIDs = (System.Collections.SortedList)HttpContext.Current.Session["SystemSearchTitleIds"];
SetProperties(ReportID);
HttpContext.Current.Session["ReportID"] = ReportID;
GridData gd;
string[] orderArray;
if (order.Trim() != string.Empty)
{
orderArray = order.Split('#');
orderArray[0] = SalesColumnMapper(orderArray[0]);
order = orderArray[0] + "#" + orderArray[1];
}
gd = GetGridData(isVolume: isVolume, pageNumber: pageNumber, pageSize: pageSize, rowArea: rowArea, modalityFilter: modalityFilter, locationFilter: locationFilter, referringPhysician: referringphysiciansFilter, referringPractice: referringpracticeFilter, speciality: specialityFilter, cpt: CptFilters, practice: practiceFilter, order: order, orderBy: orderBy, direction: direction, orderBySummary: orderBySummary, OrderBySummaryCol: orderBySummaryCol, ShowSubTotalForAllGroup: ShowSubTotalForAllGroup);
HttpContext.Current.Session["SalesUserSession"] = gd;
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(gd);
}
}
catch (Exception ex)
{
Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(ex));
throw;
}
}
private static GridData GetGridData(bool isVolume, int pageNumber, int pageSize, string rowArea, string modalityFilter, string locationFilter, string referringPhysician, string referringPractice, string speciality, string cpt, string practice, string order,string orderBy, string direction, string orderBySummary, string OrderBySummaryCol,string ShowSubTotalForAllGroup)
{
try
{
string[] rowAreaFields = rowArea.Split(',').Select(field => field.Trim()).ToArray();
DataSet ds = GetDataByFilterCriteria(isVolume: isVolume, pageNumber: pageNumber, pageSize: pageSize, rowArea: rowArea, modalityFilter: modalityFilter, locationFilter: locationFilter, referringPhysician: referringPhysician, referringPractice: referringPractice, speciality: speciality, cpt: cpt, practice: practice, order: order, ShowSubTotalForAllGroup: ShowSubTotalForAllGroup);
DataTable Datadt = ds.Tables[0];
StringBuilder sbcol = new StringBuilder();
sbcol.Append("[");
foreach (DataColumn column in Datadt.Columns)
{
sbcol.Append("\"").Append(GetPivotColumnName(column.ColumnName.Trim())).Append("\",");
}
sbcol.Remove(sbcol.Length - 1, 1);
sbcol.Append("]");
StringBuilder sbcolModel = new StringBuilder();
sbcolModel.Append("[");
foreach (DataColumn column in Datadt.Columns)
{
if (rowAreaFields.Any(s => GetPivotColumnName(column.ColumnName.Trim()).Contains(s)))
{
sbcolModel.Append("{\"name\":\"").Append(GetPivotColumnName(column.ColumnName.Trim())).Append("\",\"index\":\"").Append(GetPivotColumnName(column.ColumnName.Trim())).Append("\",\"classes\":\"colstyle\",\"align\":\"left\",\"sortable\":true,\"frozen\":true},");
}
else if (column.Ordinal >= Datadt.Columns.Count - NoofSummaryColumns)
{
if (PosNegSummaryValues.Contains(Datadt.Columns.Count - column.Ordinal))
{
sbcolModel.Append("{\"name\":\"").Append(GetPivotColumnName(column.ColumnName.Trim())).Append("\",\"index\":\"").Append(GetPivotColumnName(column.ColumnName.Trim())).Append("\",\"classes\":\"PosNegValue\",\"align\":\"right\",\"sortable\":true,\"width\":\"70px\"},");
}
else
{
if (column.ColumnName.Contains(" Actual"))
{
sbcolModel.Append("{\"name\":\"").Append(GetPivotColumnName(column.ColumnName.Trim())).Append("\",\"index\":\"").Append(GetPivotColumnName(column.ColumnName.Trim())).Append("\",\"classes\":\"SummaryColumns\",\"align\":\"right\",\"sortable\":true,\"width\":\"70px\"},");
}
else {
sbcolModel.Append("{\"name\":\"").Append(GetPivotColumnName(column.ColumnName.Trim())).Append("\",\"index\":\"").Append(GetPivotColumnName(column.ColumnName.Trim())).Append("\",\"classes\":\"SummaryColumns\",\"align\":\"right\",\"sortable\":true,\"width\":\"120px\"},");
}
}
}
else
{
sbcolModel.Append("{\"name\":\"").Append(GetPivotColumnName(column.ColumnName.Trim())).Append("\",\"index\":\"").Append(GetPivotColumnName(column.ColumnName.Trim())).Append("\",\"align\":\"right\",\"width\":\"70px\"},");
}
}
sbcolModel.Remove(sbcolModel.Length - 1, 1);
sbcolModel.Append("]");
JavaScriptSerializer serializer = new JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> drow;
foreach (DataRow dr in Datadt.Rows)
{
drow = new Dictionary<string, object>();
foreach (DataColumn col in Datadt.Columns)
{
drow.Add(GetPivotColumnName(col.ColumnName), dr[col]);
}
rows.Add(drow);
}
StringBuilder sbjsonRows = new StringBuilder();
sbjsonRows.Append(serializer.Serialize(rows));
StringBuilder json = new StringBuilder();
json.Append("{");
json.Append("\"rows\":");
json.Append(sbjsonRows.ToString().Trim());
json.Append("}");
json.ToString();
GridData gridData = new GridData();
gridData.SelectedPractice = practice; // New statement returns practicefilter -- if grid displays this we can remove it
gridData.SelectedLocation = locationFilter;
gridData.SelectedModality = modalityFilter;
gridData.SelectedReferringPractice = referringPractice;
gridData.SelectedPhysicians = referringPhysician;
gridData.SelectedCpt = cpt;
gridData.SelectedSpeciality = speciality;
gridData.PageSize = pageSize;
gridData.RowArea = rowArea;
gridData.IsVolume = isVolume;
gridData.page = 1;
gridData.total = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(ds.Tables[1].Rows[0][0]) / Convert.ToDouble(pageSize)));
gridData.records = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
gridData.col = sbcol.ToString();
gridData.colModel = sbcolModel.ToString();
gridData.rows = sbjsonRows.ToString().Trim();
gridData.order = order;
gridData.orderBy = orderBy;
gridData.direction = direction;
gridData.orderBySummary = orderBySummary;
gridData.orderBySummaryCol = OrderBySummaryCol;
gridData.ShowSubTotalForAllGroup = ShowSubTotalForAllGroup;
//Temporary Solution for getting the practice name for the corresponding practice
//var reader = SqlHelper.ExecuteReader(sqlConnectionString, CommandType.Text, "select practicename from practice where practiceid="+practice);
SqlConnection con = new SqlConnection(sqlConnectionString);
SqlCommand cmd = new SqlCommand();
Object returnValue;
cmd.CommandText = "select practicename from practice where practiceid="+practice;
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
returnValue = cmd.ExecuteScalar();
con.Close();
gridData.SelectedPracticeName = returnValue.ToString();
return gridData;
}
catch (Exception ex)
{
Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(ex));
throw;
}
}
When you do
gd = HttpContext.Current.Session["SalesUserSession"] as GridData;
gd is an Object reference to HttpContext.Current.Session["SalesUserSession"]
that's why your session is impacted by your modifications.
this topic may help you achieve what you want :
Deep cloning objects

Is that necessary to dispose objects inside static functions?

Ok i am having a major problem atm.
My software is using extremely high amount of ram. I am using a lot of HtmlAgilityPack.HtmlDocument objects with big size pages sources.
However all of the objects are used inside static functions and HtmlAgilityPack.HtmlDocument isn't IDisposable
So do i need to set every variable explicitly to null ?
Even if they are inside static functions ?
For example do i need to set these variables to null at the end of the function below
The variables i am asking : lstDrwList ? Or since it is inside it will get disposed automatically ?
Should i call explicitly garbage collector ?
C# .net 4.5 WPF application
private static void func_CheckWaitingToProcessPages(Object state)
{
ParallelOptions myOptions = new ParallelOptions();
myOptions.MaxDegreeOfParallelism = PublicSettings.ir_How_Many_Tasks_For_Per_Pages_Process;
List<DataRow> lstDrwList = new List<DataRow>();
using (DataTable dtMyTable = DbConnection.db_Select_DataTable(srSelectTopProcessPagesQuery))
{
foreach (DataRow drw in dtMyTable.Rows)
{
lstDrwList.Add(drw);
}
}
Parallel.ForEach(lstDrwList, myOptions, drw =>
{
process_Given_Page(drw);
});
}
The problem is found issue is how to fix
Here the problem this happens in 10 seconds i used visual studio profiler
Here the full class that causes this huge memory leak issue
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace doktora_tez_projesi_crawler_program
{
public static class PagesProcessor
{
private static Timer _timer;
private static int howManySeconds = 10;
public static void func_StartCrawlingWaitingUrls()
{
PublicStaticFunctions.AddMsgToEvents("Checking waiting to process crawled urls process started every " + howManySeconds + " seconds!");
_timer = new Timer(func_CheckWaitingToProcessPages, null, PublicSettings.irTimers_Delayed_Start_MiliSeconds, howManySeconds * 1000);
}
private static string srSelectTopProcessPagesQuery = " select top 100 cl_IdUrl,cl_RooSiteId,cl_CrawlSource,cl_CrawlOrgUrl from tblCrawlUrls " +
" where cl_PageProcessed=0 and cl_TotalCrawlTimes > 0 " +
" order by cl_LastProcessDate asc";
private static void func_CheckWaitingToProcessPages(Object state)
{
ParallelOptions myOptions = new ParallelOptions();
myOptions.MaxDegreeOfParallelism = PublicSettings.ir_How_Many_Tasks_For_Per_Pages_Process;
List<DataRow> lstDrwList = new List<DataRow>();
using (DataTable dtMyTable = DbConnection.db_Select_DataTable(srSelectTopProcessPagesQuery))
{
foreach (DataRow drw in dtMyTable.Rows)
{
lstDrwList.Add(drw);
}
}
Parallel.ForEach(lstDrwList, myOptions, drw =>
{
process_Given_Page(drw);
});
}
private class csProductFeatures
{
public string srProductRootSiteId = "null", srProductTitle = "null", srProductCode = "null", srProductImageLink = "null";
public string srProductDetailedExplanation = "null", srProductFeatures = "null", srCrawledOrgUrl = "null", srProductIdCode = "null";
public bool blPossibleProductPage = false, blFreeCargo = false, blProductPage = true;
public List<string> lstProductCategories = new List<string>();
public int irProductPrice = 0;
public List<csProductComments> lstProductComments = new List<csProductComments>();
public List<KeyValuePair<string, string>> lstProductFeatures = new List<KeyValuePair<string, string>>();
}
private class csProductComments
{
public string srCommentTitle = "null", srCommentPros = "null", srCommentCons = "null";
public int irCommentScore = 0; //0 = negative 5=full star
}
private static void process_Given_Page(DataRow drw)
{
csProductFeatures temp_ProductFeatures = new csProductFeatures();
temp_ProductFeatures.srProductRootSiteId = drw["cl_RooSiteId"].ToString();
temp_ProductFeatures.srCrawledOrgUrl = drw["cl_CrawlOrgUrl"].ToString();
HtmlDocument hdMyDoc = new HtmlDocument();//nulled
hdMyDoc.LoadHtml(drw["cl_CrawlSource"].ToString());
bool blBreakLoop = false;
foreach (var vrVariable in PublicVariables.dicRootSites[temp_ProductFeatures.srProductRootSiteId].lstRootSiteIdentifiers)
{
if (vrVariable.srHtmlObjectType != "link")
{
HtmlNodeCollection hdNodes;
if (vrVariable.blSelectMultipleNodes == false)
hdNodes = hdMyDoc.DocumentNode.SelectNodes(string.Format("//{0}[#{1}='{2}']", vrVariable.srHtmlObjectType,
vrVariable.srHtmlObjectTypeIdentifier, vrVariable.srHtmlObjectTypeName));
else
hdNodes = hdMyDoc.DocumentNode.SelectNodes(string.Format("//{0}[#{1}='{2}']//{3}", vrVariable.srHtmlObjectType,
vrVariable.srHtmlObjectTypeIdentifier, vrVariable.srHtmlObjectTypeName, vrVariable.srHtmlSubIdentifierType));
if (hdNodes == null && vrVariable.srIndetifierType == "ProductTitle")
{
blBreakLoop = true;
temp_ProductFeatures.blProductPage = false;
continue;
}
if (blBreakLoop == true)
break;
if (hdNodes == null)
continue;
string sr_Node_Required_Val = "null";
if (hdNodes[0].InnerText != null)
sr_Node_Required_Val = hdNodes[0].InnerText;
string srLinkVal = "null";
if (vrVariable.srHtmlObjectType == "a" && hdNodes[0].Attributes != null)
{
if (hdNodes[0].Attributes["href"] != null)
{
srLinkVal = PublicStaticFunctions.Return_Absolute_Url(hdNodes[0].Attributes["href"].Value, temp_ProductFeatures.srCrawledOrgUrl);
}
}
if (vrVariable.blGetValue == true)
{
if (hdNodes[0].Attributes != null)
if (hdNodes[0].Attributes["value"] != null)
sr_Node_Required_Val = hdNodes[0].Attributes["value"].Value;
}
sr_Node_Required_Val = sr_Node_Required_Val.Trim();
switch (vrVariable.srIndetifierType)
{
case "ProductPage":
temp_ProductFeatures.blPossibleProductPage = true;
break;
case "ProductTitle":
temp_ProductFeatures.srProductTitle = sr_Node_Required_Val;
break;
case "ProductCode":
temp_ProductFeatures.srProductCode = sr_Node_Required_Val;
break;
case "ProductCargo":
temp_ProductFeatures.blFreeCargo = true;
break;
case "ProductCategories":
temp_ProductFeatures.lstProductCategories = func_Return_Product_Categories(hdNodes);
break;
case "ProductPrice":
temp_ProductFeatures.irProductPrice = func_Return_Product_Price(sr_Node_Required_Val, temp_ProductFeatures.srProductRootSiteId);
break;
case "ProductImage":
temp_ProductFeatures.srProductImageLink = srLinkVal;
break;
case "ProductIdCode":
temp_ProductFeatures.srProductIdCode = sr_Node_Required_Val;
break;
}
}
if (vrVariable.srHtmlObjectType == "link")
{
string srLinkToFetch = vrVariable.srHtmlObjectTypeIdentifier;
if (vrVariable.blUsesProductIdCode == true)
{
srLinkToFetch = string.Format(srLinkToFetch, temp_ProductFeatures.srProductIdCode);
}
string srFetchResult = CrawlGivenUrl.func_fetch_Page(srLinkToFetch);
string srResultToAssign = "null";
if (srFetchResult == PublicSettings.srCrawlFailedMessage)
{
srResultToAssign = srFetchResult;
}
else
{
HtmlDocument temp_HdDocument = new HtmlDocument();//nulled
temp_HdDocument.LoadHtml(srFetchResult);
if (temp_HdDocument.DocumentNode != null)
if (temp_HdDocument.DocumentNode.InnerText != null)
srResultToAssign = temp_HdDocument.DocumentNode.InnerText;
temp_HdDocument = null;
}
switch (vrVariable.srIndetifierType)
{
case "ProductExplanation":
temp_ProductFeatures.srProductDetailedExplanation = srResultToAssign;
break;
case "ProductFeatures":
temp_ProductFeatures.lstProductFeatures = func_Return_Product_Features(temp_ProductFeatures.srProductRootSiteId, srFetchResult, temp_ProductFeatures.srCrawledOrgUrl);
break;
}
}
}
if (temp_ProductFeatures.blProductPage == true)
{
string asdas = "";
}
hdMyDoc = null;
}
private static List<string> func_Return_Product_Categories(HtmlNodeCollection hdNodeCollection)
{
List<string> lstCategories = new List<string> { };
foreach (HtmlNode hdNode in hdNodeCollection)
{
if (hdNode.InnerText != null)
{
lstCategories.Add(hdNode.InnerText);
}
}
return lstCategories;
}
private static int func_Return_Product_Price(string srPriceText, string srRootSiteId)
{
int irPrice = 0;
srPriceText = srPriceText.Replace(PublicVariables.dicRootSites[srRootSiteId].srPriceDelimeter, "");
if (srPriceText.Contains(PublicVariables.dicRootSites[srRootSiteId].srPriceIgnoreDelimeter) == true)
{
srPriceText = srPriceText.Substring(0, srPriceText.IndexOf(PublicVariables.dicRootSites[srRootSiteId].srPriceIgnoreDelimeter));
}
Int32.TryParse(srPriceText, out irPrice);
return irPrice;
}
private static List<KeyValuePair<string, string>> func_Return_Product_Features(string srRootSiteId, string srPageSource, string srCrawlUrl)
{
List<KeyValuePair<string, string>> lstFoundFeatures = new List<KeyValuePair<string, string>>();
if (srPageSource == PublicSettings.srCrawlFailedMessage)
return lstFoundFeatures;
HtmlDocument temp_HdDocument = new HtmlDocument();//nulled
temp_HdDocument.LoadHtml(srPageSource);
List<string> lstFeatureTitles = new List<string>();
List<string> lstFeatureDescriptions = new List<string>();
foreach (var vrVariable in PublicVariables.dicRootSites[srRootSiteId].lstRootSitesFeaturesIdentifiers)
{
if (vrVariable.blPerFeatureIdentifier == true)
{
HtmlNodeCollection hdNodes = temp_HdDocument.DocumentNode.SelectNodes(string.Format("//{0}[#{1}='{2}']", vrVariable.srHtmlObjectType,
vrVariable.srHtmlObjectIdentifier, vrVariable.srHtmlObjectIdentifierName));
if (hdNodes != null)
foreach (var vrNewVariable in PublicVariables.dicRootSites[srRootSiteId].lstRootSitesFeaturesIdentifiers)
{
if (vrNewVariable.blPerFeatureIdentifier == false)
{
foreach (HtmlNode hdTempNode in hdNodes)
{
var vrTempNewNode = hdTempNode.SelectSingleNode(string.Format("//{0}[#{1}='{2}']", vrVariable.srHtmlObjectType,
vrVariable.srHtmlObjectIdentifier, vrVariable.srHtmlObjectIdentifierName));
if (vrTempNewNode != null)
if (vrTempNewNode.InnerText != null)
{
string srNodeFeature = vrTempNewNode.InnerText.Trim();
switch (vrVariable.srWhichFeatureIdentifier)
{
case "FeatureTitle":
lstFeatureTitles.Add(srNodeFeature);
break;
case "FeatureDescription":
lstFeatureDescriptions.Add(srNodeFeature);
break;
}
}
}
}
}
break;
}
}
temp_HdDocument = null;
if (lstFeatureDescriptions.Count != lstFeatureTitles.Count)
{
ErrorLogger.LogError("found features count not equal to features description count crawled url: " + srCrawlUrl);
return lstFoundFeatures;
}
for (int i = 0; i < lstFeatureDescriptions.Count; i++)
{
KeyValuePair<string, string> myKeyValPair = new KeyValuePair<string, string>(lstFeatureTitles[i], lstFeatureDescriptions[i]);
lstFoundFeatures.Add(myKeyValPair);
}
return lstFoundFeatures;
}
}
}
No, you don't need to set variables to null in both static and instance methods. The variables inside a method (even inside static method) are on the stack space of the method, so generally they will go out of scope at the end of method execution and will be targeted for garbage collection. And Generally explicitly calling the garbage collector isn't a good practice.

Categories