DateTime error inserting to a Database from Visual Studio ADO (SqlException: varchar data type to datetime produced a value outside of range)










1















I'm working with MVVM in WPF and I have ADO for the model to use the database connection.



Now I'm trying to save new Artikel to database from my project, and I'm getting a SqlException: The conversion of the varchar data type to datetime produced a value outside of range.



This error is the same if I use that value with DateTime.Now or doing a select from the same database table using another register that should be in the correct format. I also tried to use ToShortDateString, ToUniversalTime, Convert.ToDateTime to format the Date but nothing worked.



In the database this DateTime can't be null, and I can't set it to nullable because it's a field from Sage Database that I can't modify.



If I don't specify that field value, it gives me a different error:
SqlException: The conversion of datetime2 data type to datetime produced a value outside of range.
The instruction was finished.



I really don't know how to insert a new Artikel with this problem.



Insert code:



//Getting another value from the table to try this format
Articulos articlePerData = (from t in contextDomoli.Articulos
where t.CodigoEmpresa == 1
select t).FirstOrDefault();
Articulos article = new Articulos()

CodigoEmpresa = 1,
CodigoArticulo = cost.RefDomoli,
DescripcionArticulo = cost.Descripcio,
TipoArticulo = tipoArticulo,
CodigoAlternativo = EAN,
C_DataCanviPreu = articlePerData.C_DataCanviPreu,
;
contextDomoli.Articulos.Add(article);
contextDomoli.SaveChanges();


Database field










