Click here to Skip to main content
15,914,780 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
hi guys i am new to c# how can make this methods in a single one


C#
/// <returns></returns>
     public int GetParentIdForState(string country)
     {
         try
         {
             var obj = Entity.StoreExcelDatas.SingleOrDefault(x => x.ValText == country);
             return obj.ID;
         }
         catch (Exception ex)
         {
             Logger.Error(ex.Message, ex);
             return 0;
         }
     }

     public int GetParentIdForCity(string state,string Country)
     {
         try
         {
             var StateID=GetParentIdForState(Country);
             var obj = Entity.StoreExcelDatas.SingleOrDefault(x => x.ValText == state &&x.ParentId==StateID);
             return obj.ID;
         }
         catch (Exception ex)
         {
             Logger.Error(ex.Message, ex);
             return 0;
         }
     }
     public int GetParentIdForPincode(string city,int stateid)
     {
         try
         {
             var obj = Entity.StoreExcelDatas.SingleOrDefault(x => x.ValText == city &&x.ParentId==stateid);
             return obj.ID;
         }
         catch (Exception ex)
         {
             Logger.Error(ex.Message, ex);
             return 0;
         }
     }
     #endregion
Posted
Updated 10-Jan-16 20:08pm
v2
Comments
Tomas Takac 11-Jan-16 3:07am    
Why did you change the question? Now solution #1 seems to be completely unrelated to the question and people may down-vote it. Please revert it back. You are welcome to raise a separate question for your other code.

1 solution

C#
public void InsertToDataBase(string valType, string valText, int parentId = 0)
{
    try
    {
        StoreExcelData details = new StoreExcelData();
        details.ParentId = parentId;
        details.ValType = valType;
        details.ValText = valText;
        Entity.StoreExcelDatas.Add(details);
        Entity.SaveChanges();
    }
    catch (Exception ex)
    {
        Logger.Error(ex.Message, ex);
    }
}

//InsertCountryToDataBase
InsertToDataBase("Country", country);

//InsertState
InsertToDataBase("state", state, yourDefinedParentId);

//InsertCity
InsertToDataBase("City", city, yourDefinedParentId);

//and so on...
 
Share this answer
 
v2
Comments
Prasad Billupati 11-Jan-16 2:09am    
thank you
Tomas Takac 11-Jan-16 3:08am    
If the answer is helpful to you, you should accept it. This way you give credit to the person who answered it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900