EasyE
EasyE

Reputation: 580

Not able to assign a return value from another class

I am a bit new to C# and it seems like I am having trouble assigning a value to a list which I have created in a controller. I trying to assign a value from a repo class that is returning the value of a list.

The message I am getting is

There is no argument given that corresponds to the required formal parameter 'client' of 'Repo.SearchClient(ClientInfo)'

mY controller:

    public ActionResult SearchResult()
    {
        Repo repo = new Repo();
        List<ClientInfo> searchResult = new List<ClientInfo>();
        searchResult = repo.SearchClient(); // error here 
        JsonResult result = new JsonResult();
        result.Data = searchResult;
        result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
        return result;
    }

My Repo class that is returning a list value

  public List<ClientInfo> SearchClient(ClientInfo client)
    {
        var clientName = client.clientName;
        List<ClientInfo> clientResult = new List<ClientInfo>();
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            conn.Open();
            try
            {
                SqlCommand command = new SqlCommand("SELECT * FROM Table_1 WHERE ClientName =@clientName", conn);
                command.Parameters.AddWithValue("@clientName", clientName);
                SqlDataReader reader = command.ExecuteReader();

                    while (reader.Read())
                    {
                        ClientInfo data = new ClientInfo();
                        data.clientName = reader["ClientName"].ToString();
                        data.clientNumber = reader["ClientNumber"].ToString();
                        data.clientType = reader["ClientType"].ToString();
                        clientResult.Add(data);
                    }                    
            }
            catch
            {
                throw;
            }
        }
        return clientResult;
    }

My Model

namespace ClientSearch.Models
{
    public class ClientInfo
    { 
        public string clientName { get; set; }

        public string clientNumber { get; set; }

        public string clientType { get; set; }
    }
}

Upvotes: 0

Views: 82

Answers (1)

Dave Greilach
Dave Greilach

Reputation: 895

Your search client method requires a ClientInfo as a parameter.

public List<ClientInfo> SearchClient(ClientInfo client) // required parameter

The action in your controller is not providing this when calling the method.

List<ClientInfo> searchResult = new List<ClientInfo>();
    searchResult = repo.SearchClient(); // no parameter

This will give an error when compiling.

To fix this you need to do something like:

var clientInfo = new ClientInfo()
    {
        ClientName = "test client"
    }; // create a new ClientInfo object

var clientList = SearchClient(clientInfo); // call the search method and assign the results to a list

Upvotes: 3

Related Questions