Question: I was wondering why the result of the following code is 0 and not 3.

```
var fn = function(){
    for (i = 0; i < 3; i++){
        return function(){
            console.log(i);
        };
    }
}();

fn();

```

Answer: 
You should do this **properly**:

* define your parameters **once** outside the loop
* define the **values** of your parameters inside the loop for each iteration
* use `using(...) { ... }` blocks to get rid of the `try ... catch ... finally` (the `using` block will ensure proper and speedy disposal of your classes, when no longer needed)
* stop using a `try...catch` if you're not actually *handling* the exceptions - just rethrowing them (makes no sense)

Try this code:

```
public static void featuresentry()
{
    string connectionString = HandVeinPattern.Properties.Settings.Default.HandVeinPatternConnectionString;
    string insertQuery = "INSERT INTO FEATURES(UserID, Angle, ClassID, Octave, PointX, PointY, Response, Size) VALUES(@UserID, @Angle, @ClassID, @Octave, @PointX, @PointY, @Response, @Size)";

    using (SqlConnection connection = new SqlConnection(connectionString))
    using (SqlCommand command = new SqlCommand(insertQuery, connection))
    {
        // define your parameters ONCE outside the loop, and use EXPLICIT typing
        command.Parameters.Add("@UserID", SqlDbType.Int);
        command.Parameters.Add("@Angle", SqlDbType.Double);
        command.Parameters.Add("@ClassID", SqlDbType.Double);
        command.Parameters.Add("@Octave", SqlDbType.Double);
        command.Parameters.Add("@PointX", SqlDbType.Double);
        command.Parameters.Add("@PointY", SqlDbType.Double);
        command.Parameters.Add("@Response", SqlDbType.Double);
        command.Parameters.Add("@Size", SqlDbType.Double);

        connection.Open();

        for (int i = 0; i < Details.modelKeyPoints.Size; i++)
        {
            // now just SET the values
            command.Parameters["@UserID"].Value = Details.ID;
            command.Parameters["@Angle"].Value = Convert.ToDouble(Details.modelKeyPoints[i].Angle);
            command.Parameters["@ClassID"].Value = Convert.ToDouble(Details.modelKeyPoints[i].ClassId);
            command.Parameters["@Octave"].Value = Convert.ToDouble(Details.modelKeyPoints[i].Octave);
            command.Parameters["@PointX"].Value = Convert.ToDouble(Details.modelKeyPoints[i].Point.X);
            command.Parameters["@PointY"].Value = Convert.ToDouble(Details.modelKeyPoints[i].Point.Y);
            command.Parameters["@Response"].Value = Convert.ToDouble(Details.modelKeyPoints[i].Response);
            command.Parameters["@Size"].Value = Convert.ToDouble(Details.modelKeyPoints[i].Size);

            command.ExecuteNonQuery();
        }
    }
}

```
You can do this by sending your data as an xml string and convert in into table in a stored procedure in sql. For example:
suppose I am sending multiple rows to add/update in an sql table then here are the steps:

1. Convert your class or list of class into an xml string using following method:

```
public static string SerializeObjectToXmlString(object value)

          {
          var emptyNamepsaces = new XmlSerializerNamespaces(new[] { 
                                    XmlQualifiedName.Empty });

    var serializer = new XmlSerializer(value.GetType());
    var settings = new XmlWriterSettings();
    settings.Indent = true;
    settings.OmitXmlDeclaration = true;

    using (var stream = new StringWriter())
    using (var writer = XmlWriter.Create(stream, settings))
    {
        serializer.Serialize(writer, value, emptyNamepsaces);
        return stream.ToString();
    }
}

```
2. Now while sending data to the database convert your class object into xml 
string (Here I am using entity framework in my code, you can do this without using it as well):

```
bool AddUpdateData(List<MyClass> data)
{
    bool returnResult = false;
    string datatXml = Helper.SerializeObjectToXmlString(data);
    var sqlparam = new List<SqlParameter>()
                 {
   new SqlParameter() { ParameterName = "dataXml", Value = datatXml}

                 };
    var result = this.myEntity.Repository<SQL_StoredProc_ComplexType>().ExecuteStoredProc("SQL_StoredProc", sqlparam);
    if (result != null && result.Count() > 0)
    {
        returnResult = result[0].Status == 1 ? true : false;
    }
    return returnResult;
}

```
3. Now your SQL Code:

3.1 Declare a table variable:

```sql
DECLARE @tableVariableName TABLE
(
    ID INT, Name VARCHAR(20)
)

```

3.2 Insert Your xml string into Table variable

```sql
INSERT INTO @tableVariableName
SELECT 
    Finaldata.R.value ('(ID/text())[1]', 'INT') AS ID, 
    Finaldata.R.value ('(Name/text())[1]', 'VARCHAR(20)') AS Name
FROM @MyInputXmlString.nodes ('//ArrayMyClass/MyClass') AS Finaldata (R)

```

3.3 Finally insert this table value into your sql table

```sql
INSERT INTO MyTable (ID, Name)                  
SELECT ID, Name          
FROM @tableVariableName

```

This will save your effort of hitting database again and again using a for loop.

Hope it will help you