title: How to pass a JavaScript array to C# function using Ajax
body: <p>i have array of strings in JavaScript passed on the button click, here on button click the dataArray stores string value of first elements from the table in which some of the rows has been selected after that i <code>stringify</code> as JSON and call a Ajax function to send the data to my code behind function DeleteStudent.<br>
  my JavaScript function that i call on button click:</p>
  
  <pre><code>$('#deleteStudent').click(function () {
              var dataArr = [];
              $.each($("#StudentTable tr.selected"), function () {
                  dataArr.push($(this).find('td').eq(0).text()); 
              });
              var StudentList = JSON.stringify(dataArr);
              $.ajax({
                  type: "POST",
                  url: "ViewStudents.aspx/DeleteStudent",
                  contentType: "application/json; charset=utf-8",
                  data: { Students: dataArr },
                  dataType: "json",
                  traditional: true,
                  success: function (result) {
                      alert('Yay! It worked!');
                  },
                  error: function (result) {
                      alert('Oh no :(  : '+result);
                  }
              });
              console.log(StudentList);
      });
  </code></pre>
  
  <p>the dataArray looks like this</p>
  
  <pre><code>["10363","10364","10366"]
  </code></pre>
  
  <p>the  code behind function:</p>
  
  <pre><code>[WebMethod]
  public static void DeleteStudent(string[] Students)
  {
      Console.WriteLine("Reached CS");
      string[] a =Students;
      for (int i = 0; i &lt; a.Length; i++)
      {
          string admissionNumber=a[i];
          using (MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString))
          {
              using (MySqlCommand deleteStudent = new MySqlCommand())
              {
                  deleteStudent.CommandType = CommandType.Text;
                  deleteStudent.Connection = conn;
                  deleteStudent.CommandText = "DELETE FROM validstudents WHERE admissionNumber = @admissionNumber ";
  
                  deleteStudent.Parameters.AddWithValue("@admissionNumber", admissionNumber);
  
                  conn.Open();
                  deleteStudent.ExecuteNonQuery();
                  conn.Close();
              }
          }
      }
  }
  </code></pre>
  
  <p>it gives a 500 internal server</p>