views:

362

answers:

1

I am trying to make a messagebox text provide datatable results. Below is a snippet of code that I have written so far:

String mystring = comboBox1.Text; if (mystring.Substring(0, 12) == ("Company Name")) { textBox2.Text = mystring.Substring(13); ADOCon.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Name\Desktop\SmallBizDb.mdb"; ADOCon.Open();

            OleDbCommandBuilder CBuilder = new OleDbCommandBuilder(DAdapter);
            DAdapter = new OleDbDataAdapter("Select Companies.Company_Name From Companies Where Companies.Company_Name = '" + textBox2.Text + "'", ADOCon);

            DAdapter.Fill(DTable);
            MessageBox.Show(DTable.ToString());
            ADOCon.Close();
            ADOCon.Dispose();
        }
        else

Basically, if an end user types "Company Name-Company One", for example, I would like a message box to appear stating the datatable (DTable) information, which comes from a sql query. Currently, I have "messagebox.Show(DTable.ToString());", which does not work. Also, all other examples I have seen use Row indexes, such as ".Rows[0]", which I cannot use since row numbers are not involved, but rather column names and record names from the sql "where" statement within the data adapter.

There is a lot fluff here, so my major issue is how to convert my datable results so that they will show up in a message box.

Thank you,

DFM

+1  A: 

I'm not certain what you are wanting, but if you want to display the values in your datatable you should be able to get the data from it and display it in any order you want. I think you are wanting to do something like

        System.Text.StringBuilder b = new System.Text.StringBuilder();
        foreach (System.Data.DataRow r in dt.Rows)
        {
            foreach (System.Data.DataColumn c in dt.Columns)
            {
                b.Append(c.ColumnName.ToString() + ":" + r[c.ColumnName].ToString());
            }
        }
        MessageBox.Show(b.ToString());

This will loop through all the rows returned, and for each row (each company in the results) add the details in the form ColumnName:ActualValue of the dataTable.

Of course I'm not certain displaying an unknown amount of data in a message box like this is a good idea, that's just a way you can do it.

Tetraneutron