列表在返回语句MVC处为空

时间:2015-05-01 16:48:24

标签: c#

我有一个表单,用户通过名称,ssn,旧帐号等搜索客户的帐户。当我在while循环中填充列表时,值存在,但是当它退出循环时,计数为0的return语句。任何想法我为什么要丢失这些值?

创建列表的功能:

public static List<Account> GetAccountNumbersFromCustomerSSN(string CustomerSSN)
        {

            List<Account> accts = new List<Account>();

            try
            {
                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TA_connectionstring"].ConnectionString))
                {
                    SqlCommand cmd = new SqlCommand("spGet_Account_Numbers_From_SSN", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    con.Open();

                    SqlParameter P1 = new SqlParameter("@CUSTOMER_SSN", DbType.String);
                    P1 = new SqlParameter("@CUSTOMER_SSN", DbType.String);
                    P1.Value = CustomerSSN;
                    cmd.Parameters.Add(P1);

                    SqlDataReader dr = cmd.ExecuteReader();

                    while (dr.Read())
                    {
                        Account acct = new Account
                        {
                            OriginalAcctNumber = dr["TABOAT_ACCOUNT_NUMBER"].ToString().TrimStart('0'),
                            FTBOATAcctNumber = dr["FTBOAT_ACCOUNT_NUMBER"].ToString().TrimStart('0'),
                            SSN = CustomerSSN.Substring(CustomerSSN.Length -4).PadLeft(CustomerSSN.Length, '*'),
                            CustomerName = dr["CUST_NAME"].ToString(),
                            ProductType = dr["TRGT_PROD"].ToString()
                        };
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                ExceptionUtility.LogException(ex, "GetCustomerSSN()");

            }
            return accts;
        }

3 个答案:

答案 0 :(得分:3)

您未将import UIKit class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() addTableViewConstraints() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func addTableViewConstraints() { tableView.setTranslatesAutoresizingMaskIntoConstraints(false) var topConstraint = NSLayoutConstraint(item: self.tableView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 20) var leadingContraint = NSLayoutConstraint(item: self.tableView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0) var trailingConstraint = NSLayoutConstraint(item: self.tableView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0) var bottomConstraint = NSLayoutConstraint(item: self.tableView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0) self.view.addConstraints([topConstraint, leadingContraint, trailingConstraint, bottomConstraint]) } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 5 } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell cell.textLabel?.text = "Cell" return cell } } 添加到acct列表中。

Account

答案 1 :(得分:1)

您未将acct添加到accts列表中!

                while (dr.Read())
                {
                    Account acct = new Account
                    {
                        OriginalAcctNumber = dr["TABOAT_ACCOUNT_NUMBER"].ToString().TrimStart('0'),
                        FTBOATAcctNumber = dr["FTBOAT_ACCOUNT_NUMBER"].ToString().TrimStart('0'),
                        SSN = CustomerSSN.Substring(CustomerSSN.Length -4).PadLeft(CustomerSSN.Length, '*'),
                        CustomerName = dr["CUST_NAME"].ToString(),
                        ProductType = dr["TRGT_PROD"].ToString()
                    };
                    accts.Add(acct); // ADD THIS HERE
                }

答案 2 :(得分:1)

所有取决于你想要怎么做,你也可以通过以下方式返回IEnumerable:

public static IEnumerable<Account> GetAccountNumbersFromCustomerSSN
 (string CustomerSSN)
        {

            List<Account> accts = new List<Account>();

            try
            {
                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings
                 ["TA_connectionstring"].ConnectionString))
                {
                    SqlCommand cmd = new SqlCommand("spGet_Account_Numbers_From_SSN", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    con.Open();

                    SqlParameter P1 = new SqlParameter
                       ("@CUSTOMER_SSN", DbType.String);
                    P1 = new SqlParameter
                       ("@CUSTOMER_SSN", DbType.String);
                    P1.Value = CustomerSSN;
                    cmd.Parameters.Add(P1);

                    SqlDataReader dr = cmd.ExecuteReader();

                    while (dr.Read())
                    {
                        yield return new Account
                        {
                            OriginalAcctNumber = 
                  dr["TABOAT_ACCOUNT_NUMBER"].ToString().TrimStart('0'),
                            FTBOATAcctNumber = 
                  dr["FTBOAT_ACCOUNT_NUMBER"].ToString().TrimStart('0'),
                            SSN = 
                  CustomerSSN.Substring(CustomerSSN.Length -4).PadLeft(CustomerSSN.Length, '*'),
                            CustomerName = 
                  dr["CUST_NAME"].ToString(),
                            ProductType = 
                  dr["TRGT_PROD"].ToString()
                        };
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                ExceptionUtility.LogException(ex, "GetCustomerSSN()");

            }
        }