share|improve this question
























  • From the exception you posted, I see that the type of the problematic column is the old datetime (and not datetime2), so it may be something that's related to its limitations. Try this: make the required Articulos .NET property a string type instead of DateTime, and then convert your DateTime value to string by using the standard format, e.g: myDateTime.ToString("s");

    – HeyJude
    Nov 19 '18 at 12:47












  • It's a problem for me to edit the .NET property. It's auto-generated from the Database (Sage, can't edit too). If I understood correctly your answer, I think its not viable in this case. I need some way to pass this DateTime value correct for the Database without editing types.

    – Scud
    Dec 5 '18 at 16:53











  • I've tried to save my date into a string value with that standard format, and then reconvert that into DateTime without editing the database. Its working now! code DateTime data = DateTime.Now; string strData = data.ToString("s"); data = DateTime.Parse(strData);

    – Scud
    Dec 5 '18 at 16:53












  • it crashes again, the same error with the same code from the last comment, I really don't know why.

    – Scud
    Dec 18 '18 at 10:58






  • 1





    We finally found it, another worker set a datatrigger that is doing this insert crash... solved deactivating the trigger.

    – Scud
    Dec 24 '18 at 8:07















1















I'm working with MVVM in WPF and I have ADO for the model to use the database connection.



Now I'm trying to save new Artikel to database from my project, and I'm getting a SqlException: The conversion of the varchar data type to datetime produced a value outside of range.



This error is the same if I use that value with DateTime.Now or doing a select from the same database table using another register that should be in the correct format. I also tried to use ToShortDateString, ToUniversalTime, Convert.ToDateTime to format the Date but nothing worked.



In the database this DateTime can't be null, and I can't set it to nullable because it's a field from Sage Database that I can't modify.



If I don't specify that field value, it gives me a different error:
SqlException: The conversion of datetime2 data type to datetime produced a value outside of range.
The instruction was finished.



I really don't know how to insert a new Artikel with this problem.



Insert code:



//Getting another value from the table to try this format
Articulos articlePerData = (from t in contextDomoli.Articulos
where t.CodigoEmpresa == 1
select t).FirstOrDefault();
Articulos article = new Articulos()

CodigoEmpresa = 1,
CodigoArticulo = cost.RefDomoli,
DescripcionArticulo = cost.Descripcio,
TipoArticulo = tipoArticulo,
CodigoAlternativo = EAN,
C_DataCanviPreu = articlePerData.C_DataCanviPreu,
;
contextDomoli.Articulos.Add(article);
contextDomoli.SaveChanges();


Database field










share|improve this question
























  • From the exception you posted, I see that the type of the problematic column is the old datetime (and not datetime2), so it may be something that's related to its limitations. Try this: make the required Articulos .NET property a string type instead of DateTime, and then convert your DateTime value to string by using the standard format, e.g: myDateTime.ToString("s");

    – HeyJude
    Nov 19 '18 at 12:47












  • It's a problem for me to edit the .NET property. It's auto-generated from the Database (Sage, can't edit too). If I understood correctly your answer, I think its not viable in this case. I need some way to pass this DateTime value correct for the Database without editing types.

    – Scud
    Dec 5 '18 at 16:53











  • I've tried to save my date into a string value with that standard format, and then reconvert that into DateTime without editing the database. Its working now! code DateTime data = DateTime.Now; string strData = data.ToString("s"); data = DateTime.Parse(strData);

    – Scud
    Dec 5 '18 at 16:53












  • it crashes again, the same error with the same code from the last comment, I really don't know why.

    – Scud
    Dec 18 '18 at 10:58






  • 1





    We finally found it, another worker set a datatrigger that is doing this insert crash... solved deactivating the trigger.

    – Scud
    Dec 24 '18 at 8:07













1












1








1








I'm working with MVVM in WPF and I have ADO for the model to use the database connection.



Now I'm trying to save new Artikel to database from my project, and I'm getting a SqlException: The conversion of the varchar data type to datetime produced a value outside of range.



This error is the same if I use that value with DateTime.Now or doing a select from the same database table using another register that should be in the correct format. I also tried to use ToShortDateString, ToUniversalTime, Convert.ToDateTime to format the Date but nothing worked.



In the database this DateTime can't be null, and I can't set it to nullable because it's a field from Sage Database that I can't modify.



If I don't specify that field value, it gives me a different error:
SqlException: The conversion of datetime2 data type to datetime produced a value outside of range.
The instruction was finished.



I really don't know how to insert a new Artikel with this problem.



Insert code:



//Getting another value from the table to try this format
Articulos articlePerData = (from t in contextDomoli.Articulos
where t.CodigoEmpresa == 1
select t).FirstOrDefault();
Articulos article = new Articulos()

CodigoEmpresa = 1,
CodigoArticulo = cost.RefDomoli,
DescripcionArticulo = cost.Descripcio,
TipoArticulo = tipoArticulo,
CodigoAlternativo = EAN,
C_DataCanviPreu = articlePerData.C_DataCanviPreu,
;
contextDomoli.Articulos.Add(article);
contextDomoli.SaveChanges();


Database field










share|improve this question
















I'm working with MVVM in WPF and I have ADO for the model to use the database connection.



Now I'm trying to save new Artikel to database from my project, and I'm getting a SqlException: The conversion of the varchar data type to datetime produced a value outside of range.



This error is the same if I use that value with DateTime.Now or doing a select from the same database table using another register that should be in the correct format. I also tried to use ToShortDateString, ToUniversalTime, Convert.ToDateTime to format the Date but nothing worked.



In the database this DateTime can't be null, and I can't set it to nullable because it's a field from Sage Database that I can't modify.



If I don't specify that field value, it gives me a different error:
SqlException: The conversion of datetime2 data type to datetime produced a value outside of range.
The instruction was finished.



I really don't know how to insert a new Artikel with this problem.



Insert code:



//Getting another value from the table to try this format
Articulos articlePerData = (from t in contextDomoli.Articulos
where t.CodigoEmpresa == 1
select t).FirstOrDefault();
Articulos article = new Articulos()

CodigoEmpresa = 1,
CodigoArticulo = cost.RefDomoli,
DescripcionArticulo = cost.Descripcio,
TipoArticulo = tipoArticulo,
CodigoAlternativo = EAN,
C_DataCanviPreu = articlePerData.C_DataCanviPreu,
;
contextDomoli.Articulos.Add(article);
contextDomoli.SaveChanges();


Database field







c# database wpf linq insert






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 15:12









FredM

116213




116213










asked Nov 14 '18 at 12:15









ScudScud

62




62












  • From the exception you posted, I see that the type of the problematic column is the old datetime (and not datetime2), so it may be something that's related to its limitations. Try this: make the required Articulos .NET property a string type instead of DateTime, and then convert your DateTime value to string by using the standard format, e.g: myDateTime.ToString("s");

    – HeyJude
    Nov 19 '18 at 12:47












  • It's a problem for me to edit the .NET property. It's auto-generated from the Database (Sage, can't edit too). If I understood correctly your answer, I think its not viable in this case. I need some way to pass this DateTime value correct for the Database without editing types.

    – Scud
    Dec 5 '18 at 16:53











  • I've tried to save my date into a string value with that standard format, and then reconvert that into DateTime without editing the database. Its working now! code DateTime data = DateTime.Now; string strData = data.ToString("s"); data = DateTime.Parse(strData);

    – Scud
    Dec 5 '18 at 16:53












  • it crashes again, the same error with the same code from the last comment, I really don't know why.

    – Scud
    Dec 18 '18 at 10:58






  • 1





    We finally found it, another worker set a datatrigger that is doing this insert crash... solved deactivating the trigger.

    – Scud
    Dec 24 '18 at 8:07

















  • From the exception you posted, I see that the type of the problematic column is the old datetime (and not datetime2), so it may be something that's related to its limitations. Try this: make the required Articulos .NET property a string type instead of DateTime, and then convert your DateTime value to string by using the standard format, e.g: myDateTime.ToString("s");

    – HeyJude
    Nov 19 '18 at 12:47












  • It's a problem for me to edit the .NET property. It's auto-generated from the Database (Sage, can't edit too). If I understood correctly your answer, I think its not viable in this case. I need some way to pass this DateTime value correct for the Database without editing types.

    – Scud
    Dec 5 '18 at 16:53











  • I've tried to save my date into a string value with that standard format, and then reconvert that into DateTime without editing the database. Its working now! code DateTime data = DateTime.Now; string strData = data.ToString("s"); data = DateTime.Parse(strData);

    – Scud
    Dec 5 '18 at 16:53












  • it crashes again, the same error with the same code from the last comment, I really don't know why.

    – Scud
    Dec 18 '18 at 10:58






  • 1





    We finally found it, another worker set a datatrigger that is doing this insert crash... solved deactivating the trigger.

    – Scud
    Dec 24 '18 at 8:07
















From the exception you posted, I see that the type of the problematic column is the old datetime (and not datetime2), so it may be something that's related to its limitations. Try this: make the required Articulos .NET property a string type instead of DateTime, and then convert your DateTime value to string by using the standard format, e.g: myDateTime.ToString("s");

– HeyJude
Nov 19 '18 at 12:47






From the exception you posted, I see that the type of the problematic column is the old datetime (and not datetime2), so it may be something that's related to its limitations. Try this: make the required Articulos .NET property a string type instead of DateTime, and then convert your DateTime value to string by using the standard format, e.g: myDateTime.ToString("s");

– HeyJude
Nov 19 '18 at 12:47














It's a problem for me to edit the .NET property. It's auto-generated from the Database (Sage, can't edit too). If I understood correctly your answer, I think its not viable in this case. I need some way to pass this DateTime value correct for the Database without editing types.

– Scud
Dec 5 '18 at 16:53





It's a problem for me to edit the .NET property. It's auto-generated from the Database (Sage, can't edit too). If I understood correctly your answer, I think its not viable in this case. I need some way to pass this DateTime value correct for the Database without editing types.

– Scud
Dec 5 '18 at 16:53













I've tried to save my date into a string value with that standard format, and then reconvert that into DateTime without editing the database. Its working now! code DateTime data = DateTime.Now; string strData = data.ToString("s"); data = DateTime.Parse(strData);

– Scud
Dec 5 '18 at 16:53






I've tried to save my date into a string value with that standard format, and then reconvert that into DateTime without editing the database. Its working now! code DateTime data = DateTime.Now; string strData = data.ToString("s"); data = DateTime.Parse(strData);

– Scud
Dec 5 '18 at 16:53














it crashes again, the same error with the same code from the last comment, I really don't know why.

– Scud
Dec 18 '18 at 10:58





it crashes again, the same error with the same code from the last comment, I really don't know why.

– Scud
Dec 18 '18 at 10:58




1




1





We finally found it, another worker set a datatrigger that is doing this insert crash... solved deactivating the trigger.

– Scud
Dec 24 '18 at 8:07





We finally found it, another worker set a datatrigger that is doing this insert crash... solved deactivating the trigger.

– Scud
Dec 24 '18 at 8:07












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300038%2fdatetime-error-inserting-to-a-database-from-visual-studio-ado-sqlexception-var%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300038%2fdatetime-error-inserting-to-a-database-from-visual-studio-ado-sqlexception-var%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党