Resolved How to export from ListBox data to text file?

Joined
Sep 14, 2020
Messages
11
Programming Experience
5-10
Hi group,

I am trying to export from listbox to a text file, but I am getting next error message:

1600571616816.png



Can not convert from object type 'System.Data.DataRowView' to type 'System.String'

Error is on line 24.

My piece of code is as follow:


C#:
using (OracleConnection con = new OracleConnection(oradb))
{
//create a new oledb dataadapter
con.Open();
OracleDataAdapter daTXT = new OracleDataAdapter("SELECT decode(c1, 'L', 'G', 'V', 'P', 'E') ||','|| decode(c2, 'L', 'G', 'V', 'P', 'E') ||','|| decode(c3, 'L', 'G', 'V', 'P', 'E') ||','|| decode(c4, 'L', 'G', 'V', 'P', 'E') ||','|| decode(c5, 'L', 'G', 'V', 'P', 'E') ||','|| decode(c6, 'L', 'G', 'V', 'P', 'E') ||','|| decode(c7, 'L', 'G', 'V', 'P', 'E') ||','|| decode(c8, 'L', 'G', 'V', 'P', 'E') ||','|| decode(c9, 'L', 'G', 'V', 'P', 'E') ctotal FROM combina_final_2f7d_laluz where jornada = " + CmbJornada.Text + " and temporada = '" + CmbTemporada.Text + "' and pronosticador in ('FRANK1', 'FRANK2') order by desarrollo", con);

//create a new dataset
DataSet dsTXT = new DataSet();

//fill the datset
daTXT.Fill(dsTXT, "combinaciones_txt");
lstCombinaciones.DataSource = dsTXT.Tables["combinaciones_txt"];
lstCombinaciones.DisplayMember = "ctotal";


SaveFileDialog dlg = new SaveFileDialog();

if (dlg.ShowDialog() == DialogResult.OK)
{
StreamWriter writer = new StreamWriter(dlg.FileName);

for (int i = 0; i < lstCombinaciones.Items.Count; i++)
{
writer.WriteLine((string)lstCombinaciones.Items[i]);
}



writer.Close();
}

dlg.Dispose();

con.Close();
}

Any help or guidance will be really appreciated.

Thanks in advance.

Kind regards,

Francisco Mtz.
 
The items you get out of a ListBox are the items you put in. When you set the DataSource of a control, the items in that control come from the data source. When you bind a DataTable, the items come from its DefaultView, which is a DataView, which contains DataRowView objects. If you're putting DataRowView objects in, there's a good bet that you're going to get DataRowView objects out, not String objects.

If you want to get the displayed text for the current SelectedItem in a ListBox or ComboBox, you can use the Text property of the control. If you want to get the displayed text for any item, regardless of what type the item itself is, you can call the GetItemText method. In your case, that would look like this:
C#:
writer.WriteLine(lstCombinaciones.GetItemText(lstCombinaciones.Items[i]));
 
That said, a foreach loop would be more appropriate here than a for loop. If you have a for loop and the only use for the loop counter is as an index into a list to get every item in that list, you should be enumerating that list with a foreach loop:
C#:
foreach (var item in lstCombinaciones.Items)
{
    writer.WriteLine(lstCombinaciones.GetItemText(item));
}
You could also make your code much more succinct by calling File.WriteAllLines and using a LINQ query:
C#:
var lines = lstCombinaciones.Items.Cast<object>().Select(item => lstCombinaciones.GetItemText(item));

File.WriteAllLines(dlg.FileName, lines);
 
Thank you very much "jmcilhinney" for your great explanation and your quick responses.

Now, after replacing line 24 with your suggestion, it is working as expected.

C#:
using (OracleConnection con = new OracleConnection(oradb))
            {
                //create a new oledb dataadapter
                con.Open();
                OracleDataAdapter daTXT = new OracleDataAdapter("SELECT decode(c1, 'L', 'G', 'V', 'P', 'E') ||','|| decode(c2, 'L', 'G', 'V', 'P', 'E') ||','|| decode(c3, 'L', 'G', 'V', 'P', 'E') ||','|| decode(c4, 'L', 'G', 'V', 'P', 'E') ||','|| decode(c5, 'L', 'G', 'V', 'P', 'E') ||','|| decode(c6, 'L', 'G', 'V', 'P', 'E') ||','|| decode(c7, 'L', 'G', 'V', 'P', 'E') ||','|| decode(c8, 'L', 'G', 'V', 'P', 'E') ||','|| decode(c9, 'L', 'G', 'V', 'P', 'E') ctotal FROM combina_final_2f7d_laluz where jornada = " + CmbJornada.Text + " and temporada = '" + CmbTemporada.Text + "' and pronosticador in ('FRANK1', 'FRANK2') order by desarrollo", con);

                //create a new dataset
                DataSet dsTXT = new DataSet();

                //fill the datset
                daTXT.Fill(dsTXT, "combinaciones_txt");
                lstCombinaciones.DataSource = dsTXT.Tables["combinaciones_txt"];
                lstCombinaciones.DisplayMember = "ctotal";
                

                SaveFileDialog dlg = new SaveFileDialog();

                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    StreamWriter writer = new StreamWriter(dlg.FileName);

                    for (int i = 0; i < lstCombinaciones.Items.Count; i++)
                    {
                        //writer.WriteLine((string)lstCombinaciones.Items[i]);
                        writer.WriteLine(lstCombinaciones.GetItemText(lstCombinaciones.Items[i]));
                    }

                    writer.Close();
                }

                dlg.Dispose();

                con.Close();
             }

Thanks in advance.

Kind regards,

Francisco Mtz.
 
Back
Top Bottom