简体   繁体   中英

Looping through arrays in asp.net is not working

I have an array which sometimes holds values like "ABC, XYZ, TTT" and sometime only "ABC" So while iterating through the arrays when it holds multiple it checks only for the first item and not the other item.

string[] strStateArray = new string[] { "" };
                    strStateArray = strOne.Split(',');

 for (int i = 0; i < strStateArray.Length; i++)
                    {                            
                        if (dt.Rows[0]["CIRCLE"].ToString() == strStateArray[i].ToString()) // not checking for multiple items
                        {
            }
        }

updated code

for (int i = 0; i < strStateArray.Length; i++)
                    {
                        if (dt.Rows[0]["CIRCLE"].ToString() == strStateArray[i].Trim().ToString())
                        {
                            if (dt.Rows.Count > 0)
                            {
                                dt.TableName = "RecodSet";
                                string xml = ConvertDatatableToXML(dt);
                                mycon.Close();

                                ScriptManager.RegisterStartupScript(this, this.GetType(), key, "alert('File uploaded successfully.!!');", true);
                                //  System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertBox", "alert('File uploaded successfully.!!');", true);
                            }
                            else
                            {
                                string noData = "No data to upload.";
                                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "script", noData, false);

                            }
                        }
                        else
                        {
                            string file_name = fluUploadBtn.FileName;

                            if ((System.IO.File.Exists(file_name)))
                            {
                                System.IO.File.Delete(file_name);
                            }
                            ScriptManager.RegisterStartupScript(this, this.GetType(), key, "alert('User is not authorised to upload data for state mentioned in excel report ');", true);
                        }                            
                    }

Could you just go debug the value then?

let me rewrite some code for you

String logMsg ="";
String targetKeyword = dt.Rows[0]["CIRCLE"].ToString();
for (int i = 0; i < strStateArray.Length; i++)
{
       String tmp_Val_Pure = strStateArray[i] ==null? null : strStateArray[i].ToString();   //debug here see the value1
       String tmp_Val = strStateArray[i] ==null? "" : strStateArray[i].ToString().Trim(); //debug here see the value2
       bool isThisOk =  (targetKeyword == tmp_Val );   //debug here see the val of isThisOk 
      if ( isThisOk  )
      {
          logMsg += "Success Record : "+ i.ToString() + " val : " + tmp_Val ; 
           if (dt.Rows.Count > 0)
            {  // alert case OK
            } else { // alert no val dt  }
      }
      else
      {  
          logMsg += "Err Record : "+ i.ToString() + " val : " + tmp_Val ;           
      }
} //end loop
//Go check logMsg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM