-1

I approached this issue by trying to convert string (movie name) to int32 (movie ID #) when I typed movie name to print out its movie ID number. Am I doing something wrong here? It causes error as stated:

Unable to cast object of type 'System.Data.SqlClient.SqlConnection' to type 'System.IConvertible'

My code:

string filename, connectionInfo;
SqlConnection db;

this.listBox1.Items.Clear();

filename = "netflix.mdf";

connectionInfo = String.Format(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\{0};Integrated Security=True;", filename);

db = new SqlConnection(connectionInfo);
db.Open();

SqlCommand cmd = new SqlCommand();
cmd.Connection = db;

string moviename = textBox1.Text;

cmd.CommandText = string.Format(
            @"SELECT MovieID FROM Movies                     
              where MovieName = '{0}';", moviename);

SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
db.Close();

int id = System.Convert.ToInt32(cmd);

this.listBox1.Items.Add(id);
2
  • And what would a SqlCommand object look like as a int??? Think about it... which variable do you really want to convert? Mar 17, 2015 at 0:01
  • you used the SqlCommand object to fill the dataset, thats probably where your data is Mar 17, 2015 at 0:07

2 Answers 2

1

Your cmd object isn't the result of the query, for executing the query look at the ExecuteReader method. This will return a SqlDataReader object that is used to iterate query results.

This is the example from MSDN:

using (SqlConnection connection = new SqlConnection(
           connectionString))
{
    connection.Open();

    SqlCommand command = new SqlCommand(queryString, connection);
    SqlDataReader reader = command.ExecuteReader();
    while (reader.Read())
    {
        Console.WriteLine(String.Format("{0}", reader[0]));
    }
}

In this example, queryString would be your query SELECT MovieID FROM MOVIES WHERE... and connection would be your db object

The object reader can be accesed as an array (property indexer), reader[0] contains the value of the first column as an Object after the first read, after multiple reads contains the value of the first column iterating over SELECT resulting rows

Also, you can't cast a string to an int, you need the int.Parse or TryParse method for converting a string to an int.

For converting the first column that you know is a string to an int it would be int.Parse((string)reader[0])

-1

I was supposed to execute a scalar query first before I can covert it to an int. I forgot to add ExecuteScalar() line after the command line.

       string moviename = textBox1.Text;

        cmd.CommandText = string.Format(
            @"SELECT MovieID FROM Movies                     
              where MovieName = '{0}';", moviename);

        object result = cmd.ExecuteScalar(); //solved my problem!

        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adapter.Fill(ds);
        db.Close();

        int id = System.Convert.ToInt32(result);

        this.listBox1.Items.Add(id);
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.