Click here to Skip to main content
16,017,261 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a problem with insert variables types from DateTime.

I have the following Table in a DB in SQL Server Express 2005:
USE [SCCS]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Log](
	[TimeID] [datetime] NOT NULL,
	[LogType] [int] NULL,
	[LogDesc] [nvarchar](50) COLLATE Modern_Spanish_CI_AS NULL,
 CONSTRAINT [PK_Log] PRIMARY KEY CLUSTERED 
(
	[TimeID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]


Then I have the following Stored Procedure in the DB:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[spInsertLog](@UpdatedProdData nText)
AS
DECLARE @hDoc int   
 
 exec sp_xml_preparedocument @hDoc OUTPUT,@UpdatedProdData 

INSERT INTO [SCCS].[dbo].[Log]
           ([TimeID]
           ,[LogType]
           ,[LogDesc])
 SELECT 
	XMLProdTable.TimeID,
	XMLProdTable.LogType,
	XMLProdTable.LogDesc
    FROM OPENXML(@hDoc, 'ArrayOfLog/Log', 2)   
       WITH (
				TimeID datetime,
				LogType int,
				LogDesc nvarchar(50)

            ) XMLProdTable

EXEC sp_xml_removedocument @hDoc
GO

SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO


Then in my C# Solution I include in my LINQ to SQL Class (*.DBML File) the Table [Log] and the Stored Procedure dbo.spInsertLog. Then I will include records to this Table with the following code:
using (TestDataContext oDataBase = new TestDataContext())
{
    Log[] aRecords = new Log[2];
    for (int i = 0; i < 2; i++)
    {
        Log oRecord = new Log();
        oRecord.TimeID = DateTime.Now.AddDays(Convert.ToDouble(i));
        oRecord.LogType = i;
        oRecord.LogDesc = "Desc: " + i.ToString();
        aRecords[i] = oRecord;
    }

    StringBuilder sBuilder = new StringBuilder();
    System.IO.StringWriter sWriter = new System.IO.StringWriter(sBuilder);
    XmlSerializer serializer = new XmlSerializer(typeof(Log[]));
    serializer.Serialize(sWriter, aRecords);
    oDataBase.spInsertLog(sBuilder.ToString());
}


When I call the line oDataBase.spInsertLog(sBuilder.ToString()); I receive a Exception:
SqlException
Conversion error when converting a string to datetime

Anyone can tell me I'm doing wrong?? Because if you change the DateTime type to long it works.
Posted
Comments
Umair Feroze 28-Jan-11 11:31am    
From this the only possibility I percieve is that, data is not correct in the xml file. I will suggest to verify the data from xml specially the TimeID node.
StefanHM 28-Jan-11 12:01pm    
The XML, I think see well:
{
<ArrayOfLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<log>
<timeid>2011-01-28T17:56:48.631096+01:00
<logtype>0
<logdesc>Desc: 0

<log>
<timeid>2011-01-29T17:56:48.631096+01:00
<logtype>1
<logdesc>Desc: 1

</ArrayOfLog>}

1 solution

I think that the date/time the XmlSerializer provides does not match the format the OPENSQL expects. If I recall correctly, XmlSerializer includes a time zone; I am not 100% certain that OPENXML supports it. One thing I'd try is replacing datetime with datetimeoffset in the stored procedure (the 'WITH' clause of OPENXML), and see if it fixes things.
 
Share this answer
 
Comments
StefanHM 31-Jan-11 1:58am    
First at all, thank you for you help, but it doesn't work because the Type datetimeoffset don't exist in MS SQL Server Express 2005. I see in an articel from MSDN (http://technet.microsoft.com/en-us/library/bb677236.aspx) that must be work in MS SQL Server 2008.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900