0

I am using the following code to update clock in and clock out times in the record table but it doesn't save the data to the database. I checked the mdf file with sql management studio and there is no data saved. What am I doing wrong?

private void btClockIn_Click(object sender, EventArgs e)
    {
        using (TimeKeepEntities ctx = new TimeKeepEntities())
        {
          // if employee clocked out add new clock in record else update existing record with clock out time
            if (cmbProjects.Enabled)
            {
                tblTimeRecord record = new tblTimeRecord();
                record.RecordID = DateTime.Now; 
                record.EmployeeID = (int)cmbEmployees.SelectedValue;
                record.ProjectID = (int)cmbProjects.SelectedValue;
                record.ClockIn = DateTime.Now;


                ctx.tblTimeRecords.Add(record);
                ctx.SaveChanges();

                update_cmbProjects_btClockIn();

            }
            else
            {
                tblTimeRecord record = (from r in ctx.tblTimeRecords
                                        where r.EmployeeID == (int)cmbEmployees.SelectedValue && !r.ClockOut.HasValue
                                        select r).FirstOrDefault();
                record.ClockOut = DateTime.Now;
                ctx.SaveChanges();

                update_cmbProjects_btClockIn();
            }
        }
    }

EDIT 1: Here is my app.config file (including what looks like a connection string)

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <connectionStrings>
    <add name="TimeKeepEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\TimeKeep.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
</configuration>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mr1159pm
  • 774
  • 1
  • 10
  • 21
  • Does it error at all? (the above looks fine to me, so my bet would be its something to do with your actual connection string or permissions) – undefined Sep 19 '12 at 22:17
  • no, no errors whatsoever. I looked at the entity data model in debug mode and it looks like the data is being saved to the model but not the database. – Mr1159pm Sep 19 '12 at 22:19
  • 2
    Are you sure you're looking at the right database? EF will often create a database using convention naming if you don't specify one. Check your connection strings to make sure (you also have to name the connection string correctly, or it will use a default). – Erik Funkenbusch Sep 19 '12 at 22:24
  • 1
    Try running SqlProfiler and see if the sql command is getting to the db you think it is. Or try EFProf. – Dave Hogan Sep 19 '12 at 22:37
  • Can you show us your **connection string**? Are you using the `AttachDbFileName=.....` feature by any chance? – marc_s Sep 19 '12 at 23:36
  • where can I find the connection string?( I am new to C#). I added the db to the project using wizard if that's what you are asking for. – Mr1159pm Sep 20 '12 at 00:22
  • @DaveHogan I don't think I have sqlProfiler. I am using sql and VS express editions 2012(both. – Mr1159pm Sep 20 '12 at 00:24
  • I's likely that you are looking at the wrong DB as @MystereMan said. The database is being attached at run time. In the SQL Management Studio, make sure the files of the database you're looking at match App_Data/TimeKeep.mdf. – Andre Pena Sep 20 '12 at 00:40
  • I'd also set a break point in that handler to make sure this code is getting called in the first place. – Jay Sep 20 '12 at 00:46

1 Answers1

1

As MM suggested in the comments, I also suspect that you're looking at the wrong MDF file the first time. With SQL Express those wizards tend to make copies of it and the final copy that the application is using is sometimes a different location than the one created by the wizard. The final one that is used is usually in the App_Data folder of your solution but you can trace in Debug in the save changes to be sure.

Here is an answer to a similar problem: Entity Framework 4.1 is not adding any rows to SQL Server Express database

Community
  • 1
  • 1
Turnkey
  • 9,266
  • 3
  • 27
  • 36
  • Thanks! I used ssms to watch the table while I ran the code and indeed the records were added. As described in that related problem the mdf copying by VS was replacing the the db every time and that's why I didn't see any data. Thanks again! – Mr1159pm Sep 20 '12 at 01:16