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. General Programming
  3. C#
  4. What is the return value of the insert and delete sql statement

What is the return value of the insert and delete sql statement

Scheduled Pinned Locked Moved C#
questiondatabasesql-serversysadmin
8 Posts 2 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.
  • Z Offline
    Z Offline
    Zar Ni
    wrote on last edited by
    #1

    I read that if I successfully insert one row of data or delete one row of data, the sql server will return me 1. But when I try it, it returns me -1 instead of 1 although the row of data is successfully inserted or deleted. Anyone here could enlighten me if I got something wrong? Thank you very much in advance.

               SqlConnection con = new SqlConnection(GUIInitializer.DBconnectionString);
                SqlCommand cmd = new SqlCommand(GUIInitializer.stp\_GUI\_DELETEITEMAIRLINEMAPPING, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@ITEM\_CODE", itemCode);
                con.Open();
                int rows = cmd.ExecuteNonQuery();
                con.Close();
                if (rows == -1)
                {
                    DeleteItemData(itemCode);
                }
                else
                    MessageBox.Show("Item Code: " + itemCode + " Deletion FAILED!");
    
    P 1 Reply Last reply
    0
    • Z Zar Ni

      I read that if I successfully insert one row of data or delete one row of data, the sql server will return me 1. But when I try it, it returns me -1 instead of 1 although the row of data is successfully inserted or deleted. Anyone here could enlighten me if I got something wrong? Thank you very much in advance.

                 SqlConnection con = new SqlConnection(GUIInitializer.DBconnectionString);
                  SqlCommand cmd = new SqlCommand(GUIInitializer.stp\_GUI\_DELETEITEMAIRLINEMAPPING, con);
                  cmd.CommandType = CommandType.StoredProcedure;
                  cmd.Parameters.AddWithValue("@ITEM\_CODE", itemCode);
                  con.Open();
                  int rows = cmd.ExecuteNonQuery();
                  con.Close();
                  if (rows == -1)
                  {
                      DeleteItemData(itemCode);
                  }
                  else
                      MessageBox.Show("Item Code: " + itemCode + " Deletion FAILED!");
      
      P Offline
      P Offline
      Palash Biswas
      wrote on last edited by
      #2

      For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.

      Z 1 Reply Last reply
      0
      • P Palash Biswas

        For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.

        Z Offline
        Z Offline
        Zar Ni
        wrote on last edited by
        #3

        Thank you for the reply.. But the above code currently, returns me -1 even though only one row is affected. Is there anything wrong with the code? Thanks again.. :)

        P 1 Reply Last reply
        0
        • Z Zar Ni

          Thank you for the reply.. But the above code currently, returns me -1 even though only one row is affected. Is there anything wrong with the code? Thanks again.. :)

          P Offline
          P Offline
          Palash Biswas
          wrote on last edited by
          #4

          I think you should put "SET NOCOUNT ON" in Top of your SP and "SET NOCOUNT OFF" At the end of your SP THANKS

          Z 2 Replies Last reply
          0
          • P Palash Biswas

            I think you should put "SET NOCOUNT ON" in Top of your SP and "SET NOCOUNT OFF" At the end of your SP THANKS

            Z Offline
            Z Offline
            Zar Ni
            wrote on last edited by
            #5

            Hi, Just tried it out. But still cannot. The insert is still replying me -1 when one row of data is added. But the delete is ok without adding NoCount. Thank you for your help.

            1 Reply Last reply
            0
            • P Palash Biswas

              I think you should put "SET NOCOUNT ON" in Top of your SP and "SET NOCOUNT OFF" At the end of your SP THANKS

              Z Offline
              Z Offline
              Zar Ni
              wrote on last edited by
              #6

              ok..got it..sorry for the trouble.. I didn't put the ( ) when I declare the variables in STP.

              CREATE PROCEDURE [dbo].[stp_GUI_InsertAirlineData] (
              @AIRLINE_CODE varchar(3),
              @AIRLINE_DESC nvarchar(50) )

              P 1 Reply Last reply
              0
              • Z Zar Ni

                ok..got it..sorry for the trouble.. I didn't put the ( ) when I declare the variables in STP.

                CREATE PROCEDURE [dbo].[stp_GUI_InsertAirlineData] (
                @AIRLINE_CODE varchar(3),
                @AIRLINE_DESC nvarchar(50) )

                P Offline
                P Offline
                Palash Biswas
                wrote on last edited by
                #7

                Cool!Enjoy! :-D

                Z 1 Reply Last reply
                0
                • P Palash Biswas

                  Cool!Enjoy! :-D

                  Z Offline
                  Z Offline
                  Zar Ni
                  wrote on last edited by
                  #8

                  Thanks anyway.. :)..Cheers!

                  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