SQL Datareader

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SCSrate
    New Member
    • Mar 2019
    • 1

    SQL Datareader

    front end: Visual Basic C# 2019;
    back end: SQL Server 2017;

    I am rather new to C# and could use some help. I am reading a SQL table using a SQL data reader. Here is the code

    Code:
    string strSQL = "select * from proddta.f4311 where pdan8='" + supplierNo + "' AND PDDRQJ>='" + jdeStart + "' AND PDDRQJ<='" + jdeEnd + "' order by pddrqj";
    SqlConnection con = new SqlConnection(SR_SupplierRatings.SQLTables.csJDE);
                con.Open();
                SqlCommand cmd = new SqlCommand(strSQL, con);
                SqlDataReader dr = cmd.ExecuteReader();
    
                int myCount = 0;
                try
                {
                    while (dr.Read())
                    {
                        if (dr("PDMCU") != 372)
                        {
                            myCount += 1;
                        }
                    }
    
                    con.Close();
    I am getting a compile error that states:

    CS0149 Method name expected at this statement

    Code:
     if (dr("PDMCU") != 372)
    I cannot figure out why. Can someone help?

    Thanks.
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    Perhaps you should pull the information into a variable first and thin run the conditional...

    Code:
      while (rdr.read())
      {
        //get the field - guessing it's an integer
        Int myPDMCU = (int)rdr["PDMCU"]
        if (myPDMCU != 372)
        {
          myCount ++;
        }
      }

    Comment

    Working...