Solved

C# with ADO

Posted on 2014-04-27
27
351 Views
Last Modified: 2016-02-18
Hi All ,

I have connected my appication to SQL database through  ADO.NET Entity Data Model.

Now all work fine I can query database but when I'm trying to insert data and run the application I get notimplemenentedException message.

it seems that the line de.AddtoCustomer(newCust) doesn't work.

here is my code:
class Program
    {
        static void Main(string[] args)
        {
            NorthwindEntities1 de = new NorthwindEntities1();


            Customer newCust = new Customer();

            newCust.CompanyName = "Moti Mashiah";
            newCust.ContactName = "Nicole Mashiah";
            newCust.City = "Tel Aviv";
            newCust.Country = "Israel";
            newCust.ContactTitle = "balll";
            newCust.Address = "puccini 17";

            de.AddtoCustomer(newCust);

            de.SaveChanges();

            var cust = from c in de.Customers
                       where c.Address == "puccini 17"
                       select c;


            Console.WriteLine(cust.Count());
            Console.ReadLine();
        }
    }
}

Open in new window

0
Comment
Question by:Moti Mashiah
  • 16
  • 11
27 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40026066
Hi motimash;

I believe you are using Linq to Entity Framework and that you are also using DbContext as opposed to ObjectContext. The DbContext does not implement the method AddtoCustomer and therefore the most likely cause of the error. Try changing this line in your code, de.AddtoCustomer(newCust);, with this line:

de.Customers.Add(newCust);
0
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 40026468
Hi Fernando ,

After I try your suggestion I got this error, please see attachment.
Untitled.png
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40026485
Hi motimash;

OK lets try it this way. If you can attach the files under the EDMX node in Solution Explorer with the extension .cs, these files have the definitions for the classes of the DbContext and the code that maps to the Table in the database. do not paste the code into the editor but upload as text files to the post. If in Solution Explorer you can not expand the EDMX node make sure you click on the icon on the top of Solution Explorer to display all files.
0
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 40028072
Here is what I did:

