-1

I have class which gets info from Imdb.com about movie ant stores info in class variables.I have another class which writes info about movie in database. So, I want to make inheritence and get variables info and save into database. But I am getting error: 'FilmuBiblioteka.IMDb' does not contain a constructor that takes 0 arguments

Here is my code:

public class IMDb
    {
        public bool status { get; set; }
        public string Id { get; set; }
        public string Title { get; set; }
        public string OriginalTitle { get; set; }
        public string Year { get; set; }
        public string Rating { get; set; }
        public ArrayList Genres { get; set; }
        public ArrayList Directors { get; set; }
        public ArrayList Cast { get; set; }
        public string Plot { get; set; }
        public string Poster { get; set; }
        public string PosterLarge { get; set; }
        public string PosterFull { get; set; }
        public string Runtime { get; set; }
        public ArrayList Languages { get; set; }
        public ArrayList Countries { get; set; }
        public string ImdbURL { get; set; }

        //Search Engine URLs
        private string GoogleSearch = "http://www.google.com/search?q=imdb+";
        private string BingSearch = "http://www.bing.com/search?q=imdb+";
        private string AskSearch = "http://www.ask.com/web?q=imdb+";
        private Func<string> toString;
        private bool v;

        //Constructor
        public IMDb(string MovieName, bool GetExtraInfo = true)
        {
            string imdbUrl = getIMDbUrl(System.Uri.EscapeUriString(MovieName));
            status = false;
            if (!string.IsNullOrEmpty(imdbUrl))
            {
                parseIMDbPage(imdbUrl, GetExtraInfo);
            }
        }

        public IMDb(Func<string> toString, bool v)
        {
            this.toString = toString;
            this.v = v;
        }

        //Get IMDb URL from search results
        private string getIMDbUrl(string MovieName, string searchEngine = "google")
        {
            string url = GoogleSearch + MovieName; //default to Google search
            if (searchEngine.ToLower().Equals("bing")) url = BingSearch + MovieName;
            if (searchEngine.ToLower().Equals("ask")) url = AskSearch + MovieName;
            string html = getUrlData(url);
            ArrayList imdbUrls = matchAll(@"<a href=""(http://www.imdb.com/title/tt\d{7}/)"".*?>.*?</a>", html);
            if (imdbUrls.Count > 0)
                return (string)imdbUrls[0]; //return first IMDb result
            else if (searchEngine.ToLower().Equals("google")) //if Google search fails
                return getIMDbUrl(MovieName, "bing"); //search using Bing
            else if (searchEngine.ToLower().Equals("bing")) //if Bing search fails
                return getIMDbUrl(MovieName, "ask"); //search using Ask
            else //search fails
                return string.Empty;
        }
        //Parse IMDb page data
        private void parseIMDbPage(string imdbUrl, bool GetExtraInfo)
        {
            string html = getUrlData(imdbUrl + "combined");
            Id = match(@"<link rel=""canonical"" href=""http://www.imdb.com/title/(tt\d{7})/combined"" />", html);
            if (!string.IsNullOrEmpty(Id))
            {
                status = true;
                Title = match(@"<title>(IMDb \- )*(.*?) \(.*?</title>", html, 2);
                OriginalTitle = match(@"title-extra"">(.*?)<", html);
                Year = match(@"<title>.*?\(.*?(\d{4}).*?\).*?</title>", html);
                Rating = match(@"<b>(\d.\d)/10</b>", html);
                Genres = matchAll(@"<a.*?>(.*?)</a>", match(@"Genre.?:(.*?)(</div>|See more)", html));
                Directors = matchAll(@"<td valign=""top""><a.*?href=""/name/.*?/"">(.*?)</a>", match(@"Directed by</a></h5>(.*?)</table>", html));
                Cast = matchAll(@"<td class=""nm""><a.*?href=""/name/.*?/"".*?>(.*?)</a>", match(@"<h3>Cast</h3>(.*?)</table>", html));
                Plot = match(@"Plot:</h5>.*?<div class=""info-content"">(.*?)(<a|</div)", html);
                Runtime = match(@"Runtime:</h5><div class=""info-content"">(\d{1,4}) min[\s]*.*?</div>", html);
                Languages = matchAll(@"<a.*?>(.*?)</a>", match(@"Language.?:(.*?)(</div>|>.?and )", html));
                Countries = matchAll(@"<a.*?>(.*?)</a>", match(@"Country:(.*?)(</div>|>.?and )", html));
                Poster = match(@"<div class=""photo"">.*?<a name=""poster"".*?><img.*?src=""(.*?)"".*?</div>", html);
                if (!string.IsNullOrEmpty(Poster) && Poster.IndexOf("media-imdb.com") > 0)
                {
                    Poster = Regex.Replace(Poster, @"_V1.*?.jpg", "_V1._SY200.jpg");
                    PosterLarge = Regex.Replace(Poster, @"_V1.*?.jpg", "_V1._SY500.jpg");
                    PosterFull = Regex.Replace(Poster, @"_V1.*?.jpg", "_V1._SY0.jpg");
                }
                else
                {
                    Poster = string.Empty;
                    PosterLarge = string.Empty;
                    PosterFull = string.Empty;
                }
                ImdbURL = "http://www.imdb.com/title/" + Id + "/";
                if (GetExtraInfo)
                {
                    string plotHtml = getUrlData(imdbUrl + "plotsummary");
                }
            }

        }

        /*******************************[ Helper Methods ]********************************/

        //Match single instance
        private string match(string regex, string html, int i = 1)
        {
            return new Regex(regex, RegexOptions.Multiline).Match(html).Groups[i].Value.Trim();
        }

        //Match all instances and return as ArrayList
        private ArrayList matchAll(string regex, string html, int i = 1)
        {
            ArrayList list = new ArrayList();
            foreach (Match m in new Regex(regex, RegexOptions.Multiline).Matches(html))
                list.Add(m.Groups[i].Value.Trim());
            return list;
        }

        //Strip HTML Tags
        static string StripHTML(string inputString)
        {
            return Regex.Replace(inputString, @"<.*?>", string.Empty);
        }

        //Get URL Data
        private string getUrlData(string url)
        {
            WebClient client = new WebClient();
            Random r = new Random();
            //Random IP Address
            client.Headers["X-Forwarded-For"] = r.Next(0, 255) + "." + r.Next(0, 255) + "." + r.Next(0, 255) + "." + r.Next(0, 255);
            //Random User-Agent
            client.Headers["User-Agent"] = "Mozilla/" + r.Next(3, 5) + ".0 (Windows NT " + r.Next(3, 5) + "." + r.Next(0, 2) + "; rv:2.0.1) Gecko/20100101 Firefox/" + r.Next(3, 5) + "." + r.Next(0, 5) + "." + r.Next(0, 5);
            Stream datastream = client.OpenRead(url);
            StreamReader reader = new StreamReader(datastream);
            StringBuilder sb = new StringBuilder();
            while (!reader.EndOfStream)
                sb.Append(reader.ReadLine());
            return sb.ToString();
        }
    }
    public class filmai : IMDb
    {
        public System.Data.DataSet gauticonnection
        {
            get
            { return gauti(); }
        }

        private System.Data.DataSet gauti()
        {
            System.Data.SqlClient.SqlConnection sqlConnection1;
            System.Data.SqlClient.SqlDataAdapter da;
            sqlConnection1 = new System.Data.SqlClient.SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Darius\Documents\Visual Studio 2013\Projects\FilmuBiblioteka\FilmuBiblioteka\duombaze.mdf");
            da = new SqlDataAdapter("select * from filmai", sqlConnection1);
            DataSet ds = new DataSet();
            da.Fill(ds, "Table");

            try
            {
                sqlConnection1.Open();
                da.Fill(ds);
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                if (sqlConnection1.State == System.Data.ConnectionState.Open)
                    sqlConnection1.Close();
            }

            return ds;
        }
        public void prideti()
        {
            System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Darius\Documents\Visual Studio 2013\Projects\FilmuBiblioteka\FilmuBiblioteka\duombaze.mdf");

            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.CommandText = "INSERT filmai (Id, Pavadinimas, Metai, trukme, salis, kalba, reitingas, zanras, aktoriai, link, plot, rezisieriai) VALUES (Id, Title, Year, Runtime, Countries, Languages, Rating, Genres, Cast, ImdbURL, Plot, Directors)";
            cmd.Connection = sqlConnection1;
            sqlConnection1.Open();
            cmd.ExecuteNonQuery();
            sqlConnection1.Close();
        }
    }

