Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8277239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:37:55+00:00 2026-06-08T08:37:55+00:00

My table structure in sql server is : TableId int (Pk) identity Data string

  • 0

My table structure in sql server is :

TableId int (Pk) identity

Data string

DateNTime DateTime

My method is::

public int insertData(string data){

Date= DateTime.Now;

Table table= new Table();

table.Data= data;

table.DateNTime=Date;

 this.DataContext.Set<Table>().Add(table);

this.DataContext.SaveChanges();

return this.DataContext.Tables.Single(b => b.DateNTime == Date).TableId

}

The data insertion happens without any problems but in 9 out of 10 times while returning TableId I get the exception “sequence contains no elements”

Could it be that before the table row is saved the select command is fired and I get this error, if so what do I do?

Thanks
Arnab

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T08:37:58+00:00Added an answer on June 8, 2026 at 8:37 am

    SaveChanges() is a synchronous “blocking” operation, it doesn’t return before the transaction that saves the date is committed. So, when you call the query the date is definitely saved in the database.

    I think, it is a precision/rounding problem. If you look at the precision of a DateTime in .NET you’ll see (for example in the TimeOfDay property):

    TimeOfDay of .NET DateTime type: 10:32:51.0312500

    So, the precision is 10E-7 seconds. A datetime in SQL Server has only 10E-3 seconds precision and the .NET DateTime is saved like this in the database:

    Column value of SQL Server datetime type: 10:32:51.030

    So, it is rounded to three digits. When you run the query the .NET DateTime is transmitted with high precision (as type datetime2(7) in SQL Server) …

    WHERE [Extent1].[MyDateTimeColumn] = @p__linq__0',
        N'@p__linq__0 datetime2(7)', @p__linq__0='2012-05-18 10:32:51.0312500'
    

    … and the equality comparison fails because

    2012-05-18 10:32:51.0312500 != 2012-05-18 10:32:51.030

    If you want a higher precision use a datetime2(7) as type in SQL Server that matches the .NET DateTime type. Or avoid such queries for equality and instead query for an interval of +/- 1 second or something around your DateTime value, like so:

    var date1 = Date.AddSeconds(-1);
    var date2 = Date.AddSeconds( 1);
    return this.DataContext.Tables
        .Single(b => b.DateNTime >= date1 && b.DateNTime <= date2)
        .TableId;
    

    (Not a good solution of course if you save faster than every 2nd second, Single might fail with “Sequence contains more than one element” exception.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sql Server 2005 Table Structure CREATE TABLE [dbo].[Rate]( [RateID] [bigint] IDENTITY(1,1) NOT NULL, [PairID]
I have this table structure on a SQL Server 2008 R2 database: CREATE TABLE
I have a hierarchical (tree structure) SQL Server table TEmployee with following columns Id
I have a table in SQL Server 2008 Table Name : tbl_device Table Structure:
I've got a table X with the following structure in sql server 2008R2 db
SQL STRUCTURE CREATE TABLE IF NOT EXISTS `map` ( `id` int(11) NOT NULL AUTO_INCREMENT,
I'm building multi tenant application with shared table structure using Microsoft SQL Server. I
I have a single row of data in a SQL Server table that I'm
Good Morning All. I have a table structure (I am using SQL SERVER 2000)
Given the following simple table structure (SQL Server 2008), I want to be able

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.