oshirowanen
oshirowanen

Reputation: 15925

Getting a "The best overloaded method match for ... has some invalid arguments" error message

I am having problems with the following code:

public class ClientGroupDetails
{
    public DateTime Col2;
    public String Col3;
    public Int32 Col4;

    public ClientGroupDetails(DateTime m_Col2, String m_Col3, Int32 m_Col4)
    {
        Col2 = m_Col2;
        Col3 = m_Col3;
        Col4 = m_Col4;
    }

    public ClientGroupDetails() { }
}

[WebMethod()]
public List<ClientGroupDetails> GetClientGroupDetails(string phrase)
{
    var client_group_details = new List<ClientGroupDetails>();

    using (connection = new SqlConnection(ConfigurationManager.AppSettings["connString"]))
    {
        using (command = new SqlCommand(@"select col2, col3, col4 from table1 where col1 = @strSearch", connection))
        {
            command.Parameters.Add("@strSearch", SqlDbType.VarChar, 255).Value = phrase;

            connection.Open();
            using (reader = command.ExecuteReader())
            {
                int Col2Index = reader.GetOrdinal("col2");
                int Col3Index = reader.GetOrdinal("col3");
                int Col4Index = reader.GetOrdinal("col4");

                while (reader.Read())
                {
                    client_group_details.Add(new ClientGroupDetails(
                        reader.IsDBNull(Col2Index) ? (Nullable<DateTime>)null : (Nullable<DateTime>)reader.GetDateTime(Col2Index),
                        reader.IsDBNull(Col3Index) ? null : reader.GetString(Col3Index),
                        reader.GetInt32(Col4Index)));
                }
            }
        }
    }

    return client_group_details;
}
}

It is giving me the following error:

Compiler Error Message: CS1502: The best overloaded method match for 'Conflicts.ClientGroupDetails.ClientGroupDetails(System.DateTime, string, int)' has some invalid arguments
Line 184: client_group_details.Add(new ClientGroupDetails(

Upvotes: 0

Views: 16241

Answers (3)

kingpin
kingpin

Reputation: 1498

make m_Col2 DateTime? (Nullable<DateTime>)

so finally it will be like this

public class ClientGroupDetails
{
    public Nullable<DateTime> Col2;
    public String Col3;
    public Int32 Col4;

    public ClientGroupDetails(Nullable<DateTime> m_Col2, String m_Col3, Int32 m_Col4)
    {
        Col2 = m_Col2;
        Col3 = m_Col3;
        Col4 = m_Col4;
    }

    public ClientGroupDetails() { }
}

[WebMethod()]
public List<ClientGroupDetails> GetClientGroupDetails(string phrase)
{
    var client_group_details = new List<ClientGroupDetails>();
    using (connection = new SqlConnection(ConfigurationManager.AppSettings["connString"]))
    {
        using (command = new SqlCommand(@"select col2, col3, col4 from table1 where col1 = @strSearch", connection))
        {
            command.Parameters.Add("@strSearch", SqlDbType.VarChar, 255).Value = phrase;

            connection.Open();
            using (reader = command.ExecuteReader())
            {
                int Col2Index = reader.GetOrdinal("col2");
                int Col3Index = reader.GetOrdinal("col3");
                int Col4Index = reader.GetOrdinal("col4");

                while (reader.Read())
                {
                    client_group_details.Add(new ClientGroupDetails(
                        reader.IsDBNull(Col2Index) ? (Nullable<DateTime>)null : (Nullable<DateTime>)reader.GetDateTime(Col2Index),
                        reader.IsDBNull(Col3Index) ? (string)null : reader.GetString(Col3Index),
                        reader.GetInt32(Col4Index)));
                }
            }
        }
    }

    return client_group_details;
}
}

BTW: I'll recommend you 1. to use properties in your POCO instead of public instance variables and give them meaningful names 2. use lowercase local variable names

Upvotes: 2

gdoron
gdoron

Reputation: 150283

The ctor signature is DataTime and you call it with Nullable<DataTime>

public ClientGroupDetails(DateTime m_Col2, String m_Col3, Int32 m_Col4)

You called it with:

new ClientGroupDetails(
            reader.IsDBNull(Col2Index) ? (Nullable<DateTime>)null
        ...

Change the ctor to:

public ClientGroupDetails(DateTime? m_Col2, string m_Col3, int m_Col4)

And the property:

public DateTime? Col2;

Notes:

  • You don't have to write Int32, int is an "alias" for it.
  • The same thing with Nullable<DateTime> => DataTime? is it's alias.

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1063453

The constructor for ClientGroupDetails doesn't take Nullable<DateTime> (aka DateTime?) - it takes flat DateTime. You'll need to either change what ClientGroupDetails uses, or think of a default value (maybe DateTime.MinValue).

For example::

client_group_details.Add(new ClientGroupDetails(
    reader.IsDBNull(Col2Index) ? DateTime.MinValue : reader.GetDateTime(Col2Index),
    reader.IsDBNull(Col3Index) ? null : reader.GetString(Col3Index),
    reader.GetInt32(Col4Index)));

Or, keep your existing reader code and change the POCO:

public class ClientGroupDetails
{
    public DateTime? Col2;
    ...    
    public ClientGroupDetails(DateTime? m_Col2, ...
    {
        Col2 = m_Col2;
        ...
    }   
    ...
}

Alternatively - use a tool like "dapper" which will do it all for you:

var list = connection.Query<ClientGroupDetails>(
      @"select col2, col3, col4 from table1 where col1 = @phrase",
      new {phrase}).ToList();

Upvotes: 2

Related Questions