---
title: "code optimized in c#"  
description: "code optimized in c#"  
author: "Anonymous User"  
published: 2012-11-16  
updated: 2012-11-19  
canonical: https://www.mindstick.com/forum/465/code-optimized-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# code optimized in c#

hello every one,\
\

```
public static int SetSelectedItem(string[] sValues)        {            int nRow = 0;            try            {                foreach (string str in sValues)                {                    SqlCommand cmdUpdate = new SqlCommand("UPDATE [dbo].[tblProductsManager] SET IsActive = 1 WHERE Id= '" + str + "' ", con);                    if (con.State == ConnectionState.Closed)                        con.Open();                    nRow = cmdUpdate.ExecuteNonQuery();                }            }            catch { }            finally            {                if (con.State == ConnectionState.Open)                    con.Close();            }            return nRow;        }
```

\

in the above [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii), "cmdUpdate.ExecuteNonQuery();" call for every [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) of "sValues".\

i want, "cmdUpdate.ExecuteNonQuery();" [execute](https://www.mindstick.com/interview/49/how-can-i-execute-a-php-script-using-command-line) only one time for all [array](https://www.mindstick.com/articles/335/jagged-array-in-c-sharp-dot-net) value.\
if have any idea, plz help me.

## Replies

### Reply by Anonymous User

\
\
It resolve my problem\
\
Thanks

### Reply by Anonymous User

Hi Goti Bandhu,\
You can use StringBuilder concept here, replace your code with following code:\

```
public static int SetSelectedItem(string[] sValues)        {            int nRow = 0;            try            {               StringBuilder sqlQuery = new StringBuilder ();               sqlQuery.Append("UPDATE [dbo].[tblProductsManager] SET IsActive = 1 WHERE Id in '('");          foreach (string str in sValues)                {                    sqlQuery.Append(str+"'," );                                    }               sqlQuery.Append("')" );              SqlCommand cmdUpdate = new SqlCommand ( sqlQuery.ToString(), con);                   if (con.State == ConnectionState.Closed)                         con.Open();                     nRow = cmdUpdate.ExecuteNonQuery();            }            catch { }            finally            {                if (con.State == ConnectionState.Open)                    con.Close();            }            return nRow;        }
```

Please mark as answer if this code will resolve your problem.


---

Original Source: https://www.mindstick.com/forum/465/code-optimized-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
