Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Update all gridview rows at once

Update all gridview rows at once

Scheduled Pinned Locked Moved ASP.NET
helpannouncement
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    amina89
    wrote on last edited by
    #1

    Hi, when I execute this code i get the following error:Incorrect syntax near the keyword When.

    StringBuilder sb = new StringBuilder();
    foreach (GridViewRow row in GridView1.Rows)
    {

            string note1 = (row.FindControl("note1") as TextBox).Text.Replace(",", ".");
            string note2 = (row.FindControl("note2") as TextBox).Text.Replace(",", ".");
            string userID = (row.FindControl("lblUserID1") as Label).Text;
    
            sb.AppendFormat(
                @"UPDATE EXAMEN
             SET NOTE = case  when 
    

    CODE_MATIERE ='111' then {0}

         when   CODE\_MATIERE ='112' then {1}
          
         END
             where NUM\_INSCRIPTION  = {2}
         ;
         ", note1, note2, userID);
    
        }
    

    </pre>

    G 1 Reply Last reply
    0
    • A amina89

      Hi, when I execute this code i get the following error:Incorrect syntax near the keyword When.

      StringBuilder sb = new StringBuilder();
      foreach (GridViewRow row in GridView1.Rows)
      {

              string note1 = (row.FindControl("note1") as TextBox).Text.Replace(",", ".");
              string note2 = (row.FindControl("note2") as TextBox).Text.Replace(",", ".");
              string userID = (row.FindControl("lblUserID1") as Label).Text;
      
              sb.AppendFormat(
                  @"UPDATE EXAMEN
               SET NOTE = case  when 
      

      CODE_MATIERE ='111' then {0}

           when   CODE\_MATIERE ='112' then {1}
            
           END
               where NUM\_INSCRIPTION  = {2}
           ;
           ", note1, note2, userID);
      
          }
      

      </pre>

      G Offline
      G Offline
      Geoff Williams
      wrote on last edited by
      #2

      You need to enclose your CASE statement in brackets:

      UPDATE EXAMEN
      SET NOTE =
      ( CASE
      WHEN CODE_MATIERE = '111' THEN '{0}'
      WHEN CODE_MATIERE = '112' THEN '{1}'
      END )
      WHERE NUM_INSCRIPTION = {2};

      I would also recommend using a parameterised update query - that way you only need to create the query once and you can pass the row-specific values (note1, note2, userId) as parameters.

      P 1 Reply Last reply
      0
      • G Geoff Williams

        You need to enclose your CASE statement in brackets:

        UPDATE EXAMEN
        SET NOTE =
        ( CASE
        WHEN CODE_MATIERE = '111' THEN '{0}'
        WHEN CODE_MATIERE = '112' THEN '{1}'
        END )
        WHERE NUM_INSCRIPTION = {2};

        I would also recommend using a parameterised update query - that way you only need to create the query once and you can pass the row-specific values (note1, note2, userId) as parameters.

        P Offline
        P Offline
        phil o
        wrote on last edited by
        #3

        You cannot have CR/LF in strings. Thus you have to concatenate your strings if you want to write them on several lines :

        sb.AppendFormat(
        @"UPDATE EXAMEN "

        • "SET NOTE = "
        • "(CASE "
        • "WHEN CODE_MATIERE = '111' then {0} "
        • "WHEN CODE_MATIERE = '112' then {1} "
        • "END) "
        • "WHERE NUM_INSCRIPTION = {2};"
          , note1, note2, userID);

        An even better option would be to use a parameterized query :

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "UPDATE EXAMEN "

        • "SET NOTE = "
        • "(CASE "
        • "WHEN CODE_MATIERE = '111' then @note1 "
        • "WHEN CODE_MATIERE = '112' then @note2 "
        • "END) "
        • "WHERE NUM_INSCRIPTION = @numinsc;"
          SqlParameter param1 = new SqlParameter("note1", note1);
          cmd.Parameters.Add(param1);
          SqlParameter param2 = new SqlParameter("note2", note2);
          cmd.Parameters.Add(param2);
          SqlParameter param3 = new SqlParameter("numinsc", userID);
          cmd.Parameters.Add(param3);
          ...

        Edit : I replied to the wrong post. Sorry :(

        A 1 Reply Last reply
        0
        • P phil o

          You cannot have CR/LF in strings. Thus you have to concatenate your strings if you want to write them on several lines :

          sb.AppendFormat(
          @"UPDATE EXAMEN "

          • "SET NOTE = "
          • "(CASE "
          • "WHEN CODE_MATIERE = '111' then {0} "
          • "WHEN CODE_MATIERE = '112' then {1} "
          • "END) "
          • "WHERE NUM_INSCRIPTION = {2};"
            , note1, note2, userID);

          An even better option would be to use a parameterized query :

          SqlCommand cmd = new SqlCommand();
          cmd.CommandText = "UPDATE EXAMEN "

          • "SET NOTE = "
          • "(CASE "
          • "WHEN CODE_MATIERE = '111' then @note1 "
          • "WHEN CODE_MATIERE = '112' then @note2 "
          • "END) "
          • "WHERE NUM_INSCRIPTION = @numinsc;"
            SqlParameter param1 = new SqlParameter("note1", note1);
            cmd.Parameters.Add(param1);
            SqlParameter param2 = new SqlParameter("note2", note2);
            cmd.Parameters.Add(param2);
            SqlParameter param3 = new SqlParameter("numinsc", userID);
            cmd.Parameters.Add(param3);
            ...

          Edit : I replied to the wrong post. Sorry :(

          A Offline
          A Offline
          amina89
          wrote on last edited by
          #4

          Hi; Thak you phil.o :) I tried using a parameterized query but it does not work:It update with zero all marks foreach (GridViewRow row in GridView1.Rows) { TextBox notem1 = row.FindControl("note1") as TextBox; TextBox notem2 = row.FindControl("note2") as TextBox; Label num = row.FindControl("lblUserID1") as Label; SqlConnection myConnection = new SqlConnection(ConnectionString); SqlCommand myCommand = new SqlCommand( "UPDATE EXAMEN " + "SET NOTE = " + "(CASE " + "WHEN CODE_MATIERE = '111' then @note1 " + "WHEN CODE_MATIERE = '112' then @note2 " + "END) " + "WHERE NUM_INSCRIPTION = @num;" , myConnection); myCommand.Parameters.Add(new SqlParameter("@num", SqlDbType.VarChar, 4)); myCommand.Parameters["@num"].Value = num.Text; myCommand.Parameters.Add(new SqlParameter("@note1", SqlDbType.Float, 2)); myCommand.Parameters["@note1"].Value = notem1.Text.Replace(",", "."); myCommand.Parameters.Add(new SqlParameter("@note2", SqlDbType.Float, 2)); myCommand.Parameters["@note2"].Value = notem2.Text.Replace(",", "."); myConnection.Open(); myCommand.ExecuteNonQuery(); myConnection.Close(); }

          modified on Thursday, October 14, 2010 6:21 AM

          P 1 Reply Last reply
          0
          • A amina89

            Hi; Thak you phil.o :) I tried using a parameterized query but it does not work:It update with zero all marks foreach (GridViewRow row in GridView1.Rows) { TextBox notem1 = row.FindControl("note1") as TextBox; TextBox notem2 = row.FindControl("note2") as TextBox; Label num = row.FindControl("lblUserID1") as Label; SqlConnection myConnection = new SqlConnection(ConnectionString); SqlCommand myCommand = new SqlCommand( "UPDATE EXAMEN " + "SET NOTE = " + "(CASE " + "WHEN CODE_MATIERE = '111' then @note1 " + "WHEN CODE_MATIERE = '112' then @note2 " + "END) " + "WHERE NUM_INSCRIPTION = @num;" , myConnection); myCommand.Parameters.Add(new SqlParameter("@num", SqlDbType.VarChar, 4)); myCommand.Parameters["@num"].Value = num.Text; myCommand.Parameters.Add(new SqlParameter("@note1", SqlDbType.Float, 2)); myCommand.Parameters["@note1"].Value = notem1.Text.Replace(",", "."); myCommand.Parameters.Add(new SqlParameter("@note2", SqlDbType.Float, 2)); myCommand.Parameters["@note2"].Value = notem2.Text.Replace(",", "."); myConnection.Open(); myCommand.ExecuteNonQuery(); myConnection.Close(); }

            modified on Thursday, October 14, 2010 6:21 AM

            P Offline
            P Offline
            phil o
            wrote on last edited by
            #5

            Hi, Try to use the correct types when you assign your parameter : here you define float parameters but you assign them string values. Try instead :

            string cmd = "UPDATE EXAMEN SET NOTE = (CASE WHEN CODE_MATIERE = '111' then @note1 WHEN CODE_MATIERE = '112' then @note2 END) WHERE NUM_INSCRIPTION = @num;";
            float note1, note2;
            string num;
            SqlCommand myCommand;
            SqlParameter param1, param2, param3;

            using (SqlConnection myConnection = new SqlConnection(ConnectionString))
            {
            myConnection.Open();

            foreach (GridViewRow row in GridView1.Rows)
            {
            note1 = float.Parse(((TextBox)row.FindControl("note1")).Text);
            note2 = float.Parse(((TextBox)row.FindControl("note2")).Text);
            num = ((Label)row.FindControl("lblUserID1")).Text;

              myCommand = new SqlCommand(cmd, myConnection);
            
              param1 = new SqlParameter("num", SqlDbType.VarChar, 4);
              param1.Value = num;
              myCommand.Parameters.Add(param1);
            
              param2 = new SqlParameter("note1", SqlDbType.Float, 2);
              param2.Value = note1;
              myCommand.Parameters.Add(param2);
            
              param3 = new SqlParameter("note2", SqlDbType.Float, 2);
              param3.Value = note2;
              myCommand.Parameters.Add(param3);
            
              myCommand.ExecuteNonQuery();
            

            }

            myConnection.Close();
            }

            Several points here : - Try to obtain your values in the type you will need them in your database (note1, note2, num). - I removed the concatenations in the query string for higher simplicity. - When you define your parameters, defines variables of type SqlParameter (as in my previous example) ; it will exhaust you from having to find them out using an array accessor. - The '@' sign for parameters must only be present in the SQL query string, not in the declaration of the SqlParameter objects. - It's better to open your connection before the loop, and close it after ; opening connection is time-consuming, and you'd better avoid it if you can. And, as SqlConnection implements IDisposable interface, it's even better to use it with an 'using' statement (which will take care of disposing the connection when not needed anymore). If this still doesn't work, start a debug session and watch for the content of all your variables, you should see there what's the issue. Hope it'll help ;)

            A 1 Reply Last reply
            0
            • P phil o

              Hi, Try to use the correct types when you assign your parameter : here you define float parameters but you assign them string values. Try instead :

              string cmd = "UPDATE EXAMEN SET NOTE = (CASE WHEN CODE_MATIERE = '111' then @note1 WHEN CODE_MATIERE = '112' then @note2 END) WHERE NUM_INSCRIPTION = @num;";
              float note1, note2;
              string num;
              SqlCommand myCommand;
              SqlParameter param1, param2, param3;

              using (SqlConnection myConnection = new SqlConnection(ConnectionString))
              {
              myConnection.Open();

              foreach (GridViewRow row in GridView1.Rows)
              {
              note1 = float.Parse(((TextBox)row.FindControl("note1")).Text);
              note2 = float.Parse(((TextBox)row.FindControl("note2")).Text);
              num = ((Label)row.FindControl("lblUserID1")).Text;

                myCommand = new SqlCommand(cmd, myConnection);
              
                param1 = new SqlParameter("num", SqlDbType.VarChar, 4);
                param1.Value = num;
                myCommand.Parameters.Add(param1);
              
                param2 = new SqlParameter("note1", SqlDbType.Float, 2);
                param2.Value = note1;
                myCommand.Parameters.Add(param2);
              
                param3 = new SqlParameter("note2", SqlDbType.Float, 2);
                param3.Value = note2;
                myCommand.Parameters.Add(param3);
              
                myCommand.ExecuteNonQuery();
              

              }

              myConnection.Close();
              }

              Several points here : - Try to obtain your values in the type you will need them in your database (note1, note2, num). - I removed the concatenations in the query string for higher simplicity. - When you define your parameters, defines variables of type SqlParameter (as in my previous example) ; it will exhaust you from having to find them out using an array accessor. - The '@' sign for parameters must only be present in the SQL query string, not in the declaration of the SqlParameter objects. - It's better to open your connection before the loop, and close it after ; opening connection is time-consuming, and you'd better avoid it if you can. And, as SqlConnection implements IDisposable interface, it's even better to use it with an 'using' statement (which will take care of disposing the connection when not needed anymore). If this still doesn't work, start a debug session and watch for the content of all your variables, you should see there what's the issue. Hope it'll help ;)

              A Offline
              A Offline
              amina89
              wrote on last edited by
              #6

              Hi, Thank you very much phil. the program does not read TexBox of the gridview it returns me this message 'The format of the input string is incorrect.' for this line : note1 = float.Parse(((TextBox)row.FindControl("note1")).Text); when I debug I found that the value of note1 is 0.0 while I typed 13. Thanks.

              A 1 Reply Last reply
              0
              • A amina89

                Hi, Thank you very much phil. the program does not read TexBox of the gridview it returns me this message 'The format of the input string is incorrect.' for this line : note1 = float.Parse(((TextBox)row.FindControl("note1")).Text); when I debug I found that the value of note1 is 0.0 while I typed 13. Thanks.

                A Offline
                A Offline
                amina89
                wrote on last edited by
                #7

                Hi, the program does not read :((TextBox)row.FindControl("note2")).Text; it reads ((Label)row.FindControl("lblUserID1")).Text; I think the problem come from there but I do not know how to correct that. Thanks

                A 1 Reply Last reply
                0
                • A amina89

                  Hi, the program does not read :((TextBox)row.FindControl("note2")).Text; it reads ((Label)row.FindControl("lblUserID1")).Text; I think the problem come from there but I do not know how to correct that. Thanks

                  A Offline
                  A Offline
                  amina89
                  wrote on last edited by
                  #8

                  It works thaaaaaaaaaaaaaaaaaaanks merciiiiiiiiiiii phil.o.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups