none
How to insert data using Domain Context class ?

    Question

  • Hi friends,

    I'm using "Domain Service Class" and "Domain Context" for retrieving the data in the data grid. Can any body tell me how can I insert the new record in the table using the "Domain Context". Please tell me any simple implementation for doing it.

    Tuesday, March 20, 2012 5:01 PM

Answers

  • 22990atinesh

     

    1. Create an object of DomainContext:

    DomainContext ctx = new DomainContext();

    2. Create an entity that u would like to insert.

    Entity e = new Entity();

    e.field1 = "Bacon";

    e.field2 = "SomeWhere";

    3. Add to the ctx

    ctx.entity.add(e);

    ctx.SubmitChanges();

     

    Hand writting

    Tuesday, March 20, 2012 6:44 PM
  • 22990atinesh

    Member

     

    First add namespace to the web

    using USilverlightProject.Web;

    public partial class MainPage : UserControl
        {

            protected ATI_Virtual_ClassroomContext ctx = new ATI_Virtual_ClassroomContext();
            public MainPage()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, RoutedEventArgs e)
            {

                   Newsletters_Table ex = new Newsletters_Table();

                   ex.NL_ID = 10;

                   ex.EmailAdd = abc@gmail.com;

                   ctx.add(ex);

                   ctx.SubmitChanges();

            }
        }

    Thursday, March 22, 2012 5:43 PM
  • Atinesh,

    Here your insert code at the client:

                Newsletters_Table ex = new Newsletters_Table();
                ex.NL_ID = 10;
                ex.EmailAdd = "abc@gmail.com";
                ctx.Newsletters_Tables.Add(ex);
                ctx.SubmitChanges();

    Here code at your domain contaext. it was generated automatically when you create your DomainService correctly, you shoud nothing to do with this,

            public void InsertNewsletters_Table(Newsletters_Table newsletters_Table)
            {
                if ((newsletters_Table.EntityState != EntityState.Detached))
                {
                    this.ObjectContext.ObjectStateManager.ChangeObjectState(newsletters_Table, EntityState.Added);
                }
                else
                {
                    this.ObjectContext.Newsletters_Table.AddObject(newsletters_Table);
                }
            }

    Look there is no "ADD" methodt that, but Ria Services know that add row is at this service:

    Tuesday, March 27, 2012 8:31 PM

