0
votes

I have 3 error and I can't manage this. Please, help me to solve them.

Thanks,

Raul

  • Severity Code Description Project File Line Suppression State Error CS0029 Cannot implicitly convert type 'System.Data.SqlClient.SqlCommand' to 'System.Data.SqlClient.SqlConnection' Website

  • Severity Code Description Project File Line Suppression State Error CS1061 'SqlConnection' does not contain a definition for
    'CommandText' and no extension method 'CommandText' accepting a first argument of type 'SqlConnection' could be found (are you missing a
    using directive or an assembly reference?)

  • Severity Code Description Project File Line Suppression State Error CS1061 'SqlConnection' does not contain a definition for
    'ExecuteReader' and no extension method 'ExecuteReader' accepting a
    first argument of type 'SqlConnection' could be found (are you
    missing a using directive or an assembly reference?) Website

    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Collections;

    namespace Website
    {
        public static class ConnectionClass
        {
            private static SqlConnection conn;
            private static SqlConnection command;

            static ConnectionClass()
            {
                string connectionString = ConfigurationManager.ConnectionStrings["tigariConnection"].ToString();
                conn = new SqlConnection(connectionString);
                command = new SqlCommand("",conn);
            }

            public static ArrayList GetTigariByType(string tigariType)
            {
                ArrayList list = new ArrayList();
                string query = string.Format("SELECT * FROM tigari WHERE type LIKE '{0}'", tigariType);
                try
                {
                    conn.Open();
                    command.CommandText = query;
                    SqlDataReader reader = command.ExecuteReader();

                    while(reader.Read())
                    {
                        int id = reader.GetInt32(0);
                        string name = reader.GetString(1);
                        string type = reader.GetString(2);
                        double price = reader.GetDouble(3);
                        string country = reader.GetString(4);
                        string image = reader.GetString(5);
                        string review = reader.GetString(6);

                        Tigari tigari = new Tigari(id, name, type, price, country, image, review);
                        list.Add(tigari);

                    }
                }
                finally
                {
                    conn.Close();
                }
                return list;
            }
        }
    }
1
Don't add tags at random please.Alan Stokes

1 Answers

1
votes

All three of them are a sign of a type error. I think the line

        private static SqlConnection command;

should be

        private static SqlCommand command;

https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

Additionally, I suggest you sanitize your inputs. If tigariType has single-quotes, that's a SQL injection waiting to happen! Even if this is just practice, it's good to practice with good form.

https://technet.microsoft.com/en-us/library/ms161953(v=sql.105).aspx