Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unable to cast object of type 'System.Int32' to type 'System.String'??

Status
Not open for further replies.

zishan876

Programmer
Mar 19, 2007
61
0
0
US
Hi all:
I keep getting this error:
"unable to cast object of type 'System.Int32' to type 'System.String'"
What does it mean, this is my code
Code:
            SqlConnection conn = new SqlConnection("Server=xxxx;Database=xxx;User ID=xxx;Password=xxxxxx");

            //Create A Command (the query or store proc you will use)
            
            SqlCommand sqlComm = new SqlCommand("Select LocationId,SF_STREET,SF_City,SF_State,SF_Zip from SF_Locations where LocationId=15", conn);
            //Open the connection
            
            conn.Open();
            //Execute the command(query)
            
            SqlDataReader r = sqlComm.ExecuteReader();
            
            while (r.Read())
            {
                this.textBoxAddress.Text = r.GetString(2);
                //I even tried this.textBoxAddress.Text=r["SF_State"]); did not work
            
            }
            // Close the reader and the connection

            r.Close();

            conn.Close();
Thanks
 
textBoxAddress.Text = r["SF_State"].ToString();

My guess is that you have a State table and you are linking to it in the address. You are in this case expecting an integer ID rather than the state's name...

 
Thank you so much that worked out...I am new to C#.Net, So why did this happen?
Thanks
Shan
 
the DataRow is an array of objects. You tried to use an integer as a string in this case because r["SF_State"] contains an integer.

The object type is dependent on the Column type in the database.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top