Skip to content

How to pass value from string array to Select statement?

RD Design

Asked Feb 23, 2015, 12:34 AM EST3 answers
Share
LinkedInX
Hi,

I have a List and there I have stored customerNo from database like this

                    List<string[]> _customerDetails = new List<string[]>();
                    while (readerCus.Read())
                    {
                        string[] fields = new string[2];
                        fields[0] = readerCus["Customer"].ToString();
                        _customerDetails.Add(fields);
                    }

Inside a foreach I need to get this "Customer" no one by one and pass it to SQL select statement.

foreach (string[] fields in _customerDetails)
                    {
                        string cusId = fields[0];
  using (SqlConnection con2 = new SqlConnection(connectionString))
                        {
                            con2.Open();
                            using (SqlCommand command2 = new SqlCommand("SELECT * FROM dbo.Customers WHERE Customer = '" + fields[0].ToString() + "'", con2))
                            using (SqlDataReader reader2 = command2.ExecuteReader())
                            {
 
                                while (reader2.Read())
                                {
                                        //My code and here I am generating a token for customer
                                }
                            }
                         }
                      }

My issue is all the time it runs one time and generate a token.Not continuing and not completing all available customers (say 1000 customers).Any one can point me how can I loop through all customers one by one.

Thank

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    It should return all customer Tokens as per the code one by one, so where are you getting error or what output you exactly need?
    0
  • 0
    I think there should be something in while loop.Inside while loop I am calling a webservice and receive token for each customer.When I run the code it runs 1 time (only for 1st user) generates a token and finish it.This is what I cannot figure out.

    while (reader2.Read())
      {
      TokenCheck check = new TokenCheck();
      check.CusTokenNumber = reader2["TokenNo"].ToString();
      reader2.Close();
      command2.Dispose();
      con2.Close();

     
      lblTokenNumber.Text = check.CusTokenNumber;
          //Here I am calling a PDF generating method to generate the PDF based on token
      getGeneratedPDF();
     
      }
    0
  • 1 short reply
    • 0
      I got it fixed
      0