Search code examples
c#databasepostgresqlodoo

Insert record in odoo PostgreSQL database from c# and see record in Odoo


I'm trying to insert a new client record in Odoo8 PostrgreSQL database using c#.

To connect with PostgreSQL database from c# , i use npgsql.

here is my Insert command

cmd.CommandText = "INSERT INTO res_partner (name,email,notify_email ,active) VALUES ('user name','[email protected]','[email protected]'," + true + ")";

I'm able to see the new insert record in res_partner table using pgAdmin, but using Odoo , i'm not able to see the new client.


Solution

  • Odoo has a Web Service api which is better to use to interact with external application. With a little research, I found that interesting wrapper: OdooRpcWrapper . I used it in my application and everything works fine. I'm now able to see new added client from c# in Odoo8.

    Code to add new client:

    OdooConnectionCredentials creds = new OdooConnectionCredentials("http://localhost:8069", "your_bd", "admin", "admin");
    OdooAPI api = new OdooAPI(creds);
    //Define what model you want to use
    OdooModel partnerModel = api.GetModel("res.partner");
    //Create new objects by calling the model. New objects need to be saved.
    OdooRecord record = partnerModel.CreateNew();
    record.SetValue("name", "Abdelaziz test");
    record.Save();
    

    Step to use it in asp.net Webforms application:

    1. Download OdooRpcWrapper from github
    2. add it to your project: right click on solution => add => existing project and select the wrapper
    3. add it as reference in your asp project: right click on reference => add reference => under solution tab, select OdooRpcWrapper project you added earlier
    4. use the code to add new client