开发者

datareader returning empty with parameter?

开发者 https://www.devze.com 2023-03-15 12:44 出处:网络
I\'m using a select where field.table =\" + foo; query, and returning no data. Confused as to why. publicSubmission GetSubmissionsByID(string x)

I'm using a select where field.table =" + foo; query, and returning no data. Confused as to why.

public  Submission GetSubmissionsByID(string x)
{


      string viewQuery = "SELECT Submission.SubmissionId, Submission.CustId, Submission.BroId, Submission.Coverage, Submission.CurrentCoverage, Submission.PrimEx, Submission.Retention, Submission.EffectiveDate, Submission.Commission, Submission.Premium, Submission.Comments FROM Submission WHERE Submission.SubmissionId =" + x;
      string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
      using (SqlConnection conn = new SqlConnection(connectionString))
      {
          conn.Open();

          SqlCommand viewCmd = new SqlCommand(viewQuery, conn);
          SqlDataReader dr = null;
          dr = viewCmd.ExecuteReader();
          Submission tempSubmissi开发者_如何学Goon = new Submission();

          tempSubmission.SubmissionId1 = dr.GetInt32(0);
          tempSubmission.CustomerId1 = dr.GetInt32(1);
          tempSubmission.BrokerId1 = dr.GetInt32(2);
          tempSubmission.Coverage1 = dr.GetInt32(3);
          tempSubmission.CurrentCoverage1 = dr.GetInt32(4);
          tempSubmission.PrimEx1 = dr.GetInt32(5);
          tempSubmission.Retention1 = dr.GetInt32(6);
          tempSubmission.EffectiveDate1 = dr.GetDateTime(7);
          tempSubmission.Commission1 = dr.GetInt32(8);
          tempSubmission.Premium1 = dr.GetInt32(9);
          tempSubmission.Comment1 = dr.GetString(10);

          return tempSubmission;
      }
} 

Called up here, where queried value X is valid, with data on that row. Also fails with x set to constant/valid value.

string x = Request.QueryString["SubmissionId"];

    SubmissionService ss = new SubmissionService();
    Submission sub = ss.getSubmissionByID(x);


You have to call Read() to advance to the first record.

      dr = viewCmd.ExecuteReader();
      if( dr.Read())
      {
        Submission tempSubmission = new Submission();

        tempSubmission.SubmissionId1 = dr.GetInt32(0);
        tempSubmission.CustomerId1 = dr.GetInt32(1);
        tempSubmission.BrokerId1 = dr.GetInt32(2);
        tempSubmission.Coverage1 = dr.GetInt32(3);
        tempSubmission.CurrentCoverage1 = dr.GetInt32(4);
        tempSubmission.PrimEx1 = dr.GetInt32(5);
        tempSubmission.Retention1 = dr.GetInt32(6);
        tempSubmission.EffectiveDate1 = dr.GetDateTime(7);
        tempSubmission.Commission1 = dr.GetInt32(8);
        tempSubmission.Premium1 = dr.GetInt32(9);
        tempSubmission.Comment1 = dr.GetString(10);

        return tempSubmission;
      }
      return null;
0

精彩评论

暂无评论...
验证码 换一张
取 消