Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am making desktop application using C#.I have created a form in which there are various labels with respective textboxes,combox(like threshold id,name,date etc) and one listbox.I have taken 2 buttons add ,update for a form.I have taken group box in which there is a list box with 2 buttons add.delete.For a whole form it add/update the contents(like id,name,date and items of listbox(for each entry items in listbox vary).Now group box containing listbox,add/update button operate accordingly for each entry made for threshold id,name,date etc.the problem is when i add whole contents of the form .I get an error

"Unable to cast object to type 'System.Data.DataRowView' to type 'System.String'.
I have checked when i dont include listbox coding ,there is no error and when i include listbox coding i get an error
below is the coding

 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)));
          
              string strC = "";
              foreach (string itm in listBox1.Items)
              {
                  strC = strC + " ," + itm;

              }
              cmd.Parameters.Add(new SqlParameter("@Resources",strC));        
          cmd.ExecuteNonQuery();
          MessageBox.Show("Insertion successfully done");

}
Posted

try this in the Following way


 foreach (ListItem item in ListBox1.Items)
           {
               strc += item + ",";
           }


//You are taking string for ListBox items instead of list Item
 
Share this answer
 
Comments
shivani 2013 12-May-11 3:02am    
what is listitem here
shivani 2013 12-May-11 3:23am    
pls help
Do it like that it should work for you

listBox1.Items.Cast<object>().Aggregate((item1, item2) => item1 + "," + item2).ToString();
</object>


In your code the problem is here

foreach (string itm in listBox1.Items)
 
Share this answer
 
Comments
shivani 2013 12-May-11 3:11am    
sir its desktop application .hw can i use </object>
shivani 2013 12-May-11 3:23am    
plz help
nit_singh 12-May-11 4:17am    
listBox1.Items.Cast<object>().Aggregate((item1, item2) => item1 + "," + item2).ToString();
use this. </object> is automatic generated
shivani 2013 12-May-11 4:53am    
i did
cmd.Parameters.Add(new SqlParameter("@Resources",listBox1.Items.Cast<object>().Aggregate((item1, item2) => item1 + "," + item2).ToString())); in place of
{
strC = strC + " ," + itm;

}
cmd.Parameters.Add(new SqlParameter("@Resources",strC);


insertion is successfully but in database in field resources data not fetched and written "System.Data.DataRowView,System.Data.DataRowView"
nit_singh 12-May-11 5:31am    
See the binding of the listboc. What is displayed at the run time? What is the type of ListBox?

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