Muruga
Muruga

Reputation: 123

The type arguments for methods cannot be inferred from the usage. Try specifying the type arguments explicitly

I am trying to populate Autocomplete text from SQL Server in textbox using TextChanged() event. [WindowsForm, C#]

I have a partial class Form

TextChanged Event:

private void textBoxFilterCName_TextChanged(object sender, EventArgs e)
    {
        DataTable dt = FillCustomerName(textBoxFilterCName.Text);
        List<string> listNames = CustomerName.ConvertDataTableToList(dt);
        textBoxFilterCName.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        AutoCompleteStringCollection dataName = new AutoCompleteStringCollection();
        dataName.AddRange(listNames.ToArray());
        textBoxFilterCName.AutoCompleteCustomSource = dataName;
        textBoxFilterCName.AutoCompleteSource = AutoCompleteSource.CustomSource;
    }

I am getting this error when I call this method CustomerName.ConvertDataTableToList().

The type arguments for methods 'CustomerName.ConvertDataTableToList(DataTable)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

FillCustomerName() Method

public DataTable FillCustomerName(string cName)
    {
        DataTable dtName = new DataTable();
        List<string> listName = new List<string>();
        try
        {
            dbconnection.Open();
            string query = "SELECT [Name] FROM Customers WHERE Is_Active=1 AND Name like @CName + '%'";
            using (SqlCommand cmd = new SqlCommand(query, dbconnection))
            {
                cmd.Parameters.Add("CName", SqlDbType.VarChar).Value = cName;
                dtName.Load(cmd.ExecuteReader());
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Connection Error");
        }
        finally
        {
            dbconnection.Close();
        }
        return dtName;
    }

CustomerName Class:

public static class CustomerName
{
    private static T CreateItemFromRow<T>(DataRow row, IList<PropertyInfo> properties) where T : new()
    {
        T item = new T();
        foreach (var property in properties)
        {
            if (property.PropertyType == typeof(System.DayOfWeek))
            {
                DayOfWeek day = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), row[property.Name].ToString());
                property.SetValue(item, day, null);
            }
            else
            {
                property.SetValue(item, row[property.Name], null);
            }
        }
        return item;
    }
    public static List<T> ConvertDataTableToList<T>(this DataTable table) where T : new()
    {
        IList<PropertyInfo> properties = typeof(T).GetProperties().ToList();
        List<T> result = new List<T>();

        foreach (var row in table.Rows)
        {
            var item = CreateItemFromRow<T>((DataRow)row, properties);
            result.Add(item);
        }
        return result;            
    }
}

The query is executed and results are generated successfully when I debug the application. But I could not load the datatable results as list.

Possible suggestions for this conversion are most welcome.

Upvotes: 6

Views: 19917

Answers (1)

Zein Makki
Zein Makki

Reputation: 30052

So you want to convert your DataTable to a list of objects of a certain type. Right?

The generic method infers the generic type T from the parameters only. Since you have no parameters of type T then you need to explicitly specify it. So when you write this line:

List<string> listNames = CustomerName.ConvertDataTableToList(dt);

The Compiler can't infer what that the type T you want is a string because it is only used in the return type. So you need to explicitly specify that:

CustomerName.ConvertDataTableToList<string>(dt);

However, since you specify the condition where T : new(), this means the generic type should have a parameterless constructor, string doesn't satisfy that. You're matching the row by name, so your only option is to modify the generic methods or create a class like this:

public class CustomerNameHolder
{
    public string Name { set; get; }
}

Then:

List<string> listNames = CustomerName.ConvertDataTableToList<CustomerNameHolder>(dt)
                                     .Select(x=> x.Name).ToList();

Upvotes: 8

Related Questions