Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 107143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:42:27+00:00 2026-05-11T01:42:27+00:00

I have following Code Block Which I tried to optimize in the Optimized section

  • 0

I have following Code Block Which I tried to optimize in the Optimized section

DataSet dsLastWeighing = null; DataSet ds = null; DataSet dsWeight = null; string strQuery = string.Empty; string strWhere = string.Empty; Database db = null;  #region Original Code Block             try             {                    db = DatabaseFactory.CreateDatabase();                  strWhere = 'WHERE SESSION_ID = ''+pSessionID+''';                  strQuery = SQ.BusinessLogic.SQLQueryFactory.GetPatientLastWeighing('DeleteWeightRecent',db.ToString(),pFacilityID,pSessionID,strWhere,0,DateTime.Now.ToUniversalTime(),0,'');                 db.ExecuteNonQuery(System.Data.CommandType.Text, strQuery);                  strWhere = 'WHERE LAB_ID = 0';                 strQuery = SQ.BusinessLogic.SQLQueryFactory.GetPatientLastWeighing('InsertWeightRecent',db.ToString(),pFacilityID,pSessionID,strWhere,0,DateTime.Now.ToUniversalTime(),0,'');                 db.ExecuteNonQuery(System.Data.CommandType.Text, strQuery);                  strWhere = strWhere = 'WHERE SESSION_ID = ''+pSessionID+''';                  strQuery = SQ.BusinessLogic.SQLQueryFactory.GetPatientLastWeighing('GetPatientID',db.ToString(),pFacilityID,pSessionID,strWhere,0,DateTime.Now.ToUniversalTime(),0,'');                 ds = (DataSet) db.ExecuteDataSet(System.Data.CommandType.Text, strQuery);                  foreach(DataRow dr in ds.Tables[0].Rows)                 {                     if (db.ToString() == 'Microsoft.Practices.EnterpriseLibrary.Data.SqlBase.SqlBaseDatabase')                     {                         strWhere = 'WHERE LAB_ID=0 AND PAT_ID ='+ int.Parse(dr['PAT_ID'].ToString())+' AND WHEN IN(SELECT MAX(WHEN) FROM PATIENT_LAB WHERE LAB_ID=0 AND PAT_ID='+ int.Parse(dr['PAT_ID'].ToString())+')';                      }                     else if (db.ToString() == 'Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase')                     {                         strWhere = 'WHERE LAB_ID=0 AND PAT_ID ='+ int.Parse(dr['PAT_ID'].ToString())+' AND [WHEN] IN(SELECT MAX([WHEN]) FROM PATIENT_LAB WHERE LAB_ID=0 AND PAT_ID='+ int.Parse(dr['PAT_ID'].ToString())+')';                      }                     strQuery = SQ.BusinessLogic.SQLQueryFactory.GetPatientLastWeighing('GetWeight',db.ToString(),pFacilityID,pSessionID,strWhere,0,DateTime.Now.ToUniversalTime(),0,'');                      strMain.append(strQuery+' ');                     dsWeight = (DataSet) db.ExecuteDataSet(System.Data.CommandType.Text, strQuery);                       foreach(DataRow drWeight in dsWeight.Tables[0].Rows)                     {                                                    strWhere =  'WHERE PAT_ID = '+int.Parse(dr['PAT_ID'].ToString())+' AND SESSION_ID =''+pSessionID+''';                         strQuery = SQ.BusinessLogic.SQLQueryFactory.GetPatientLastWeighing('UpdateWeightRecent',db.ToString(),pFacilityID,pSessionID,strWhere,decimal.Parse(drWeight['LEVEL'].ToString()),DateTime.Parse(drWeight['WHEN'].ToString()).ToUniversalTime(),int.Parse(drWeight['IS_BAD'].ToString()),drWeight['NOTE'].ToString());                         db.ExecuteNonQuery(System.Data.CommandType.Text, strQuery);                      }                  }                  strWhere = ' ORDER BY W.IS_BAD DESC, P.LASTNAME ASC, P.FIRSTNAME ASC,P.MIDDLENAME ASC';                 strQuery = SQ.BusinessLogic.SQLQueryFactory.GetPatientLastWeighing('GetPatientLastWeight',db.ToString(),pFacilityID,pSessionID,strWhere,0,DateTime.Now.ToUniversalTime(),0,'');                 dsLastWeighing = (DataSet) db.ExecuteDataSet(System.Data.CommandType.Text, strQuery);                                }             catch(Exception ex)             {                 throw ex;             }             finally             {                 db = null;                 ds= null;                 dsWeight= null;             }             return dsLastWeighing;             #endregion     --Optimized Section--      #region Optimized Code Block              try             {                    StringBuilder strMain=new StringBuilder();                 db = DatabaseFactory.CreateDatabase();                  //StartTime=DateTime.Now.ToLongTimeString();                 strWhere = 'WHERE SESSION_ID = ''+pSessionID+''';                  strQuery = SQ.BusinessLogic.SQLQueryFactory.GetPatientLastWeighing('DeleteWeightRecent',db.ToString(),pFacilityID,pSessionID,strWhere,0,DateTime.Now.ToUniversalTime(),0,'');                 //EndTime=DateTime.Now.ToLongTimeString();                 //db.ExecuteNonQuery(System.Data.CommandType.Text, strQuery);                 strMain.append(strQuery+' ');                  strWhere = 'WHERE LAB_ID = 0';                  //StartTime=DateTime.Now.ToLongTimeString();                   strQuery = SQ.BusinessLogic.SQLQueryFactory.GetPatientLastWeighing('InsertWeightRecent',db.ToString(),pFacilityID,pSessionID,strWhere,0,DateTime.Now.ToUniversalTime(),0,'');                 //EndTime=DateTime.Now.ToLongTimeString();                  //db.ExecuteNonQuery(System.Data.CommandType.Text, strQuery);                 strMain.append(strQuery+' ');                  strWhere = strWhere = 'WHERE SESSION_ID = ''+pSessionID+''';                  //StartTime=DateTime.Now.ToLongTimeString();                   strQuery = SQ.BusinessLogic.SQLQueryFactory.GetPatientLastWeighing('GetPatientID',db.ToString(),pFacilityID,pSessionID,strWhere,0,DateTime.Now.ToUniversalTime(),0,'');                 //EndTime=DateTime.Now.ToLongTimeString();                 //ds = (DataSet) db.ExecuteDataSet(System.Data.CommandType.Text, strQuery);                  strMain.append(strQuery+' ');                 //StartTime=DateTime.Now.ToLongTimeString();                  ds = (DataSet) db.ExecuteDataSet(System.Data.CommandType.Text, strMain.ToString());                 //EndTime=DateTime.Now.ToLongTimeString();                  strMain=null;                   foreach(DataRow dr in ds.Tables[0].Rows)                 {                     //StartTime=DateTime.Now.ToLongTimeString();                     if (db.ToString() == 'Microsoft.Practices.EnterpriseLibrary.Data.SqlBase.SqlBaseDatabase')                     {                         strWhere = 'WHERE LAB_ID=0 AND PAT_ID ='+ int.Parse(dr['PAT_ID'].ToString())+' AND WHEN IN(SELECT MAX(WHEN) FROM PATIENT_LAB WHERE LAB_ID=0 AND PAT_ID='+ int.Parse(dr['PAT_ID'].ToString())+')';                      }                     else if (db.ToString() == 'Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase')                     {                         strWhere = 'WHERE LAB_ID=0 AND PAT_ID ='+ int.Parse(dr['PAT_ID'].ToString())+' AND [WHEN] IN(SELECT MAX([WHEN]) FROM PATIENT_LAB WHERE LAB_ID=0 AND PAT_ID='+ int.Parse(dr['PAT_ID'].ToString())+')';                      }                     strQuery = SQ.BusinessLogic.SQLQueryFactory.GetPatientLastWeighing('GetWeight',db.ToString(),pFacilityID,pSessionID,strWhere,0,DateTime.Now.ToUniversalTime(),0,'');                      strMain.append(strQuery+' ');                     //EndTime=DateTime.Now.ToLongTimeString();                     //dsWeight = (DataSet) db.ExecuteDataSet(System.Data.CommandType.Text, strQuery);                      /*                     foreach(DataRow drWeight in dsWeight.Tables[0].Rows)                     {                                                    strWhere =  'WHERE PAT_ID = '+int.Parse(dr['PAT_ID'].ToString())+' AND SESSION_ID =''+pSessionID+''';                         strQuery = SQ.BusinessLogic.SQLQueryFactory.GetPatientLastWeighing('UpdateWeightRecent',db.ToString(),pFacilityID,pSessionID,strWhere,decimal.Parse(drWeight['LEVEL'].ToString()),DateTime.Parse(drWeight['WHEN'].ToString()).ToUniversalTime(),int.Parse(drWeight['IS_BAD'].ToString()),drWeight['NOTE'].ToString());                         db.ExecuteNonQuery(System.Data.CommandType.Text, strQuery);                      }                     */                 }                   dsWeight = (DataSet) db.ExecuteDataSet(System.Data.CommandType.Text, strMain.ToString());                 strMain=null;                 //StartTime=DateTime.Now.ToLongTimeString();                 for(int i=0;i<dsWeight.Tables.Count;i++)                 {                     foreach(DataRow drWeight in dsWeight.Tables[i].Rows)                     {                                                    strWhere =  'WHERE PAT_ID = '+int.Parse(dr['PAT_ID'].ToString())+' AND SESSION_ID =''+pSessionID+''';                         strQuery = SQ.BusinessLogic.SQLQueryFactory.GetPatientLastWeighing('UpdateWeightRecent',db.ToString(),pFacilityID,pSessionID,strWhere,decimal.Parse(drWeight['LEVEL'].ToString()),DateTime.Parse(drWeight['WHEN'].ToString()).ToUniversalTime(),int.Parse(drWeight['IS_BAD'].ToString()),drWeight['NOTE'].ToString());                         strMain.append(strQuery+' ');                         //db.ExecuteNonQuery(System.Data.CommandType.Text, strQuery);                        }                 }                 db.ExecuteNonQuery(System.Data.CommandType.Text, strMain.ToString());                    //EndTime=DateTime.Now.ToLongTimeString();                  //StartTime=DateTime.Now.ToLongTimeString();                 strWhere = ' ORDER BY W.IS_BAD DESC, P.LASTNAME ASC, P.FIRSTNAME ASC,P.MIDDLENAME ASC';                 strQuery = SQ.BusinessLogic.SQLQueryFactory.GetPatientLastWeighing('GetPatientLastWeight',db.ToString(),pFacilityID,pSessionID,strWhere,0,DateTime.Now.ToUniversalTime(),0,'');                 dsLastWeighing = (DataSet) db.ExecuteDataSet(System.Data.CommandType.Text, strQuery);                    //EndTime=DateTime.Now.ToLongTimeString();             }             catch(Exception ex)             {                 throw ex;             }             finally             {                 db = null;                 ds= null;                 dsWeight= null;             }             return dsLastWeighing;               #endregion 

