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. Inserting data based on multi select listbox data

Inserting data based on multi select listbox data

Scheduled Pinned Locked Moved C#
csharpasp-netdatabasevisual-studiotutorial
6 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
    mohd imran abdul aziz
    wrote on last edited by
    #1

    Dear all. i am making web application using asp.net and C#(Visual studio 2005). i have listbox(with multi select option) on web form. I am able to capture data through loop. now i want to know how i can place this loop data into single statement for insert statement.i am using prepared statement that is my insert query is "insert into agentmst(agentid,agentname,agentservice1,agentservice2,agentservice3,agentservice4,agentservice5,branchid,acreatedby,acreated,Ipadd) values(@aId,@aname,@serv1,@serv2,@serv3,@serv4,@serv5,@branchid,@crby,getdate(),@ip)"; (i am ristricted myselef to atmost five listbox option).now i am populating listbox option as parm3.ParameterName = "@serv1"; parm3.Value = names[0]; parm4.ParameterName = "@serv2"; parm4.Value = names[1]; parm5.ParameterName = "@serv3"; parm5.Value = names[2]; parm6.ParameterName = "@serv4"; parm6.Value = names[3]; parm7.ParameterName = "@serv5"; parm7.Value = names[4]; now i am able to fetch listbox data correctly through loop as for (int i = 0; i < listBox1.Items.Count; i++) { if (listBox1.Items[i].Selected == true) { for (int j = 1; j < 7; j++) { names[j] = listBox1.Items[i].Text; Response.Write(names[j]); break; } } } Now my question is how i can use this in insert statment. Please Guide me. Where i am going wrong. Any one knowing Please guide me. Thanks

    regards imran khan

    S V 2 Replies Last reply
    0
    • M mohd imran abdul aziz

      Dear all. i am making web application using asp.net and C#(Visual studio 2005). i have listbox(with multi select option) on web form. I am able to capture data through loop. now i want to know how i can place this loop data into single statement for insert statement.i am using prepared statement that is my insert query is "insert into agentmst(agentid,agentname,agentservice1,agentservice2,agentservice3,agentservice4,agentservice5,branchid,acreatedby,acreated,Ipadd) values(@aId,@aname,@serv1,@serv2,@serv3,@serv4,@serv5,@branchid,@crby,getdate(),@ip)"; (i am ristricted myselef to atmost five listbox option).now i am populating listbox option as parm3.ParameterName = "@serv1"; parm3.Value = names[0]; parm4.ParameterName = "@serv2"; parm4.Value = names[1]; parm5.ParameterName = "@serv3"; parm5.Value = names[2]; parm6.ParameterName = "@serv4"; parm6.Value = names[3]; parm7.ParameterName = "@serv5"; parm7.Value = names[4]; now i am able to fetch listbox data correctly through loop as for (int i = 0; i < listBox1.Items.Count; i++) { if (listBox1.Items[i].Selected == true) { for (int j = 1; j < 7; j++) { names[j] = listBox1.Items[i].Text; Response.Write(names[j]); break; } } } Now my question is how i can use this in insert statment. Please Guide me. Where i am going wrong. Any one knowing Please guide me. Thanks

      regards imran khan

      S Offline
      S Offline
      sno 1
      wrote on last edited by
      #2

      remember that the listbox in this powerfull lenguage c#, have many components that you can use in your project, for intance, only you need load the information in a datatable for example, and then tell to the listbox, and you can use too a combobox, the properties datasource that you load from a datatable: public void LoadDatatable(ref system.data.datatable some) { cmd = new SqlCommand("yourStoredProcedureOrStatement",con); cmd.CommandType = CommandType.StoredProcedure; try { adapt = new SqlDataAdapter(cmd); adapt.Fill(some); } catch(Exception ex){throw ex;} finally { con.Close(); } } system.data.datatable p=new system.data.datatable(); LoadDatatable(ref p); and after all this, only rest that you tell to the combobox or listbox, anyone that the properties DataSource = p; and the way too show the thinks that was load: ListBox.DataSource = p; ListBox.DisplayMember = "The name of the thinks that you like show"; ListBox.ValueMember = "A id, if exist, only to give more handle to this control"; ok I hope that this thinks help you.... one think: Forgive me by my English ok, because in my contry this idioms i'snt speak a lot.... if you wanna another answer o whatever please write me :laugh::laugh::laugh: e-mail:joelintel@gmail.com

      M 1 Reply Last reply
      0
      • S sno 1

        remember that the listbox in this powerfull lenguage c#, have many components that you can use in your project, for intance, only you need load the information in a datatable for example, and then tell to the listbox, and you can use too a combobox, the properties datasource that you load from a datatable: public void LoadDatatable(ref system.data.datatable some) { cmd = new SqlCommand("yourStoredProcedureOrStatement",con); cmd.CommandType = CommandType.StoredProcedure; try { adapt = new SqlDataAdapter(cmd); adapt.Fill(some); } catch(Exception ex){throw ex;} finally { con.Close(); } } system.data.datatable p=new system.data.datatable(); LoadDatatable(ref p); and after all this, only rest that you tell to the combobox or listbox, anyone that the properties DataSource = p; and the way too show the thinks that was load: ListBox.DataSource = p; ListBox.DisplayMember = "The name of the thinks that you like show"; ListBox.ValueMember = "A id, if exist, only to give more handle to this control"; ok I hope that this thinks help you.... one think: Forgive me by my English ok, because in my contry this idioms i'snt speak a lot.... if you wanna another answer o whatever please write me :laugh::laugh::laugh: e-mail:joelintel@gmail.com

        M Offline
        M Offline
        mohd imran abdul aziz
        wrote on last edited by
        #3

        Thanks for reply. But i already filled the listbox in page load event. Now On Save Button, i have to get those selected item in the listbox. and add to the parameter. My quetion is how can i assaign these selected option values to parameter used.

        regards imran khan

        S 1 Reply Last reply
        0
        • M mohd imran abdul aziz

          Thanks for reply. But i already filled the listbox in page load event. Now On Save Button, i have to get those selected item in the listbox. and add to the parameter. My quetion is how can i assaign these selected option values to parameter used.

          regards imran khan

          S Offline
          S Offline
          sno 1
          wrote on last edited by
          #4

          sorry for the long time, but I can'nt seat before my pc in some many hours, ok your question is simple the best mode to give you the answer is with a example.... if you have a listbox and in them properties selection mode you put "MultiSimple", and you do a correct add of the elements to show in run time, you selected the item that you need to save in your DB maybe using that int [] a; int p=0; p=this.listBox1 .SelectedIndices.Count ; a=new int[p]; for (int i = 0;i < p;i++) a[i] = this.listBox1 .SelectedIndices [i]; and in the array "a" you have all the position that be selected, the rest i think you know, make a loop wih that and with the class SqlParameter add all this position or the name of the item under this position.... is a plaesure help you, if you have any question tell me ok....:laugh::laugh::laugh:

          S 1 Reply Last reply
          0
          • S sno 1

            sorry for the long time, but I can'nt seat before my pc in some many hours, ok your question is simple the best mode to give you the answer is with a example.... if you have a listbox and in them properties selection mode you put "MultiSimple", and you do a correct add of the elements to show in run time, you selected the item that you need to save in your DB maybe using that int [] a; int p=0; p=this.listBox1 .SelectedIndices.Count ; a=new int[p]; for (int i = 0;i < p;i++) a[i] = this.listBox1 .SelectedIndices [i]; and in the array "a" you have all the position that be selected, the rest i think you know, make a loop wih that and with the class SqlParameter add all this position or the name of the item under this position.... is a plaesure help you, if you have any question tell me ok....:laugh::laugh::laugh:

            S Offline
            S Offline
            sno 1
            wrote on last edited by
            #5

            hi, in the last reply, I show you how you can select the rows in a listbox, but how you no tell my any more, I expect that you stand all.... regards sno-1:laugh:

            1 Reply Last reply
            0
            • M mohd imran abdul aziz

              Dear all. i am making web application using asp.net and C#(Visual studio 2005). i have listbox(with multi select option) on web form. I am able to capture data through loop. now i want to know how i can place this loop data into single statement for insert statement.i am using prepared statement that is my insert query is "insert into agentmst(agentid,agentname,agentservice1,agentservice2,agentservice3,agentservice4,agentservice5,branchid,acreatedby,acreated,Ipadd) values(@aId,@aname,@serv1,@serv2,@serv3,@serv4,@serv5,@branchid,@crby,getdate(),@ip)"; (i am ristricted myselef to atmost five listbox option).now i am populating listbox option as parm3.ParameterName = "@serv1"; parm3.Value = names[0]; parm4.ParameterName = "@serv2"; parm4.Value = names[1]; parm5.ParameterName = "@serv3"; parm5.Value = names[2]; parm6.ParameterName = "@serv4"; parm6.Value = names[3]; parm7.ParameterName = "@serv5"; parm7.Value = names[4]; now i am able to fetch listbox data correctly through loop as for (int i = 0; i < listBox1.Items.Count; i++) { if (listBox1.Items[i].Selected == true) { for (int j = 1; j < 7; j++) { names[j] = listBox1.Items[i].Text; Response.Write(names[j]); break; } } } Now my question is how i can use this in insert statment. Please Guide me. Where i am going wrong. Any one knowing Please guide me. Thanks

              regards imran khan

              V Offline
              V Offline
              vijayan_531
              wrote on last edited by
              #6

              sir i want multiselect listbox how to save in database please coding and query

              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