none
LINQ TO SQL record Insertion problem RRS feed

  • Question

  • HI ..

    I am facing an error while inserting a record into my company_detail table.

    company_detail table have composite primary key named (application_number and identification_code (coming from applicant_type table)).

    When  i try to insert manually from db then insertion is bieng successfully but in case of trying

    with this LINQ 2 SQL code, I am facing the below exception.

     public string Insert_CompanyDetail()
        {
          DL.CrackerJackDataContext db = new PMS.CrackerJack.Service.DataLayer.CrackerJackDataContext();
          DL.Company_Detail company_detail = new DL.Company_Detail
          {
            application_number = "200061/Q0Z15B",
            identification_code = 2
          };
          db.Company_Details.InsertOnSubmit(company_detail);
          db.SubmitChanges();
          
          return "Success";
        }
    

     

    System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'.
       at System.Data.Linq.IdentityManager.StandardIdentityManager.SingleKeyManager`2.TryCreateKeyFromValues(Object[] values, V& v)
       at System.Data.Linq.IdentityManager.StandardIdentityManager.MultiKeyManager`3.TryCreateKeyFromValues(Object[] values, MultiKey`2& k)
       at System.Data.Linq.IdentityManager.StandardIdentityManager.IdentityCache`2.Find(Object[] keyValues)
       at System.Data.Linq.IdentityManager.StandardIdentityManager.Find(MetaType type, Object[] keyValues)
       at System.Data.Linq.CommonDataServices.GetCachedObject(MetaType type, Object[] keyValues)
       at System.Data.Linq.ChangeProcessor.GetOtherItem(MetaAssociation assoc, Object instance)
       at System.Data.Linq.ChangeProcessor.BuildEdgeMaps()
       at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
       at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
       at System.Data.Linq.DataContext.SubmitChanges()
       at PMS.CrackerJack.Service.CrackerJackService.Insert_CompanyDetail() in D:\Shared\27-Aug-2010\PMS.CrackerJack.Service\PMS.CrackerJack.Service\CrackerJackService.asmx.cs:line 50
       --- End of inner exception stack trace ---


    maifs
    Friday, August 27, 2010 7:13 AM

Answers

  • Your problem lies here:

     

    identification_code = 2

    The exception says it cant cast the above value from int to string. It sounds like the DB is expecting a string value, not int. You can either change the value type on the DB or use the .ToString() method to cast prior to insertion.

    Hope this helps!

    • Marked as answer by liurong luo Wednesday, September 1, 2010 11:12 AM
    Friday, August 27, 2010 9:58 AM

All replies