0

パラメータが追加されているコードをステップ実行すると、SQLコマンドが実行されたときに、このエラーがスローされます

  public static DataTable GetPostings2(string AssetNumberV, string PeriodFromV, string PeriodToV)
    {
        DataTable dtGetPostings2;
        try
        {
            dtGetPostings2 = new DataTable("GetPostings");

            SqlParameter AssetNumber = new SqlParameter("@AssetNumber", SqlDbType.VarChar, 6);
            AssetNumber.Value = AssetNumberV;

            SqlParameter PeriodFrom = new SqlParameter("@PeriodFrom", SqlDbType.VarChar, 6);
            PeriodFrom.Value = PeriodFromV; 

            SqlParameter PeriodTo = new SqlParameter("@PeriodTo", SqlDbType.VarChar, 6);
            PeriodTo.Value = PeriodToV; 

            SqlCommand scGetPostings2 = new SqlCommand("SELECT * FROM [POSTING] WHERE [ASSET_NO] = @AssetNumber And PERIOD >= @PeriodFrom AND PERIOD <= @PeriodTo ORDER by PERIOD, JOUR_REF, JOUR_LINE", DataAccess.AssetConnection);

            SqlDataAdapter sdaGetPostings2 = new SqlDataAdapter();
            sdaGetPostings2.SelectCommand = scGetPostings2;
            sdaGetPostings2.Fill(dtGetPostings2);

            return dtGetPostings2;

        }
        catch (Exception ex)
        {
            MessageBox.Show("Error Retriving Posting Details: Processed with this error:" + ex.Message);
            return null;
        }
    }
4

1 に答える 1

3

リストされているコードは、パラメーターをSqlCommand.Parameters コレクションに追加しません。例えば:

SqlCommand scGetPostings2 = new SqlCommand("SELECT * FROM [POSTING] WHERE [ASSET_NO] = @AssetNumber And PERIOD >= @PeriodFrom AND PERIOD <= @PeriodTo ORDER by PERIOD, JOUR_REF, JOUR_LINE", DataAccess.AssetConnection);
scGetPostings2.Parameters.Add(AssetNumber);
scGetPostings2.Parameters.Add(PeriodFrom );
scGetPostings2.Parameters.Add(PeriodTo );

// Code continues as above
SqlDataAdapter sdaGetPostings2 = new SqlDataAdapter();
// And so on
于 2012-09-17T11:28:38.777 に答える