Click here to Skip to main content
15,896,398 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am making desktop application using C#.NET and SQL SERVER
I have created one form having one listbox which contains 3 elements(team,vehicle,stationery).Now I want to add all elements of listbox into one field(resources)of database.I have tried follwing code

private void btmadd_Click(object sender, EventArgs e)
      {
          SqlConnection con = new SqlConnection("Data Source=SW-PC-20;Integrated security =SSPI;Initial catalog=PSM");
          con.Open();
          SqlCommand cmd = new SqlCommand("INSERT INTO" +
           " Publicity_Threshold(Threshold_id,Threshold_Name,Media_method_ID,Media_Used,Starting_Date,Ending_Date,Duration_Tenure,State,City,Area,Target_Audience,Target_Audience_Volume,Walkins_Volume,Person_Incharge,Budget,Quantum_of_Threshold,Resources)" +
            " VALUES" +
           " (@Threshold_id,@Threshold_Name,@Media_method_ID,@Media_Used,@Starting_Date,@Ending_Date,@Duration_Tenure,@State,@City,@Area,@Target_Audience,@Target_Audience_Volume,@Walkins_Volume,@Person_Incharge,@Budget,@Quantum_of_Threshold,@Resources)", con);
          cmd.Parameters.Add(new SqlParameter("@Threshold_id", Convert.ToInt32(txtThresholdID.Text)));
          cmd.Parameters.Add(new SqlParameter("@Threshold_Name", cboThreholdname.Text));
          cmd.Parameters.Add(new SqlParameter("@Media_method_ID", Convert.ToInt32(cbomethodid.Text)));
          cmd.Parameters.Add(new SqlParameter("@Media_Used", cbomediaused.Text));
          cmd.Parameters.Add(new SqlParameter("@Starting_Date", this.startingdate.Value));
          cmd.Parameters.Add(new SqlParameter("@Ending_Date", this.endingdate.Value));
          cmd.Parameters.Add(new SqlParameter("@Duration_Tenure", txtduration.Text));
          cmd.Parameters.Add(new SqlParameter("@State", cboState.Text));
          cmd.Parameters.Add(new SqlParameter("@City", cboCity.Text));
          cmd.Parameters.Add(new SqlParameter("@Area", cboArea.Text));
          cmd.Parameters.Add(new SqlParameter("@Target_Audience",cboTargetAudience.Text));
          cmd.Parameters.Add(new SqlParameter("@Target_Audience_Volume", Convert.ToInt32(txtaudiencevolume.Text)));
          cmd.Parameters.Add(new SqlParameter("@Walkins_Volume", Convert.ToInt32(txtinquiryvolume.Text)));
          cmd.Parameters.Add(new SqlParameter("@Person_Incharge", txtperson.Text));
          cmd.Parameters.Add(new SqlParameter("@Budget", Convert.ToInt32(txtBudget.Text)));
          cmd.Parameters.Add(new SqlParameter("@Quantum_of_Threshold",Convert.ToInt32(txtQuantum.Text)));
          cmd.Parameters.Add(new SqlParameter("@Resources",listBox1.Text));


          cmd.ExecuteNonQuery();
          MessageBox.Show("Insertion successfully done");

      }


but when on running appliactaion....it only add one item at a time which is selected
Posted

Hi,

For inserting all the items into single column, you need to append all these before giving parameter value to SqlCommand.

For this, You need to iterate the listbox and append them. You can take a deleimiter like (, or -) to sperate them in future use.

Regards
AR
 
Share this answer
 
Comments
shivani 2013 3-May-11 3:38am    
can u explain it in detail
Ankit Rajput 3-May-11 3:42am    
You need to club all the three variables into onelike this
string strResource="team,vehicle,stationery";

cmd.Parameters.Add(new SqlParameter("@Resources",strResource));
shivani 2013 3-May-11 4:45am    
as i need to add the items through listbox
can't i use like this

string resources =Convert.ToString(listBox1.Items);
cmd.Parameters.Add(new SqlParameter("@Resources", resources));
Ankit Rajput 3-May-11 4:52am    
No. You can not use like this. You need to iterate with all the items.
shivani 2013 3-May-11 5:12am    
how loop will work i mean can u tell the coding for looping items of listbox..i am not able to write
Run a loop on your listbox items - concatenate them into a temporary string variable and then assign that variable here.
 
Share this answer
 
Comments
shivani 2013 3-May-11 4:45am    
can u explain it more
Abhinav S 3-May-11 5:11am    
foreach(string itm in ListBox.Items) strC = strC + itm;

THen finally - put strC in @Resources.
shivani 2013 3-May-11 5:19am    
got it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900