Inserting data based on multi select listbox data
-
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
-
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
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
-
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
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
-
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
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:
-
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:
-
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
sir i want multiselect listbox how to save in database please coding and query