Mr Anderson

Mr Anderson

  • NA
  • 28
  • 570

Values Not being passed Correctly

Sep 20 2017 11:06 AM
The issue I am having is the values from are not being passed into my array correctly. I've done a Response.Write to check and I have the correct data. I can not seem to get this to pass into my array or the database correctly.My goal is to pass ID and P into an array and in the for loop through them then output the corresponding data. What is showing up for P is 0. I am using razor C# without MVC.
  1. else if (Page == "2")  
  2. {  
  3.     int foo;  
  4.     string paramName = "";  
  5.   
  6.   
  7.     using (SqlConnection connect = new SqlConnection(conn.strConn))  
  8.     {  
  9.         connect.Open();  
  10.         using (SqlCommand comm = new SqlCommand(conn.SQL, connect))  
  11.         {  
  12.             using (SqlDataReader reader = comm.ExecuteReader())  
  13.             {  
  14.                 reader.Read();  
  15.                 foo = Convert.ToInt32(reader["bar"]);  
  16.             }  
  17.         }  
  18.     }  
  19.   
  20.     string[] ID = new string[foo];  
  21.     double[] P  = new double[foo];  
  22.   
  23.     i = 0;  
  24.   
  25.     if (!HttpContext.Current.Request["bar"].IsEmpty())  
  26.     {  
  27.         foreach (var selection in HttpContext.Current.Request["bar"].Split(','))  
  28.         {  
  29.             IDs[i] = selection.ToString();  
  30.             ID = selection.ToString();  
  31.             paramName = "P" + ID;  
  32.             Ps[i] = Convert.ToDouble(HttpContext.Current.Request[paramName]);  
  33.             P = Convert.ToDouble(HttpContext.Current.Request[paramName]);  
  34.             i++;  
  35.   
  36.             //insert into Table  
  37.             conn.SQL = "Insert Into Table (ID, Date, Number ) Values (@ID, @Dt, @Nm)";  
  38.   
  39.             using (SqlConnection connection = new SqlConnection(conn.strConn))  
  40.             {  
  41.                 connection.Open();  
  42.                 using (SqlCommand comm = new SqlCommand(conn.SQL, connection))  
  43.                 {  
  44.   
  45.                     comm.Parameters.AddWithValue("@ID", ID);  
  46.                     comm.Parameters.AddWithValue("@Dt", DateTime.Now);  
  47.                     comm.Parameters.AddWithValue("@Nm", Math.Round(P, 2));  
  48.   
  49.   
  50.                     comm.ExecuteNonQuery();  
  51.                 }  
  52.             }  
  53.         }    
  54.             conn.SQL = "Select Name, Name1 From Table2 Where ID = @ID";  
  55.   
  56.         "0">  
  57.             class="d0">  
  58.                 "250px">Name  
  59.                 Data  
  60.               
  61.             @for (int x = 0; x < i; x++)  
  62.             {  
  63.                 using (SqlConnection connection = new SqlConnection(conn.strConn))  
  64.                 {  
  65.                     connection.Open();  
  66.                     using (SqlCommand comm = new SqlCommand(conn.SQL, connection))  
  67.                     {  
  68.                         comm.Parameters.AddWithValue("@ID", IDs[x]);  
  69.                         using (SqlDataReader reader = comm.ExecuteReader())  
  70.                         {  
  71.                             reader.Read();  
  72.   
  73.                             class="d@((x+1)%2)">  
  74.                                 @(reader["Name"] + " " + reader["Name1"])  
  75.                                 @Ps[x].ToString("c")  
  76.   
  77.                               
  78.                             //Response.Write(@Ps[x]);  
  79.                         }  
  80.                     }  
  81.                 }  
  82.             }  
  83.           
  84.   
  85.     }  
  86. }

Answers (2)