title: how to save listbox items in a text file
body: <p>I am using .Net 3.5 - I have a problem trying list box items to a text file.  I am using this code:</p>
  
  <pre><code>if (lbselected.Items.Count != 0) {
    string Path = Application.StartupPath + "\\ClientSelected_DCX.txt";
    StreamWriter writer = new StreamWriter(Path);
    int selectedDCXCount = System.Convert.ToInt32(lbselected.Items.Count);
    int i = 0;
  
    while (i != selectedDCXCount) {
      string selectedDCXText = (string)(lbselected.Items[i]);
      writer.WriteLine(selectedDCXText);
      i++;
    }
  
    writer.Close();
    writer.Dispose();
  }
  
  MessageBox.Show("Selected list has been saved", "Success", MessageBoxButtons.OK);
  </code></pre>
  
  <p>An error occurs for this line:</p>
  
  <pre><code>string selectedDCXText = (string)(lbselected.Items[i]);
  </code></pre>
  
  <p>The error is:</p>
  
  <blockquote>
    <p>Unable to cast object of type 'SampleData' to type 'System.String'
    please help me</p>
  </blockquote>