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. Visual Basic
  4. using store procedure to insert selected checkboxlist values into database

using store procedure to insert selected checkboxlist values into database

Scheduled Pinned Locked Moved Visual Basic
databasecsharphelp
3 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.
  • M Offline
    M Offline
    Mamphekgo Bahula
    wrote on last edited by
    #1

    Hi I created store produce to insert selected values from checkboxlist into sql database and it gives me this error "Procedure or function SaveAdmin has too many arguments specified." here is my store procedure ALTER PROCEDURE [dbo].[SaveAdmin] -- Add the parameters for the stored procedure here @Perno int, @Client_id int, @Region_id int, @Service_id int AS INSERT INTO Admin(Perno, Client_id, Region_id, Service_id) VALUES(@Perno, @Client_id, @Region_id, @Service_id) and here is my vb.net code cmdStandby.CommandText = "SaveAdmin" cmdStandby.CommandType = CommandType.StoredProcedure cmdStandby.Connection = sqlstandby cmdStandby.Parameters.AddWithValue("@Perno", lblEmpNo.Text) cmdStandby.Parameters.AddWithValue("@Client_id", ddlClient1.SelectedValue) For Each li In ChkRegion.Items If li.Selected = True Then cmdStandby.Parameters.AddWithValue("@Region_id", li.Value) End If Next For Each li In ChkService.Items If li.Selected = True Then cmdStandby.Parameters.AddWithValue("@Service_id", li.Value) End If Next cmdStandby.ExecuteNonQuery() thanx in advance

    Mamphekgo

    T D 2 Replies Last reply
    0
    • M Mamphekgo Bahula

      Hi I created store produce to insert selected values from checkboxlist into sql database and it gives me this error "Procedure or function SaveAdmin has too many arguments specified." here is my store procedure ALTER PROCEDURE [dbo].[SaveAdmin] -- Add the parameters for the stored procedure here @Perno int, @Client_id int, @Region_id int, @Service_id int AS INSERT INTO Admin(Perno, Client_id, Region_id, Service_id) VALUES(@Perno, @Client_id, @Region_id, @Service_id) and here is my vb.net code cmdStandby.CommandText = "SaveAdmin" cmdStandby.CommandType = CommandType.StoredProcedure cmdStandby.Connection = sqlstandby cmdStandby.Parameters.AddWithValue("@Perno", lblEmpNo.Text) cmdStandby.Parameters.AddWithValue("@Client_id", ddlClient1.SelectedValue) For Each li In ChkRegion.Items If li.Selected = True Then cmdStandby.Parameters.AddWithValue("@Region_id", li.Value) End If Next For Each li In ChkService.Items If li.Selected = True Then cmdStandby.Parameters.AddWithValue("@Service_id", li.Value) End If Next cmdStandby.ExecuteNonQuery() thanx in advance

      Mamphekgo

      T Offline
      T Offline
      TheComputerMan
      wrote on last edited by
      #2

      Hi, I have never used add with value but I assume it adds the parameter to the collection as well as adding the value. Try using .value = Even if that worked you would still only get one 'set'passed through as your loops are replacing the value each iteration. Your executenonquery needs to be inside the loop, and the loop needs to add each type of value to the set. Looking at what you are trying to do I suspect that you should be normalising the database a bit more, unless you want each row to contain the employee and client data for each service ID and region ID Sort of like: (I don't think this is quite right as I an not sure what you controls are but you should get the idea)

      cmdStandby.CommandText = "SaveAdmin"
      cmdStandby.CommandType = CommandType.StoredProcedure
      cmdStandby.Connection = sqlstandby

      cmdStandby.Parameters.Add("@Perno", SqlDbType.Int)
      cmdStandby.Parameters.Add("@Client_id", SqlDbType.Int)
      cmdStandby.Parameters.Add("@Region_id", SqlDbType.Int)
      cmdStandby.Parameters.Add("@Service_id", SqlDbType.Int)

      For Each li In ChkRegion.CheckedItems

      cmdStandby.Parameters("@Perno").Value = CINT(lblEmpNo.Text)  'or the proper conversion
      cmdStandby.Parameters("@Client\_id").Value = ddlClient1.SelectedValue	'or should this be selected index? If value what is it ? Text, integer etc
      cmdStandby.Parameters("@Region\_id").Value = li.Value '(?)
      cmdStandby.Parameters("@Service\_id").Value = li.Value '(?)
      cmdStandby.ExecuteNonQuery()
      

      Next

      For Each li In ChkService.CheckedItems

      cmdStandby.Parameters("@Perno").Value = CINT(lblEmpNo.Text)  'or the proper conversion
      cmdStandby.Parameters("@Client\_id").Value = ddlClient1.SelectedValue
      cmdStandby.Parameters("@Region\_id").Value = li.Value '(?)
      cmdStandby.Parameters("@Service\_id").Value = li.Value '(?)
      cmdStandby.ExecuteNonQuery()
      

      Next

      1 Reply Last reply
      0
      • M Mamphekgo Bahula

        Hi I created store produce to insert selected values from checkboxlist into sql database and it gives me this error "Procedure or function SaveAdmin has too many arguments specified." here is my store procedure ALTER PROCEDURE [dbo].[SaveAdmin] -- Add the parameters for the stored procedure here @Perno int, @Client_id int, @Region_id int, @Service_id int AS INSERT INTO Admin(Perno, Client_id, Region_id, Service_id) VALUES(@Perno, @Client_id, @Region_id, @Service_id) and here is my vb.net code cmdStandby.CommandText = "SaveAdmin" cmdStandby.CommandType = CommandType.StoredProcedure cmdStandby.Connection = sqlstandby cmdStandby.Parameters.AddWithValue("@Perno", lblEmpNo.Text) cmdStandby.Parameters.AddWithValue("@Client_id", ddlClient1.SelectedValue) For Each li In ChkRegion.Items If li.Selected = True Then cmdStandby.Parameters.AddWithValue("@Region_id", li.Value) End If Next For Each li In ChkService.Items If li.Selected = True Then cmdStandby.Parameters.AddWithValue("@Service_id", li.Value) End If Next cmdStandby.ExecuteNonQuery() thanx in advance

        Mamphekgo

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        Is suspect your problem is here:

        For Each li In ChkService.Items
        If li.Selected = True Then
        cmdStandby.Parameters.AddWithValue("@Service_id", li.Value)
        End If
        Next

        This loop is going through a list and checking if it's selected. For each value selected you're adding another parameter (with the same name) to the Parameters collection. If there is more than one item selected, your code is adding more than the 4 parameters to the SQL call.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007, 2008

        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