0
  1. これは必要な画像です。このページにある送信ボタンで、名前、従業員コード、電子メール ID を介して複数の値を挿入したいと考えています。

  2. 私のデータベースには、次のフィールド ID (主キー)、名前 (varchar 50)、従業員コード (varchar 50)、電子メール ID (varchar 50) が含まれています。

  3. データ層では、次のようにコーディングします。

    public static bool AddParticipantlistemployeecode(DimensionQuestion dimension)
       {
           bool result;
    
    
           using (var helper = new DbHelper())
           {
               _cmdtext = "sp_NewGeneratedUniqueCode";
    
               var success = new SqlParameter("@Success", SqlDbType.Bit, 1,    ParameterDirection.Output, true, 0, 0,
                                              "Result", DataRowVersion.Default, 0);
    
               foreach (string s in dimension.CandidateName)
               {
                   if (s.Trim().Length > 0)
                   {
                       var parameter = new[]
                                    {
                                       // new   SqlParameter("@CompanyName",dimension.CompanyName  ),
                                        new SqlParameter("@CandidateName",s  ),
                                        new SqlParameter("@EmployeeCode",s  ),
                                        new SqlParameter("@EmailId",s  ),                                                                             
                                        success,
                                    };
                       helper.ExecuteScalar(_cmdtext, CommandType.StoredProcedure, parameter);
                   }
               }
    
               result = (bool)success.Value;
           }
    
    
           return result;
       }
    
  4. モデル層で:

    using System.Text;
    using System.Collections.Generic;
    namespace Cengrow.Survey.Core.Model
    {
    public class DimensionQuestion
    {
        public string CompanyName { get; set; }
       // public string DimensionNumber { get; set; }
        public List<string> CandidateName { get; set; }
        public List<string> EmployeeCode { get; set; }
        public List<string> EmailId { get; set; }
        public int DimensionName { get; set; }
        public string Section { get; set; }
        //public string Rating { get; set; }
        public List<string> Questions { get; set; }
        //public string Question2 { get; set; }
        //public string Question3 { get; set; }
        //public string Question4 { get; set; }
        //public string Question5 { get; set; }
        //public string Question6 { get; set; }
        //public string Question7 { get; set; }
        //public string Question8 { get; set; }
        //public string Question9 { get; set; }
        //public string Question10 { get; set; }
        //public string Question11 { get; set; }
        //public string Question12 { get; set; }
        //public string Question13 { get; set; }
        //public string Question14 { get; set; }
        //public string Question15 { get; set; }
    
    }
    }
    
  5. そして最後に、ボタンクリックのビジネスロジックで:

    protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                FillObjects();
    
                //if (InsertData.InsertCandidateCompany(_CandidateCompanyInformation )) 
                if (InsertData.AddParticipantlistemployeecode(_DimensionQuestion))
                {
                    ShowMessage("Information is saved");
                    //Reset();
    
                }
                else
                {
                    ShowMessage("Please try again");
    
                }
            }
            finally
            {
                _DimensionQuestion = null;
    
            }
        }
    
     private void FillObjects()
        {
            List<string> sp = new List<string>();
            _DimensionQuestion = new Cengrow.Survey.Core.Model.DimensionQuestion();
           // _DimensionQuestion.CompanyName = txtCompanyName.Text.Trim();
    
    
            sp.Add(txtName1.Text.Trim());
            sp.Add(txtName2.Text.Trim());
            sp.Add(txtName3.Text.Trim());
            sp.Add(txtName4.Text.Trim());
            sp.Add(txtName5.Text.Trim());
            sp.Add(txtName6.Text.Trim());
            sp.Add(txtName7.Text.Trim());
            sp.Add(txtName8.Text.Trim());
            sp.Add(txtName9.Text.Trim());
            sp.Add(txtName10.Text.Trim());
            sp.Add(txtName11.Text.Trim());
            sp.Add(txtName12.Text.Trim());
            sp.Add(txtName13.Text.Trim());
            sp.Add(txtName14.Text.Trim());
            sp.Add(txtName15.Text.Trim());
            sp.Add(txtName16.Text.Trim());
            sp.Add(txtName17.Text.Trim());
            sp.Add(txtName18.Text.Trim());
            sp.Add(txtName19.Text.Trim());
            sp.Add(txtName20.Text.Trim());
            sp.Add(txtName21.Text.Trim());
            sp.Add(txtName22.Text.Trim());
            sp.Add(txtName23.Text.Trim());
            sp.Add(txtName24.Text.Trim());
            sp.Add(txtName25.Text.Trim());
            _DimensionQuestion.CandidateName = sp;
    
            sp.Add(txtEmployeeCode1.Text.Trim());
            sp.Add(txtEmployeeCode2.Text.Trim());
            sp.Add(txtEmployeeCode3.Text.Trim());
            sp.Add(txtEmployeeCode4.Text.Trim());
            sp.Add(txtEmployeeCode5.Text.Trim());
            sp.Add(txtEmployeeCode6.Text.Trim());
            sp.Add(txtEmployeeCode7.Text.Trim());
            sp.Add(txtEmployeeCode8.Text.Trim());
            sp.Add(txtEmployeeCode9.Text.Trim());
            sp.Add(txtEmployeeCode10.Text.Trim());
            sp.Add(txtEmployeeCode11.Text.Trim());
            sp.Add(txtEmployeeCode12.Text.Trim());
            sp.Add(txtEmployeeCode13.Text.Trim());
            sp.Add(txtEmployeeCode14.Text.Trim());
            sp.Add(txtEmployeeCode15.Text.Trim());
            sp.Add(txtEmployeeCode16.Text.Trim());
            sp.Add(txtEmployeeCode17.Text.Trim());
            sp.Add(txtEmployeeCode18.Text.Trim());
            sp.Add(txtEmployeeCode19.Text.Trim());
            sp.Add(txtEmployeeCode20.Text.Trim());
            sp.Add(txtEmployeeCode21.Text.Trim());
            sp.Add(txtEmployeeCode22.Text.Trim());
            sp.Add(txtEmployeeCode23.Text.Trim());
            sp.Add(txtEmployeeCode24.Text.Trim());
            sp.Add(txtEmployeeCode25.Text.Trim());
            _DimensionQuestion.EmployeeCode = sp;
    
            sp.Add(TextBox1.Text.Trim());
            sp.Add(TextBox2.Text.Trim());
            sp.Add(TextBox3.Text.Trim());
            sp.Add(TextBox4.Text.Trim());
            sp.Add(TextBox5.Text.Trim());
            sp.Add(TextBox6.Text.Trim());
            sp.Add(TextBox7.Text.Trim());
            sp.Add(TextBox8.Text.Trim());
            sp.Add(TextBox9.Text.Trim());
            sp.Add(TextBox10.Text.Trim());
            sp.Add(TextBox11.Text.Trim());
            sp.Add(TextBox12.Text.Trim());
            sp.Add(TextBox13.Text.Trim());
            sp.Add(TextBox14.Text.Trim());
            sp.Add(TextBox15.Text.Trim());
            sp.Add(TextBox16.Text.Trim());
            sp.Add(TextBox17.Text.Trim());
            sp.Add(TextBox18.Text.Trim());
            sp.Add(TextBox19.Text.Trim());
            sp.Add(TextBox20.Text.Trim());
            sp.Add(TextBox21.Text.Trim());
            sp.Add(TextBox22.Text.Trim());
            sp.Add(TextBox23.Text.Trim());
            sp.Add(TextBox24.Text.Trim());
            sp.Add(TextBox25.Text.Trim());
            _DimensionQuestion.EmailId  = sp;
         }
    

