1

While trying to save my changes in my database table EmpTable, no commit in database takes place.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Linq;

namespace ConsoleApplication_
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Working Fine");

            DataBase1ClassDataContext dc = new DataBase1ClassDataContext(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True");
            Table<EmpTable> t = dc.EmpTables;
    
            foreach (var i in t)
            {
                Console.WriteLine(i.Id+" "+i.name+" "+i.Address+" "+i.contact+" "+i.GetType());
            }
            List<EmpTable> l = dc.EmpTables.ToList();
            for (int j = 0; j < l.Count(); j++)
            {
                Console.WriteLine(l[j].Id + " " + l[j].name + " " + l[j].Address + " " + l[j].contact + " " + l[j].GetType());
            }
            label1:
            Console.WriteLine("1 for inserting data, 2 for exit");
            int a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine();
            EmpTable obj = new EmpTable();
            switch (a)
            {
                case 1:
                    {
                        Console.Write("Enter a new unique ID: ");
                        obj.Id = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("Your new id is : "+obj.Id);
                        Console.Write("Enter name: ");
                        obj.name = Console.ReadLine();
                        Console.Write("Enter address: ");
                        obj.Address = Console.ReadLine();
                        Console.Write("Enter contact no.: ");
                        obj.contact = Console.ReadLine();
                        dc.EmpTables.InsertOnSubmit(obj);
                        dc.SubmitChanges();
                        Console.WriteLine("INSERTED. . . :)");
                        dc = new DataBase1ClassDataContext();
                        t = dc.EmpTables;
                        foreach (var i in t)
                        {
                            Console.WriteLine(i.Id + " " + i.name + " " + i.Address + " " + i.contact + " " + i.GetType());
                        }
                        Console.WriteLine("To operate again,");
                        goto label1;
                    }
                case 2:
                    Console.WriteLine("Press any key to exit,\nExiting . . .");
                    break;
                default:
                    Console.WriteLine("Wrong Entry, Try Again");
                    goto label1;
            }
            Console.ReadKey();
        }
    }
}

Whenever I run my code, it shows only the data currently from the table which I have manually inserted.

Please help.

Important: I have a primary key in my table already present.

Dale K
  • 25,246
  • 15
  • 42
  • 71
shubham shaw
  • 113
  • 12
  • 3
    I have not seen anybody 's code that was using goto for 30 years already. I was not sure that is still exist. – Serge Jun 20 '21 at 15:59
  • 1
    The usual localdb confusion. Your database is overwritten on each run. Don't use `AttachDbFilename` but `database=Database1`. – Gert Arnold Jun 20 '21 at 19:34
  • In the VS Solution Explorer, have you checked that for Database1.mdf the "Copy to Output Directory" property is "Copy if newer" instead of the default "Copy always"? – AlwaysLearning Jun 20 '21 at 21:45
  • Does this answer your question? [Why saving changes to a database fails?](https://stackoverflow.com/questions/17147249/why-saving-changes-to-a-database-fails) – SMor Jun 20 '21 at 23:32
  • How to check the settings in Database1.mdf the "Copy to Output Directory" property "Copy if newer" instead of the default "Copy always" – shubham shaw Jun 21 '21 at 03:12

1 Answers1

1

I have got my answer, by changing my database1.mdf "copy to output directory" As do not copy, this option is in properties of database, and then a new error is explored which I rectified by giving the full path of db in configuration as it always points to server explorer and real db is in solution explorer. Thank you so much guys. Happy Programming

shubham shaw
  • 113
  • 12