0

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(
gdoron
  • 147,333
  • 58
  • 291
  • 367
oshirowanen
  • 15,297
  • 82
  • 198
  • 350
  • 4
    (shudders quietly about public fields, parameters with `m_` prefixes, etc) – Marc Gravell Feb 24 '12 at 12:39
  • Public fields are never a good idea, and add nothing over properties which give you the safe ability to tweak the implementation later (and cost nothing at all). And what does `m_` tell you that you don't already know? indeed, most people like such notation use `m_` for **fields**, not parameters – Marc Gravell Feb 24 '12 at 13:17

3 Answers3

2

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

kingpin
  • 1,498
  • 1
  • 11
  • 12
  • Done, but I am now getting `Compiler Error Message: CS1502: The best overloaded method match for 'ClientGroupDetails.ClientGroupDetails(System.DateTime?, string, int)' has some invalid arguments` – oshirowanen Feb 24 '12 at 12:44
2

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();
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Changed the POCO and am now getting `Compiler Error Message: CS1502: The best overloaded method match for 'ClientGroupDetails.ClientGroupDetails(System.DateTime?, string, int)' has some invalid arguments` – oshirowanen Feb 24 '12 at 12:45
  • @oshirowanen have you tried splitting it into two steps? i.e. DateTime? x = ...; string y = ...; int z = ...; var obj = new ClientGroupDetails(x,y,z); client_group_details.Add(obj); ? that'll help identify the error – Marc Gravell Feb 24 '12 at 12:49
1

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.
gdoron
  • 147,333
  • 58
  • 291
  • 367
  • That gives me the following error message `Compiler Error Message: CS0266: Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)` – oshirowanen Feb 24 '12 at 12:40
  • @oshirowanen. You need to change the property as well. `public DateTime? Col2;` – gdoron Feb 24 '12 at 12:42
  • Done, but I am now getting `Compiler Error Message: CS1502: The best overloaded method match for 'ClientGroupDetails.ClientGroupDetails(System.DateTime?, string, int)' has some invalid arguments` – oshirowanen Feb 24 '12 at 12:44
  • @oshirowanen. Maybe this will help: `reader.IsDBNull(Col3Index) ? (string)null : reader.GetString(Col3Index)` – gdoron Feb 24 '12 at 12:50
  • @oshirowanen. Are you still having difficulties? – gdoron Feb 24 '12 at 14:34