データがデータベースに適切に入力されていません

4

1 に答える 1

0

3 つのパラメーターすべてに同じ値を追加しています。

new SqlParameter("@CandidateName",s  ),
new SqlParameter("@EmployeeCode",s  ),
new SqlParameter("@EmailId",s  

すべての場合において、CandidateName である「s」を追加しています。

編集:候補はクラスでなければなりません:

public class Candidate
{
    public string Name { get; set; }
    public string EmployeeCode { get; set; }
    public string Email { get; set; }

    public Candidate(string name, string code, string email)
    {
        this.Name = name;
        this.EmployeeCode = code;
        this.Email = email;
    }
}

次に、DimensionQuestion には

List<Candidate> Candidates 

それ以外の

public List<string> CandidateName { get; set; }
public List<string> EmployeeCode { get; set; }
public List<string> EmailId { get; set; }

次に、FillObjects メソッドで Candidate オブジェクトを Candidate リストに追加します。

Candidates.Add(new Candidate(txtName1.Text.Trim(), 
                                txtEmployeeCode1.Text.Trim(), 
                                TextBox1.Text.Trim()));


Candidates.Add(new Candidate(txtName2.Text.Trim(), 
                                txtEmployeeCode2.Text.Trim(), 
                                TextBox2.Text.Trim()));

最後に、データレイヤーは次のようになります。

foreach (Candidate candidate in dimension.Candidates)
           {
               if (!String.IsNullOrEmpty(candidate.Name))
               {
                   var parameter = new[]
                                {
                                   // new   SqlParameter("@CompanyName",dimension.CompanyName  ),
                                    new SqlParameter("@CandidateName",candidate.Name  ),
                                    new SqlParameter("@EmployeeCode",candidate.Code  ),
                                    new SqlParameter("@EmailId",candidate.Email  ),                                                                             
                                    success,
                                };
                   helper.ExecuteScalar(_cmdtext, CommandType.StoredProcedure, parameter);
               }
           }
于 2012-09-12T10:35:18.643 に答える