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 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

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 185878
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 10, 20222022-06-10T09:08:38+00:00 2022-06-10T09:08:38+00:00

c# – Error appearing only for the first item inside the if statement

  • 0

[ad_1]

I am a junior developer and my first task at work is to correct a part of a Web Forms project.
The project maps data from an sql server to some variables inside the project and checks if the fields inside the table is null.If everything is ok then it generates an xml file with all the data mapped.
If the variable(CustomerBalance) is null then one error message is generated.
The fact is that even if two or more Customers has null CustomerBalance,only the first one will show up in the UI.

This is the code inside the if statement

foreach (var CustAccnt in CustomerAccounts)
            {
                List<string> accountControllingPersons = new List<string>();

                cmd.CommandText = "SELECT " + datamapping.Where(s => s.CRSElements == "AccOwnerCust").Select(s => s.ColumsNames).FirstOrDefault().ToString() + " FROM " + datamapping.Where(s => s.CRSElements == "AccOwnerCust").Select(s => s.TableNames).FirstOrDefault().ToString() + " where " + datamapping.Where(s => s.CRSElements == "AccOwnerAccount").Select(s => s.ColumsNames).FirstOrDefault().ToString() + " = '" + CustAccnt.Account + "'" + " and " + datamapping.Where(s => s.CRSElements == "AccOwnerRelation").Select(s => s.ColumsNames).FirstOrDefault().ToString() + " !='" + datamapping.Where(s => s.CRSElements == "AccOwnerRelation").Select(s => s.Values).FirstOrDefault().ToString() + "' ";
                cmd.CommandType = CommandType.Text;
                cmd.Connection = sqlConnection1;
                sqlConnection1.Open();
                reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    accountControllingPersons.Add(reader[datamapping.Where(s => s.CRSElements == "AccOwnerCust").Select(s => s.ColumsNames).FirstOrDefault().ToString()].ToString());
                }
                sqlConnection1.Close();

                cmd.CommandText = "SELECT " + datamapping.Where(s => s.CRSElements == "CustomerOtherRelationPartyID").Select(s => s.ColumsNames).FirstOrDefault().ToString() + " FROM " + datamapping.Where(s => s.CRSElements == "CustomerOtherRelationPartyID").Select(s => s.TableNames).FirstOrDefault().ToString() + " where " + datamapping.Where(s => s.CRSElements == "CustomerOtherRelationRelatedParty").Select(s => s.ColumsNames).FirstOrDefault().ToString() + " = '" + id + "'";
                cmd.CommandType = CommandType.Text;
                cmd.Connection = sqlConnection1;
                sqlConnection1.Open();
                reader = cmd.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        accountControllingPersons.Add(reader[datamapping.Where(s => s.CRSElements == "CustomerOtherRelationPartyID").Select(s => s.ColumsNames).FirstOrDefault().ToString()].ToString());
                    }
                }

                sqlConnection1.Close();
                List<String> TINCountries = new List<String>();
                foreach (var item in accountControllingPersons)
                {
                    cmd.CommandText = "Select distinct " + datamapping.Where(s => s.CRSElements == "TINIssueCountry").Select(s => s.ColumsNames).FirstOrDefault().ToString() +
                                    " from " + datamapping.Where(s => s.CRSElements == "TINCustID").Select(s => s.TableNames).FirstOrDefault().ToString() +
                                    " where " + datamapping.Where(s => s.CRSElements == "TINCustID").Select(s => s.ColumsNames).FirstOrDefault().ToString() +
                                    " = '" + item +
                                    "'  and " + datamapping.Where(s => s.CRSElements == "TINIssueCountry").Select(s => s.ColumsNames).FirstOrDefault().ToString() +
                                    " in (" + inCoutries + ")" +
                                    " and " + datamapping.Where(s => s.CRSElements == "TINIDType").Select(s => s.ColumsNames).FirstOrDefault().ToString() +
                                    " like '" + defaultValues.Where(s => s.Names == "PartyID").Select(s => s.Values).FirstOrDefault().ToString() + "%' "; ;

                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = sqlConnection1;
                    sqlConnection1.Open();
                    reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        TINCountries.Add(reader[datamapping.Where(s => s.CRSElements == "TINIssueCountry").Select(s => s.ColumsNames).FirstOrDefault().ToString()].ToString());
                    }
                    sqlConnection1.Close();
                }

                foreach (var countr in TINCountries.Distinct())
                {
                    CorrectableAccountReport_Type AccToReport = new CorrectableAccountReport_Type();

                    string Account = CustAccnt.Account;
                    Account AcountData = new Account();
                    string repPeriod = CRScontext.tblBankParams.Where(r => r.Parameter == "ReportingPeriod").Select(r => r.Value).FirstOrDefault();
                    string year = DateTime.Parse(repPeriod).Year.ToString();
                    var custBalances = CRScontext.tblBalances.Where(b => b.CustomerID == id && b.BalanceYear == year && b.CustomerAccount == Account).FirstOrDefault();

                    cmd.CommandText = "SELECT " + customerAccountDataSelect + " FROM " + datamapping.Where(s => s.CRSElements == "AccountID").Select(s => s.TableNames).FirstOrDefault().ToString() + " where " + datamapping.Where(s => s.CRSElements == "AccountID").Select(s => s.ColumsNames).FirstOrDefault().ToString() + " = '" + Account + "'";
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = sqlConnection1;
                    sqlConnection1.Open();
                    reader = cmd.ExecuteReader();
                    if (custBalances != null)
                    {
                        while (reader.Read())
                        {
                            decimal amount = 0;

                            if (!String.IsNullOrEmpty(custBalances.December))
                            {
                                amount = Convert.ToDecimal(custBalances.December.ToString(), new CultureInfo("en-US"));
                            }
                            string closeDate = reader[datamapping.Where(s => s.CRSElements == "AccCloseDate").Select(s => s.ColumsNames).FirstOrDefault().ToString()].ToString();
                            DateTime? dateSample;
                            dateSample = null;

                            AcountData = new Account { Closed = Convert.ToBoolean(reader[datamapping.Where(s => s.CRSElements == "AccountClosed").Select(s => s.ColumsNames).FirstOrDefault().ToString()].ToString()),
                                BalanceAmt = amount,
                                User1 = reader[datamapping.Where(s => s.CRSElements == "AccountcurrCodetype").Select(s => s.ColumsNames).FirstOrDefault().ToString()].ToString(),
                                User3 = reader[datamapping.Where(s => s.CRSElements == "AccountNumbType").Select(s => s.ColumsNames).FirstOrDefault().ToString()].ToString(),
                                CloseDate = !string.IsNullOrEmpty(closeDate) ? DateTime.Parse(closeDate) : dateSample,
                                AcStatus= reader[datamapping.Where(s => s.CRSElements == "AcStatus").Select(s => s.ColumsNames).FirstOrDefault().ToString()].ToString()

                            };
                        }
                    }
                    else
                    {
                        HttpContext.Current.Session["Generated"] = "Error! No Balance Record for Account : " + Account;
                        ExceptionUtility.Logger(HttpContext.Current.Session["Generated"].ToString());
                        return null;
                    }

                    sqlConnection1.Close();

[ad_2]

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 399 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 371 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 366 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

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.