Back to write the code that way:
 static void Main(string[] args)
        {
            rmsmasterdbtestEntities1 de = new rmsmasterdbtestEntities1();

            Customer newCust = new Customer();

            newCust.FirstName = "Bryan";
            newCust.LastName = "Faley";
            newCust.City = "Cayman Islane";
            newCust.Status = "No available";
            

            de.AddToCustomer(newCust);

            de.SaveChanges();

            var cayman = from c in de.Customers
                         where c.Status == "No available"
                         select c;

            Console.WriteLine(cayman.Count());

            Console.ReadLine();

Open in new window


Now I got the same error (see the attachment) so I clicked on the little box and generated  code into model1.context.cs see attachment figure2

This is the code:
internal void AddToCustomer(Customer newCust)
        {
            throw new NotImplementedException();
        }

Open in new window


Now when I debug the application I'm getting "NotImplementedException" of course.

Please help..
figure1.JPG
figure2.JPG
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40028768
Hi motimash;

To your statement in the original question,
it seems that the line de.AddtoCustomer(newCust) doesn't work.
Actually it is doing what it was set up to do if it was called and that is to throw a NotImplementedException as can be seen in the box marked "This is the code".

Seeming you did not post the complete files I asked for please answer the following questions.

What version of the Entity Framework are you using?

Are you using DbContext or ObjectContext?

What were the steps you took to create the model?

Are you using Database First or Model First approach?

Did you write this or was it created automatically by the template
        internal void AddToCustomer(Customer newCust)
        {
            throw new NotImplementedException();
        }
0
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 40029482
Hi Fernando ,

Sorry about that I thought it was enough information what I provide above.

Here is the answers:

1. the Entity framework version is 5
2. I'm using DBcontex

3. the steps for creating model
a. connected to the database server and the database
b. right Click on my solution and create new item
c. choose data -> ADO.NET Entity Data Model and Add
d. Generate from database
model.JPG
0
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 40029495
Sorry Fernando I wasn't sure regarding your last question.

please , see attachment in my last thread I hope all the information I provided there will help.



Thanks ,
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40029530
Are you using a Stored Procedure which is called AddToCustomers(...) ? Otherwise I can not understand why it is creating such a function when you created the model.
0
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 40029553
Sorry , I think I really make it confusing.

No I'm not using store procedure.

I typed this command in my code and then click on the little blue button (see attachment)
that's what actually create the method in the model1.context class.
 see model attachment.
Capture.JPG
model.JPG
0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 500 total points
ID: 40029597
Try this then, seeming that this function did not exist before you typed it in the little blue line allowed you to create it later and just added what is called a stub. When it did this it placed in its body a call to not implemented exception just in case someone calls it before you have implemented that function. So comment out the function below where it shows up.

internal void AddToCustomer(Customer newCust)
{
    throw new NotImplementedException();
}

Open in new window

Now remove this line of code:   de.AddToCustomer(newCust);

and replace it with this line of code :   de.Customers.Add(newCust);

The last time you tried this it was using the function AddToCustomer as the target of the call.
0
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 40029738
I changed it.

Please see attachment in this error.
error.JPG
0
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 40029769
Here is the error I'm getting:

System.Data.Entity.Validation.DbEntityValidationException was unhandled
  HResult=-2146232032
  Message=Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
  Source=EntityFramework
  StackTrace:
       at System.Data.Entity.Internal.InternalContext.SaveChanges()
       at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
       at System.Data.Entity.DbContext.SaveChanges()
       at ConsoleApplication9.Program.Main(String[] args) in c:\Temp\Progrmming\ConsoleApplication9\ConsoleApplication9\Program.cs:line 30
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:
0
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 40029772
it seems that the error this time comes from the save line,
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40029793
In the image you posted there is a hot spot called "View Details...", click on that and in the new window that opens expand the node and post the exception message and also find the inner exception and post that message as well.
0
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 40029826
innerException - null
exceptionMessage.JPG
0
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 40029843
I think I got it there is some line that can't be null...that's why I can't creating the new customer.

Am I right?
0
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 40029852
Sorry very new for this world.:)
0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 500 total points
ID: 40029927
To your statement, "I think I got it there is some line that can't be null...that's why I can't creating the new customer.", I can't be sure from the posting, but go to the database and make sure that all the properties that state NOT NULL are being set in your program.
0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 500 total points
ID: 40029943
Also instead of clicking on the hot spot "View Details" you can click on the one that says Copy to Clipboard and and paste that into the post here. That will allow us to see the complete error message.
0
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 40030004
Hi Fernando ,

Here you go;

System.Data.Entity.Validation.DbEntityValidationException was unhandled
  HResult=-2146232032
  Message=Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
  Source=EntityFramework
  StackTrace:
       at System.Data.Entity.Internal.InternalContext.SaveChanges()
       at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
       at System.Data.Entity.DbContext.SaveChanges()
       at ConsoleApplication9.Program.Main(String[] args) in c:\Temp\Progrmming\ConsoleApplication9\ConsoleApplication9\Program.cs:line 52
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:


Now I'm trying to insert all the not null columns  in to the insert command and see if it will solve the problem.
0
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 40030060
Hi Fernando ,

I'm trying to add all these not null columns and the datetime column give me red line.

Please can you look at the attachment...
newcust.JPG
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40030150
Hi motimash;

You need to initialize the date with the correct format, use one of the following.

newCust.LastUpdated = new DateTime(2014, 12, 3);
or
newCust.LastUpdated = DateTime.Parse("12/3/2014");
0
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 40030193
Thanks it works now but still the solution for the create new customer is not working.

Do you have any other suggestion for this issue.


Thanks,
0
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 40030233
Actually , I solved the problem with the linq insert new customer after I added all the not null columns.


Thank you very much for great help.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40030241
If you can zip up the complete project including the database you are using, as long as the database is a test database and not one with real data I will take a look at the project to see what is going on.

You will need to upload the zip file to some web site such as Microsoft OneDrive which is a free service to join and place in a public folder so we can access it and place the link address here. Do not use EE for they will NOT allow all file types in the zip file.
0
 
LVL 1

Author Closing Comment

by:Moti Mashiah
ID: 40030244
Thank you Fernando
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40030249
Not a problem motimash, glad to help.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now