Question

I'm doing a school project where we are making a cinema booking. One of the criteria of the program is to display all the movies in a database in a list.

I know this might be a really simple question, but I need a pointer in the right direction.

How do I make an object of a movie which contains all data of the movie (movieID, movieName, movieLength, movieDesc)

How do I use this data to fill a list?

We are writing the program in a client/server based architecture using WCF in C#

Please tell me if you need further elaboration or detail.

Thanks in advance. Here is my code for creating the movies in the database, I am unsure how to create a movie object from same database, to be used in my code.

SqlConnection sc = new SqlConnection();
sc.ConnectionString = ("Data Source=balder.ucn.dk;Initial Catalog=dmaa0213_6;********");
SqlCommand com = new SqlCommand();
sc.Open();
com.Connection = sc;
com.CommandText = ("INSERT into movies (movieName, movieLength, movieDescription) VALUES  ('" + movieName + "','" + movieLength + "','" + movieDesc + "');");
com.ExecuteNonQuery();
sc.Close();
Was it helpful?

Solution

You have the choice between using an hibernation layer like NHibernation or the bettwer EntityFramework from Microsoft, or, you have to do it manually. If this is the way, you have to create a Movie object like this:

public class Movie
{
    public string MovieID { get; set; }
    public string MovieName { get; set; }
    public string MovieLength { get; set; }
    public string MovieDesc { get; set; }
}

Then you can read the database query into a reader and fill the objects into a list, like this:

List<Movie> listOfMovies = new List<Movie>();

using(SqlConnection connection = new SqlConnection("Data Source=balder.ucn.dk;Initial Catalog=dmaa0213_6;********"))
{
    using(SqlCommand cmd = new SqlCommand(connection))
    {
        cmd.CommandString = "SELECT * FROM movies ORDER BY MovieId";
        connection.Open();
        using(SqlDataReader reader = cmd.ExecuteDataReader())
        {
            while(reader.Read())
            {
                Movie item = new Movie();
                item.MovieId = reader.GetInt32(0);
                item.MovieName = reader.GetString(1);
                item.MovieLength = reader.GetString(2);
                item.MovieDesc = reader.GetString(3);
                listOfMovies.Add(item);
            }
        }
        connection.Close();
    }
}

OTHER TIPS

you can try this code for filling dropdown, grid etc..

SqlConnection sc = new SqlConnection("Data Source=balder.ucn.dk;Initial Catalog=dmaa0213_6;********");

string query="SELECT * FROM movies ORDER BY MovieId";

datatable dt=new datatatable(); //using system.data;

sc.Open();

SqlCommand com = new SqlCommand(query,sc);

SqlDataAdapter da=new SqlDataAdapter(com );

da.fill(dt);

sc.Close();

The result of this code in dt will contain all the data from your database.

check your database table contains data or not using dt.rows.count;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top