I'm getting error at this line: public class filmai : IMDb

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
SiRaZz
  • 43
  • 9
  • 3
    Because you are inheriting from IMDb you are expected to call `:base(...)` from the constructor of your filmai class. Since you have not defined a constructor for your filmai class you are automatically given the default empty constructor which is attempting to call `:base()` but since your IMDb constructors require arguments it's saying you need to implement a new constructor for filmai (does not need to have arguments) and call one of the base constructors for IMDb with the necessary arguments. – Stephen Ross Dec 01 '15 at 17:19
  • @StephenRoss Can you show some code how to do that, because I'am not familiar with these situasions using classes.... – SiRaZz Dec 01 '15 at 17:35
  • Possible duplicate of [C# Error: Parent does not contain a constructor that takes 0 arguments](http://stackoverflow.com/questions/7230544/c-sharp-error-parent-does-not-contain-a-constructor-that-takes-0-arguments) – Frank Bryce Dec 01 '15 at 18:51

1 Answers1

3

As previously said in my comment the compliation error that you have is because the current constructor for your filmai class does not correctly call a constructor of your IMDb class.

To correctly inherit from a base class you must call it's constructor before your implemented constructor of the deriving class runs such as:

public filmai(string filmName)
: base(filmName, false)
{
}

This will call the base constructor of public IMDb(string MovieName, bool GetExtraInfo = true) with the film name that was provided to the filmai constructor and will say that it does not require extra information.

I'm not sure on your use-case though as it looks like filmai is purely for database access and IMDb is for requesting for a single film? But implementing a constructor on filmai and calling : base(...) will fix your issue just now.

Stephen Ross
  • 882
  • 6
  • 21
  • @ Stephen Ross I have another problem. In Form1.cs i have button click event to call method which takes records from database and show them in datagriview. I am calling using this code: `filmai duom = new filmai(); set = duom.gauticonnection; dataGridView1.DataSource = set.Tables[0];` Getting error: `'FilmuBiblioteka.filmai' does not contain a constructor that takes 0 arguments` – SiRaZz Dec 10 '15 at 14:07
  • Ok, my example above was just an example. Find out what arguments you need to create `IMDb` and provide these from the `filmai` `:base(...)` call. In reality you'll have to pass at least a `MovieName` to the `IMDb` class from the `filmai` constructor. This data could either come from the constructor arguments of `filmai` (as above) or hard-coded into the `:base(...)` call. – Stephen Ross Dec 10 '15 at 14:39