Can This Further be optimized .. Just concentrate on minimizing the loops.. I am not getting any ideas further..Any Help would be appreciated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:42:28+00:00Added an answer on May 11, 2026 at 1:42 am

    I would suggest:

    • Use parameterised SQL
    • Get rid of the catch block, or at least use ‘throw’ instead of ‘throw ex’ so you don’t lose the information
    • Get rid of the finally block – it’s not helping you
    • Declare variables when they’re first needed, not all at the top of the method
    • Break the method up into more manageable sections

    When all those have been done, you’ll be in a better place to tune the performance. At that point, profile the app and see whether the pain point is actually in the .NET code or in the interaction with the database. In my experience, database applications are usually improved by:

    • Improving the SQL queries
    • Reducing the round-trips
    • Optimising the database (applying indexes etc)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 217k
  • Answers 217k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The key is git submodules. Start reading the Submodules chapter… May 12, 2026 at 11:16 pm
  • Editorial Team
    Editorial Team added an answer To do this, specify no @WebSite attribute at all. <!--… May 12, 2026 at 11:16 pm
  • Editorial Team
    Editorial Team added an answer I would submit the data first via AJAX then the… May 12, 2026 at 11:16 pm

Related Questions

I am building a modal box as a custom server control and I want
I need to simulate a test scenario in which I call the getBytes() method
just trying to tie up a few loose odds and ends here. I have
I am trying to read an image that resides somewhere on the web from

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.