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. Add multiple values to a SqlParameter

Add multiple values to a SqlParameter

Scheduled Pinned Locked Moved C#
databasealgorithmsdata-structureshelptutorial
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.
  • B Offline
    B Offline
    Brad Wick
    wrote on last edited by
    #1

    I current have code to add the SQL Parameters to a array but when searching I need to sometimes not include the SQLParameter as its not required in the search. A example of my SQL Parameters is below which is passed to a function that runs the query. SqlParameter[] sqlparams = { new SqlParameter("@EventID", EventID), new SqlParameter("@Status", Status) }; I tried to use the following code but it produces a error if one of the two values is not in the query string (I do not insert the EventID or Status in the query string if its not being search) SqlParameter[] sqlparams = { EventID.Trim().Equals("") ? new SqlParameter() : new SqlParameter("@EventID", "%" + EventID.Trim() + "%"), Status.Trim().Equals("") ? new SqlParameter() : new SqlParameter("@Status", "%" + Status.Trim() + "%") }; What can I do to not include the @Status in the sqlparams if its empty?

    H G 2 Replies Last reply
    0
    • B Brad Wick

      I current have code to add the SQL Parameters to a array but when searching I need to sometimes not include the SQLParameter as its not required in the search. A example of my SQL Parameters is below which is passed to a function that runs the query. SqlParameter[] sqlparams = { new SqlParameter("@EventID", EventID), new SqlParameter("@Status", Status) }; I tried to use the following code but it produces a error if one of the two values is not in the query string (I do not insert the EventID or Status in the query string if its not being search) SqlParameter[] sqlparams = { EventID.Trim().Equals("") ? new SqlParameter() : new SqlParameter("@EventID", "%" + EventID.Trim() + "%"), Status.Trim().Equals("") ? new SqlParameter() : new SqlParameter("@Status", "%" + Status.Trim() + "%") }; What can I do to not include the @Status in the sqlparams if its empty?

      H Offline
      H Offline
      HemJoshi
      wrote on last edited by
      #2

      If the stored procedure you are using for your search takes both eventID and Status field then you need to pass both the parameter otherwise always error message will be thrown. My suggestion is either you use different stored procedure for different combination of search criteria or handlle it on the single stored procedure depending upon the value of SQL parameter.

      1 Reply Last reply
      0
      • B Brad Wick

        I current have code to add the SQL Parameters to a array but when searching I need to sometimes not include the SQLParameter as its not required in the search. A example of my SQL Parameters is below which is passed to a function that runs the query. SqlParameter[] sqlparams = { new SqlParameter("@EventID", EventID), new SqlParameter("@Status", Status) }; I tried to use the following code but it produces a error if one of the two values is not in the query string (I do not insert the EventID or Status in the query string if its not being search) SqlParameter[] sqlparams = { EventID.Trim().Equals("") ? new SqlParameter() : new SqlParameter("@EventID", "%" + EventID.Trim() + "%"), Status.Trim().Equals("") ? new SqlParameter() : new SqlParameter("@Status", "%" + Status.Trim() + "%") }; What can I do to not include the @Status in the sqlparams if its empty?

        G Offline
        G Offline
        Giorgi Dalakishvili
        wrote on last edited by
        #3

        When you don't need SqlParamater just set its value to DBNULL

        Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion

        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