All replies

  • This link and its contents will start you up with RIA-Service in Silverlight.

    http://www.silverlight.net/learn/advanced-techniques/wcf-ria-services/get-started-with-wcf-ria-services

     

     

    Tuesday, March 20, 2012 5:30 PM
  • 22990atinesh

     

    1. Create an object of DomainContext:

    DomainContext ctx = new DomainContext();

    2. Create an entity that u would like to insert.

    Entity e = new Entity();

    e.field1 = "Bacon";

    e.field2 = "SomeWhere";

    3. Add to the ctx

    ctx.entity.add(e);

    ctx.SubmitChanges();

     

    Hand writting

    Tuesday, March 20, 2012 6:44 PM
  • but life is not always smooth. You may have to deal with error such as database table constraints, non-nullable fields... when inserting new record just like you would in your normal database app.

    Tuesday, March 20, 2012 7:55 PM
  • Hi thaicarrot,

    I think your way is quite way easier to understand. But I've some problem, could u please elaborate it.

    I've a domain context object of name "ATI_Virtual_Classroom" and I want to insert the data into the table "Newsletters_Table" having the two fields (NL_ID int, EmailAdd varchar(50)) on the button click event. Suppose for an example I've to insert data (10, abc@gmail.com). Could you please tell me how can I do it.

    Thursday, March 22, 2012 5:10 PM
  • 22990atinesh

    Member

     

    First add namespace to the web

    using USilverlightProject.Web;

    public partial class MainPage : UserControl
        {

            protected ATI_Virtual_ClassroomContext ctx = new ATI_Virtual_ClassroomContext();
            public MainPage()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, RoutedEventArgs e)
            {

                   Newsletters_Table ex = new Newsletters_Table();

                   ex.NL_ID = 10;

                   ex.EmailAdd = abc@gmail.com;

                   ctx.add(ex);

                   ctx.SubmitChanges();

            }
        }

    Thursday, March 22, 2012 5:43 PM
  • Hi thaicarrot,

    I can not be able to access the domain context object in "MainPage.xaml.cs". I've included the namespace 

    "using ATI_Virtual_Classroom.Web;" But still I cann't be able to access it.

    See the screen shot "Capture1" according to this my domain context object name is "ATI_Virtual_Classroom".

    and in the "Capture2" u can see that my namespace name is also "ATI_Virtual_Classroom". So, whenever I try to create the object of "ATI_Virtual_Classroom" than it is picking up the namspace rather than Domain context object (you can see this in "Capture3"). And when I put the pointer onto the "ATI_Virtual_Classroom" then a message comes saying "Type name expected, but namespace name found" (Capture4).

    Link to the screen shots

    https://skydrive.live.com/redir.aspx?cid=7529e2a34bfa8106&resid=7529E2A34BFA8106!189&parid=7529E2A34BFA8106!107

    Friday, March 23, 2012 8:27 AM
  • 22990atinesh

    Member

     

    At main Page

     

    Right click "ATI_Virtual_Classroom" at context menu click "Go To Difinition" command then delete those class.

     

    That is because you generated addition class.

     

    Friday, March 23, 2012 8:49 AM
  • Try this with full name space

    ATI_Virtual_Classroom.Web.ATI_Virtual_Classroom context = new ATI_Virtual_Classroom.Web.ATI_Virtual_Classroom()

    Friday, March 23, 2012 8:57 AM
  • Thanks thaicarrot,

    It worked. But a new problem arises, I can not be able to see the method "add" in the "ctx" object. Instead "AddReference" method is showing. You can see the screen shot. Here..

    https://skydrive.live.com/redir.aspx?cid=7529e2a34bfa8106&resid=7529E2A34BFA8106!194&parid=7529E2A34BFA8106!189

    Friday, March 23, 2012 1:15 PM
  • Thanks thaicarrot,

    It worked. But a new problem arises, I can not be able to see the method "add" in the "ctx" object. Instead "AddReference" method is showing. You can see the screen shot. Here..

    https://skydrive.live.com/redir.aspx?cid=7529e2a34bfa8106&resid=7529E2A34BFA8106!194&parid=7529E2A34BFA8106!189

    It should be following with Entity

    e.g

    ctx.Newsletters_Table.Add(Entity);

    Friday, March 23, 2012 11:09 PM
  • Hi thaicarrot,

    It is not working. when Click the button then message comes "Recorded Updated" and no error comes. But when I see the database table it is not updated. The code I'm using

      private ATI_Virtual_Classroom.Web.ATI_Virtual_Classroom ctx = new Web.ATI_Virtual_Classroom();

    private void btnnewsltrsemail_Click(object sender, RoutedEventArgs e)
    {
    try
    {
    Newsletters_Table tb = new Newsletters_Table();
    tb.EmailAdd = txtnewsltrsemail.Text;
    ctx.Newsletters_Tables.Add(tb);
    ctx.SubmitChanges();
    MessageBox.Show("Record Updated");
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }

    }
    Don't worry about "NL_ID" column, I've set it as primary key and it's identity specification to true.
    Saturday, March 24, 2012 5:31 AM
  • It's an ansynchronous call so you have to make the call as following in order to find the status of the call result. It sounds like you may have some db constraint error.

    Newsletters_Table tb = new Newsletters_Table();
    tb.EmailAdd = txtnewsltrsemail.Text;
    ctx.Newsletters_Tables.Add(tb);
    SubmitOperation submitOp = ctx.SubmitChanges();
    submitOp.Completed += (s,e) =>
    {
      if (submitOp.HasError) {
        MessageBox.Show(submitOp.Error.ToString()); // To see a complete trace stack of the exception.
        submitOp.MarkErrorAsHandled();
      } // if
      else
        MessageBox.Show("Record Updated");
    }

    Saturday, March 24, 2012 4:54 PM
  • Hi G20,

    I tried your code, But it has some flaws. Please see the screen shots below

    https://skydrive.live.com/redir.aspx?cid=7529e2a34bfa8106&resid=7529E2A34BFA8106!200&parid=7529E2A34BFA8106!189

    https://skydrive.live.com/redir.aspx?cid=7529e2a34bfa8106&resid=7529E2A34BFA8106!201&parid=7529E2A34BFA8106!189

    Sunday, March 25, 2012 2:26 PM
  • Hi G20,

    I tried your code, But it has some flaws. Please see the screen shots below

    https://skydrive.live.com/redir.aspx?cid=7529e2a34bfa8106&resid=7529E2A34BFA8106!200&parid=7529E2A34BFA8106!189

    https://skydrive.live.com/redir.aspx?cid=7529e2a34bfa8106&resid=7529E2A34BFA8106!201&parid=7529E2A34BFA8106!189

    You'd beter putting the solution on the skyDrive.

    //changed e to ex

    submitOp.Completed += (s,ex) =>
    {
      if (submitOp.HasError) {
        MessageBox.Show(submitOp.Error.ToString()); // To see a complete trace stack of the exception.
        submitOp.MarkErrorAsHandled();
      } // if
      else
        MessageBox.Show("Record Updated");
    };///Add ;

     

    I am not sure the code would work in your case

    Sunday, March 25, 2012 2:34 PM
  • Yeah! Some syntax errors due to my typing but ThaiCarrot fixed it for you already. You should see the error now so let see what is it.

    Sunday, March 25, 2012 3:45 PM
  • I tried it. But now following error is coming. Please visit the below link.

    https://skydrive.live.com/redir.aspx?cid=7529e2a34bfa8106&resid=7529E2A34BFA8106!202&parid=7529E2A34BFA8106!189

    Monday, March 26, 2012 5:45 AM
  • Please help me guys

    Tuesday, March 27, 2012 3:25 PM
  • are you the one that generated the Entity Framework model for the database? According to the error message, the table that you're trying to do the insert operation is a result of a DefiningQuery and it sounded like you're missing an <InsertFunction> element.

    Tuesday, March 27, 2012 4:11 PM
  • Atinesh,

    Here your insert code at the client:

                Newsletters_Table ex = new Newsletters_Table();
                ex.NL_ID = 10;
                ex.EmailAdd = "abc@gmail.com";
                ctx.Newsletters_Tables.Add(ex);
                ctx.SubmitChanges();

    Here code at your domain contaext. it was generated automatically when you create your DomainService correctly, you shoud nothing to do with this,

            public void InsertNewsletters_Table(Newsletters_Table newsletters_Table)
            {
                if ((newsletters_Table.EntityState != EntityState.Detached))
                {
                    this.ObjectContext.ObjectStateManager.ChangeObjectState(newsletters_Table, EntityState.Added);
                }
                else
                {
                    this.ObjectContext.Newsletters_Table.AddObject(newsletters_Table);
                }
            }

    Look there is no "ADD" methodt that, but Ria Services know that add row is at this service:

    Tuesday, March 27, 2012 8:31 PM
  • Hi G20,

    J. Siahaan is right, I've insert function in my Domain Service class named "ATI Virtual Classroom.cs" you can see it here

    https://skydrive.live.com/redir.aspx?cid=7529e2a34bfa8106&resid=7529E2A34BFA8106!206&parid=7529E2A34BFA8106!204

    Metadata related to above file is 

    https://skydrive.live.com/redir.aspx?cid=7529e2a34bfa8106&resid=7529E2A34BFA8106!205&parid=7529E2A34BFA8106!204

    And one of the file in the Generated_Code folder which generates automatically is

    https://skydrive.live.com/redir.aspx?cid=7529e2a34bfa8106&resid=7529E2A34BFA8106!207&parid=7529E2A34BFA8106!204

    Thursday, March 29, 2012 3:24 PM
  • Hi G20 and J. Siahaan,

    Is the screen shot is enough or you need more information to answer this question.

    Friday, March 30, 2012 6:50 AM
  • I don't find anything that may indicate the error yet. Maybe you zipped a trimmed down version of your project that only contains the table structure that causing error and I'll try to make it compile and work for you. That's the only thing I can think of right now.

    Friday, March 30, 2012 3:44 PM
  • Hi G20 and J. Siahaan, 

    You are right. I applied the same procedure in a small application, just to test whether data is inserting in the table or not and it worked. But when I follow the same procedure in my project, then no error is coming while inserting in the database I checked it with Fiddler. And when I see the table data is not inserting. Now tell me what can I do..

    Saturday, March 31, 2012 3:22 PM
  • Hi all I solved It. Thanks you all for the support.

    Sunday, April 01, 2012 4:00 PM