none
add items to Table RRS feed

  • Question

  • Hi Everyone,

    i am new to Entity framework i am trying to insert record into table but i unable to insert


                

    Hera i want insert data into invoice table i wrote a code like following 

    Entities v = new Entities();
            protected void Page_Load(object sender, EventArgs e)
            {
            
             var states = from sta in v.Terms orderby sta.Description select sta;
             Terms.DataSource = states;
             Terms.DataValueField = "TermsID";
             Terms.DataTextField = "Description";
             Terms.DataBind();
             var vendors = from ven in v.Vendors orderby ven.Name select ven;
             Vendors.DataSource = vendors;
             Vendors.DataTextField = "Name";
             Vendors.DataValueField = "VendorID";
             Vendors.DataBind();
               }

            protected void Button1_Click(object sender, EventArgs e)
            {
               
                Invoices invo = new Invoices();
                 invo.InvoiceID = 194;
                invo.InvoiceNumber = "989319 - 457";
                invo.InvoiceDate = Convert.ToDateTime("2009-08-08");
                invo.DueDate = Convert.ToDateTime("2009-08-08");
                invo.PaymentDate = Convert.ToDateTime("2009 -09-08");
                invo.InvoiceTotal = 3813;
                invo.CreditTotal = 0;
                invo.Vendors = Vendors.SelectedValue;
                invo.Terms = Terms.SelectedValue;

                v.SaveChanges();

            }         

    but here invo.vendors and invo.Terms showing error ,how can i add records if any one know please help me

    Thanks in Advance


    srikanth

    Wednesday, March 21, 2012 9:13 AM

Answers

  • Hi Sunny0817 ,

    vendors and Terms are collection properties . ddlVendors.SelectedValue  and ddlTerms.SelectedValue will return a single object .

    Instead of this you have to assign a collection of vendors and terms to these properties.

    else you can do something like

    invo.Vendors  = v .Vendors.FindAll( t => t.VendorId == ddlVendors.SelectedValue ).ToList();

    Try this if it works.

    Thanks

    Siddhesh


    Siddhesh Sawant

    Monday, March 26, 2012 9:59 AM

All replies

  • Hi sunny0817,

    Welcome to MSDN Forum.

    I changed the type to question, it will help you to solve the issue more effectively.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, March 22, 2012 9:50 AM
    Moderator
  • Hi Sunny0817 ,

    What is exact error you are getting ? Can you put some more description?

    Thanks

    Siddhesh


    Siddhesh Sawant

    Thursday, March 22, 2012 11:01 AM
  • Hi Sunny0817,

    As per my understanding , Entities  is your refrence to edmx .

    You  have to write something like

     v.invoices.Add(invo),

    and then do save changes. See if it works?

    Thanks

    Siddhesh


    Siddhesh Sawant

    Thursday, March 22, 2012 11:06 AM
  • Hi Siddhesh S,

    Welcome to MSDN Forum.

    Could you please post the error message here? This is, so we can help you more effectively.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Friday, March 23, 2012 5:29 AM
    Moderator
  • Hi Allen _Li_1988

    Sunny0817 has started this thread . I have already asked him to give exact error .

    Thanks

    Siddhesh


    Siddhesh Sawant

    Friday, March 23, 2012 6:04 AM
  • hi sunny0817,

    i think u have to change the object into collection in the following code 

      invo.Vendors = Vendors.SelectedValue;

      invo.Terms = Terms.SelectedValue;

    i need more details regarding the type of Vendors and Terms.

    in my view SelectedValue will give only single Objects and Not a Collection

    check it out 

    regards

    Chidambara Jagannathan K

    Friday, March 23, 2012 11:14 AM
  • Hi every one,

    sorry for late replay and thanks for your response ,i am getting error here

                 invo.Vendors = Convert.ToInt32((ddlVendors.SelectedValue));
                invo.Terms = Convert.ToInt32((ddlTerms.SelectedValue))

    like

    "Cannot implicitly convert type 'int' to 'WebApplication1.Vendors' "
    "Cannot implicitly convert type 'int' to 'WebApplication1.Terms' "

    if i put code like following

             invo.Vendors = (Vendors)Convert.ToInt32((ddlVendors.SelectedValue));
                invo.Terms = (Terms)Convert.ToInt32((ddlTerms.SelectedValue));

    then also i am getting same error.


    srikanth

    Monday, March 26, 2012 9:35 AM
  • Hi Sunny0817 ,

    vendors and Terms are collection properties . ddlVendors.SelectedValue  and ddlTerms.SelectedValue will return a single object .

    Instead of this you have to assign a collection of vendors and terms to these properties.

    else you can do something like

    invo.Vendors  = v .Vendors.FindAll( t => t.VendorId == ddlVendors.SelectedValue ).ToList();

    Try this if it works.

    Thanks

    Siddhesh


    Siddhesh Sawant

    Monday, March 26, 2012 9:59 AM
  • http://www.youtube.com/playlist?list=PL2EB52A543B3188A7&feature=view_all maybe this playlist will help you. Soon I will upload a new video where you kan insert items to your database, but you can download my whole project here http://adf.ly/6fmsn . In the movies you have also a lot of links that can help you.

    Tuesday, March 27, 2012 8:03 PM