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. ExecuteNonQuery Problem

ExecuteNonQuery Problem

Scheduled Pinned Locked Moved C#
databasehelpquestionsharepointcom
6 Posts 4 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.
  • E Offline
    E Offline
    Erdinc27
    wrote on last edited by
    #1

    i want to insert some datas to my database from my datatable.i created a stored procedure for that like that

    ALTER proc sp_VeriEkle
    (
    @cek_no nvarchar(10),
    @cek_tarih nvarchar(20),
    @num1 nvarchar(10),
    @num2 nvarchar(10),
    @num3 nvarchar(10),
    @num4 nvarchar(10),
    @num5 nvarchar(10),
    @num6 nvarchar(10)
    )
    as
    Begin
    Insert into NumaraBilgileri(cekilis_no,cekilis_tarihi,num1,num2,num3,num4,num5,num6) values (@cek_no,@cek_tarih,@num1,@num2,@num3,@num4,@num5,@num6)
    End

    and my codes like below to insert

            for (int i = 0; i < table.Rows.Count; i++)
            {
                cmd.Parameters.AddWithValue("@cek\_no", System.Data.SqlDbType.NVarChar).Value = table.Rows\[i\]\[0\].ToString();
                cmd.Parameters.AddWithValue("@cek\_tarih", System.Data.SqlDbType.NVarChar).Value = table.Rows\[i\]\[1\].ToString();
                cmd.Parameters.AddWithValue("@num1", System.Data.SqlDbType.NVarChar).Value = table.Rows\[i\]\[4\].ToString();
                cmd.Parameters.AddWithValue("@num2", System.Data.SqlDbType.NVarChar).Value = table.Rows\[i\]\[5\].ToString();
                cmd.Parameters.AddWithValue("@num3", System.Data.SqlDbType.NVarChar).Value = table.Rows\[i\]\[6\].ToString();
                cmd.Parameters.AddWithValue("@num4", System.Data.SqlDbType.NVarChar).Value = table.Rows\[i\]\[7\].ToString();
                cmd.Parameters.AddWithValue("@num5", System.Data.SqlDbType.NVarChar).Value = table.Rows\[i\]\[8\].ToString();
                cmd.Parameters.AddWithValue("@num6", System.Data.SqlDbType.NVarChar).Value = table.Rows\[i\]\[9\].ToString();
                
                cmd.ExecuteNonQuery();
    

    but here after adding first row it gives error like "Procedure or function sp_VeriEkle" has too many specified arguments and it shows as a wrong line cmd.ExecuteNonQuery().what is wrong here for second row ?

    vemedya.com

    H M 2 Replies Last reply
    0
    • E Erdinc27

      i want to insert some datas to my database from my datatable.i created a stored procedure for that like that

      ALTER proc sp_VeriEkle
      (
      @cek_no nvarchar(10),
      @cek_tarih nvarchar(20),
      @num1 nvarchar(10),
      @num2 nvarchar(10),
      @num3 nvarchar(10),
      @num4 nvarchar(10),
      @num5 nvarchar(10),
      @num6 nvarchar(10)
      )
      as
      Begin
      Insert into NumaraBilgileri(cekilis_no,cekilis_tarihi,num1,num2,num3,num4,num5,num6) values (@cek_no,@cek_tarih,@num1,@num2,@num3,@num4,@num5,@num6)
      End

      and my codes like below to insert

              for (int i = 0; i < table.Rows.Count; i++)
              {
                  cmd.Parameters.AddWithValue("@cek\_no", System.Data.SqlDbType.NVarChar).Value = table.Rows\[i\]\[0\].ToString();
                  cmd.Parameters.AddWithValue("@cek\_tarih", System.Data.SqlDbType.NVarChar).Value = table.Rows\[i\]\[1\].ToString();
                  cmd.Parameters.AddWithValue("@num1", System.Data.SqlDbType.NVarChar).Value = table.Rows\[i\]\[4\].ToString();
                  cmd.Parameters.AddWithValue("@num2", System.Data.SqlDbType.NVarChar).Value = table.Rows\[i\]\[5\].ToString();
                  cmd.Parameters.AddWithValue("@num3", System.Data.SqlDbType.NVarChar).Value = table.Rows\[i\]\[6\].ToString();
                  cmd.Parameters.AddWithValue("@num4", System.Data.SqlDbType.NVarChar).Value = table.Rows\[i\]\[7\].ToString();
                  cmd.Parameters.AddWithValue("@num5", System.Data.SqlDbType.NVarChar).Value = table.Rows\[i\]\[8\].ToString();
                  cmd.Parameters.AddWithValue("@num6", System.Data.SqlDbType.NVarChar).Value = table.Rows\[i\]\[9\].ToString();
                  
                  cmd.ExecuteNonQuery();
      

      but here after adding first row it gives error like "Procedure or function sp_VeriEkle" has too many specified arguments and it shows as a wrong line cmd.ExecuteNonQuery().what is wrong here for second row ?

      vemedya.com

      H Offline
      H Offline
      Hiren solanki
      wrote on last edited by
      #2

      erdinc27 wrote:

      for (int i = 0; i < table.Rows.Count; i++) { cmd.Parameters.AddWithValue("@cek_no", System.Data.SqlDbType.NVarChar).Value = table.Rows[i][0].ToString();

      Creating a parameter inside loop indicates you are creating same parameter with the same name again and again. So my advice would be to declare parameter outside a loop and then set values only of that inside loop. E.G.

      cmd.parameters.add(\\add parameter name with datatype here);
      Loop
      {
      cmd.parameters["\\name of the parameter"].Value = \\Set value here.
      }

      Thanks.

      Regards, Hiren. "The more we give of anything, the more we shall get back." - Grace Speare (you can consider this quote while giving vote also) Microsoft Dynamics CRM

      L E 2 Replies Last reply
      0
      • E Erdinc27

        i want to insert some datas to my database from my datatable.i created a stored procedure for that like that

        ALTER proc sp_VeriEkle
        (
        @cek_no nvarchar(10),
        @cek_tarih nvarchar(20),
        @num1 nvarchar(10),
        @num2 nvarchar(10),
        @num3 nvarchar(10),
        @num4 nvarchar(10),
        @num5 nvarchar(10),
        @num6 nvarchar(10)
        )
        as
        Begin
        Insert into NumaraBilgileri(cekilis_no,cekilis_tarihi,num1,num2,num3,num4,num5,num6) values (@cek_no,@cek_tarih,@num1,@num2,@num3,@num4,@num5,@num6)
        End

        and my codes like below to insert

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    cmd.Parameters.AddWithValue("@cek\_no", System.Data.SqlDbType.NVarChar).Value = table.Rows\[i\]\[0\].ToString();
                    cmd.Parameters.AddWithValue("@cek\_tarih", System.Data.SqlDbType.NVarChar).Value = table.Rows\[i\]\[1\].ToString();
                    cmd.Parameters.AddWithValue("@num1", System.Data.SqlDbType.NVarChar).Value = table.Rows\[i\]\[4\].ToString();
                    cmd.Parameters.AddWithValue("@num2", System.Data.SqlDbType.NVarChar).Value = table.Rows\[i\]\[5\].ToString();
                    cmd.Parameters.AddWithValue("@num3", System.Data.SqlDbType.NVarChar).Value = table.Rows\[i\]\[6\].ToString();
                    cmd.Parameters.AddWithValue("@num4", System.Data.SqlDbType.NVarChar).Value = table.Rows\[i\]\[7\].ToString();
                    cmd.Parameters.AddWithValue("@num5", System.Data.SqlDbType.NVarChar).Value = table.Rows\[i\]\[8\].ToString();
                    cmd.Parameters.AddWithValue("@num6", System.Data.SqlDbType.NVarChar).Value = table.Rows\[i\]\[9\].ToString();
                    
                    cmd.ExecuteNonQuery();
        

        but here after adding first row it gives error like "Procedure or function sp_VeriEkle" has too many specified arguments and it shows as a wrong line cmd.ExecuteNonQuery().what is wrong here for second row ?

        vemedya.com

        M Offline
        M Offline
        musefan
        wrote on last edited by
        #3

        It is because you are adding the seconds row parameters to the existing list or command parameters. Try creating your cmd instance inside of the for loop

        Life goes very fast. Tomorrow, today is already yesterday.

        M 1 Reply Last reply
        0
        • H Hiren solanki

          erdinc27 wrote:

          for (int i = 0; i < table.Rows.Count; i++) { cmd.Parameters.AddWithValue("@cek_no", System.Data.SqlDbType.NVarChar).Value = table.Rows[i][0].ToString();

          Creating a parameter inside loop indicates you are creating same parameter with the same name again and again. So my advice would be to declare parameter outside a loop and then set values only of that inside loop. E.G.

          cmd.parameters.add(\\add parameter name with datatype here);
          Loop
          {
          cmd.parameters["\\name of the parameter"].Value = \\Set value here.
          }

          Thanks.

          Regards, Hiren. "The more we give of anything, the more we shall get back." - Grace Speare (you can consider this quote while giving vote also) Microsoft Dynamics CRM

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          +5 for explaining :)

          I are Troll :suss:

          1 Reply Last reply
          0
          • H Hiren solanki

            erdinc27 wrote:

            for (int i = 0; i < table.Rows.Count; i++) { cmd.Parameters.AddWithValue("@cek_no", System.Data.SqlDbType.NVarChar).Value = table.Rows[i][0].ToString();

            Creating a parameter inside loop indicates you are creating same parameter with the same name again and again. So my advice would be to declare parameter outside a loop and then set values only of that inside loop. E.G.

            cmd.parameters.add(\\add parameter name with datatype here);
            Loop
            {
            cmd.parameters["\\name of the parameter"].Value = \\Set value here.
            }

            Thanks.

            Regards, Hiren. "The more we give of anything, the more we shall get back." - Grace Speare (you can consider this quote while giving vote also) Microsoft Dynamics CRM

            E Offline
            E Offline
            Erdinc27
            wrote on last edited by
            #5

            thak u for your help guys..it works good now

            vemedya.com

            1 Reply Last reply
            0
            • M musefan

              It is because you are adding the seconds row parameters to the existing list or command parameters. Try creating your cmd instance inside of the for loop

              Life goes very fast. Tomorrow, today is already yesterday.

              M Offline
              M Offline
              musefan
              wrote on last edited by
              #6

              Why 2 bad answers?!? The OP question only asks WHY it does not work - which I answer exactly. It does not say tell me the answer and give me a solution. Because that would be a HOW question

              Life goes very fast. Tomorrow, today is already yesterday.

              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