0
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnnFinalProject"].ToString());
SqlCommand cmd = new SqlCommand(@"SELECT c.partID, p.name, p.categoryID, p.price, p.image, p.subCategoryID, 
                                        (CASE WHEN categoryID = 1 THEN 'Y' ELSE 'N' END) AS casesYN,
                                        (CASE WHEN categoryID = 3 THEN 'Y' ELSE 'N' END) AS OSYN,
                                        (CASE WHEN categoryID = 7 THEN 'Y' ELSE 'N' END) AS HDDYN,
                                        (CASE WHEN subCategoryID = 1 THEN 'Y' ELSE 'N' END) AS powerYN,
                                        (CASE WHEN subCategoryID = 2 THEN 'Y' ELSE 'N' END) AS processorYN,
                                        (CASE WHEN subCategoryID = 3 THEN 'Y' ELSE 'N' END) AS moboYN,
                                        (CASE WHEN subCategoryID = 4 THEN 'Y' ELSE 'N' END) AS memoryYN,
                                        (CASE WHEN subCategoryID = 5 THEN 'Y' ELSE 'N' END) AS graphicsYN,
                                        (CASE WHEN subCategoryID = 6 THEN 'Y' ELSE 'N' END) AS opticalYN,
                                        (CASE WHEN subCategoryID = 7 THEN 'Y' ELSE 'N' END) AS soundYN
                                        FROM configuration c JOIN parts p ON p.partID = c.partID
                                        WHERE c.customID = @ID", conn);
      conn.Open();
      SqlDataReader dr = cmd.ExecuteReader();
      while (dr.Read())
      {
        string[] cases = { dr["partID"].ToString() }; 
      }

How can I insert into the cases array only the 'partID's that have 'casesYN' value set to Y, as it is now it would insert every 'partID' in the sql query.

1 Answer 1

6

I assumed you don't want to change your SQL query.

Use List<string> (requires using System.Collections.Generic) instead of string[] because it will be easier to add new item into it:

var cases = new List<string>();
while (dr.Read())
{
    if(dr["casesYN"].ToString() == "Y")
        cases.Add(dr["partID"].ToString()); 
}

If you really need an array, you can always call ToArray() method after the loop is finished:

string[] casesArray = cases.ToArray();

But it will require using System.Linq at the top of your file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.