2

I'm trying to update a SQL Server database and have a list of strings which are seat numbers

string strQuery = ("Update tblTransactionItems set selected = 'T' Where SeatNumber = @SeatNumber");

using (SqlCommand cmd = new SqlCommand(strQuery, con)) {
    con.Open();

    foreach (var item in list) {
       string test = item.ToString();
       test = test.Replace('"', ' ').Trim();

       cmd.Parameters.AddWithValue("@SeatNumber", test);
       cmd.ExecuteNonQuery();
    }
}

It keeps saying that the variable @SeatNumber has been declared and only the first item in the list is updated. Any ways on how to approach this?

Update :

I just placed the foreach loop outside the using and it works.

1
  • 1
    because you add @seatnumber each itteration of your foreach loop building the string.. Commented Nov 9, 2017 at 8:29

5 Answers 5

3

You are trying to make a inlist but using a equality operator saying Where SeatNumber = @SeatNumber" where as you should using a IN operator like

Where SeatNumber IN (@SeatNumber)

Again this is not the right way, rather you should populate those inlist values to a DataTable and pass that as DbType.Structured instead

Sign up to request clarification or add additional context in comments.

Comments

2

Its because you have already added CommandParameter to SqlCommand and you are again adding it in loop

string strQuery = ("Update tblTransactionItems set selected = 'T' Where SeatNumber = @SeatNumber");
con.Open();
foreach (var item in list) {
  using (SqlCommand cmd = new SqlCommand(strQuery, con)) {
    string test = item.ToString();
    test = test.Replace('"', ' ').Trim();
    cmd.Parameters.AddWithValue("@SeatNumber", test);
    cmd.ExecuteNonQuery();

  }
}

Comments

1

You need to initiate SqlCommand parameter every time. In your code you are using the SQLCommand to execute the query first time only and it updates your table with first item in the list. During the next iteration since the sqlcommand still holds the last value (query to update previous item from the list), it throws an error. The right way to do this is as follows:

try{
    string strQuery = ("Update tblTransactionItems set selected = 'T' Where SeatNumber = @SeatNumber");
    con.Open();
    foreach (var item in list) {
      using (SqlCommand cmd = new SqlCommand(strQuery, con)) {
    string test = item.ToString();
    test = test.Replace('"', ' ').Trim();
    cmd.Parameters.AddWithValue("@SeatNumber", test);
    cmd.ExecuteNonQuery();

      }
    }
}
finally{ con.Close() }

Comments

1

You can add one more clear parameter line to make it run.

string strQuery = ("Update tblTransactionItems set selected = 'T' Where SeatNumber = @SeatNumber");

    using (SqlCommand cmd = new SqlCommand(strQuery, con)) {
    con.Open();
    foreach (var item in list) {
        string test = item.ToString();
        test = test.Replace('"', ' ').Trim();
        cmd.Parameters.AddWithValue("@SeatNumber", test);
        cmd.ExecuteNonQuery();
        cmd.Parameters.Clear();
    }

Comments

0

I just placed the foreach loop outside the using and it works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.