title: Add values to a list contains a Lookup Field
body: <p>I have a list named "Designation" that contains a Designation Code and Designation Name.
  I have another list named "Employee" that contains Employee Name and Designation Name (as a Lookup Field).
  I am able to insert values to "Employee" list using the following code. </p>
  
  <pre><code>    protected void AddEmp(object sender, EventArgs e)
      {
        string emp_name = txtEmployeeName.Text;
        string emp_designation = ddlDesignation.SelectedValue;                    
        using (SPSite site = new SPSite("My Site"))
        {
         using (SPWeb web = site.OpenWeb())
         {
          SPSecurity.RunWithElevatedPrivileges(delegate()
          {
           web.AllowUnsafeUpdates = true;
           SPList splist_employees = web.Lists["Employee"];
           SPList splist_designations = web.Lists["Designations"];
           SPListItemCollection splc_items = splist_employees.Items;
           SPListItem spli_item = splc_items.Add();
           SPFieldLookup lookup = splist_employees.Fields["Designated"] as SPFieldLookup;
           string fieldName = lookup.LookupField;
           spli_item["Employee Name"] = emp_name;
           spli_item[fieldName] = GetLookFieldIDS(emp_designation, splist_designations);
           spli_item.Update();
           });
          }
        }
      }
  
      public static string GetLookFieldIDS(string lookupValues, SPList lookupSourceList)
      {
       string id = string.Empty;
       SPFieldLookupValueCollection lookupIds = new SPFieldLookupValueCollection();
       SPQuery query = new Microsoft.SharePoint.SPQuery();
       query.Query = "&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name='Designation' /&gt;&lt;Value type='Text'&gt;"+lookupValues + "&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;";
       SPListItemCollection listItems = lookupSourceList.GetItems(query);
       foreach (Microsoft.SharePoint.SPListItem item in listItems)
       {
        id = item.ID.ToString();
       }
       return id;
      }
  </code></pre>
  
  <p>Here I have given the field name as 'Designation' inside the query.</p>
  
  <p>But, I want to find the field name based on the value given from the user end instead of hard-coding the field name as 'Designation'.</p>
  
  <p>Any help is greatly appreciated. Thanks in advance.</p>