1

I have a MS Access database that has a table with several columns. When I click a button I want to take the rows of string data from one column of the database and display it in a rich text box. I have created the following code:

 public static string DisplayListOfTeams()
    {
        //create a data table object to hold data retrieved from DB
        DataSet ds = new DataSet();

        //set SQL query to string variable
        String sqlQuery = "SELECT * FROM Team";

        //instantiate new OleDbDataAdapter object
        OleDbDataAdapter adpTeams = new OleDbDataAdapter(sqlQuery, aConnection);

        //add data in DB to the dataset object/table
        adpTeams.Fill(ds, "TeamTable");            

        string myValue;

        if (ds.Tables["TeamTable"].Rows.Count > 0)
        {
            //You must cast the value because it is an object
            myValue = (string)ds.Tables["TeamTable"].Rows[0][0];
        }
        else
        {
            myValue = "No Data found";
        }
        return myValue;
    }

 richTextBox1.Text = TeamsDA.DisplayListOfTeams();

I receive an "Unable to cast object of type 'System.Int32' to type 'System.String'" error at the following line:

myValue = (string)ds.Tables["TeamTable"].Rows[0][0];

I'm not sure what I am doing wrong or how to go about doing what I am trying to do.

4
  • 1
    The type of ds.Tables["TeamTable"].Rows[0][0] is int, so you can't cast to string. You can call .ToString() though... Commented Apr 11, 2012 at 21:41
  • what is the value of ds.Tables["TeamTable"].Rows[0][0]? Commented Apr 11, 2012 at 21:42
  • 1
    you forgot to tag this as msaccess so it gets blocked on my page. Commented Apr 11, 2012 at 21:42
  • @ Valamas - Sorry about that. Commented Apr 11, 2012 at 21:46

2 Answers 2

3

One (two?) word(s?): ToString

Sign up to request clarification or add additional context in comments.

4 Comments

The column name id typeOfSport and the rows consist of Basketball, Baseball, Football, Soccer, Swimming, and Diving. What I am trying to do is display the rows in the text box. I tried: myValue = "ds.Tables["TeamTable"].ToString();" but it just displays "TeamTable"
ToString returns a string, so you don't need to cast it. But in your sample code, you were casting ds.Tables["TeamTable"].Rows[0][0], not ds.Tables["TeamTable"]. So try ds.Tables["TeamTable"].Rows[0][0].ToString(). When you call ds.Tables["TeamTable"].ToString(), you're trying to convert the entire table to a single string. That could mean lots of things, but the meaning that was chosen by the programmers of the DataTable class was to return the name of the table.
@ phoog - OK. So I am completely wrong and need to go about it a completely different way. OK. I'll do some more research and try to figure out how to list the rows. Thank you
@ProgrammingNewbie you want to display the entire table in a rich text box, as a rich text table? If so, do you know how to construct a table in a rich text box? I certainly don't. A more conventional approach would be to use a grid control of some sort. Good luck! ... ah, I just noticed the method you posted was called "display list of teams"... as you've probably figured out, the cast in the sample code is just casting the integer in the first column of the first row. If you want a single string, you could iterate rows and columns and build one with StringBuilder.
1

Don't cast it... use ToString() instead.

If you cast it will not convert the return, it will only try to send as the type of. If you use ToString(), it will convert to the right way, if possible.

If I'm wrong, please correct me. But I'm sure with ToString() will work.

Edit :

Fixed the case sensitive error. Thanks to digEmAll

1 Comment

ToString (uppercase T), toString is java ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.