Showing posts with label format. Show all posts
Showing posts with label format. Show all posts

Monday, March 12, 2012

Mysterious Date Problem

Is there a way to either set Sql Server 2000 or ASP.net datetime fields to a standard format. The problem is that I am passing correct datetime fields using stored procedures and keep getting "Cannot convert datetime into string". It seems to me that many other developers are having that same problem. I tried alot of different methods and still have the same problem. I'm using c# and I never had a problem with datetime fields when I was using vb.net. The problem is that SQl Server is returning datetime formats that are not compatible with c#. I have code that works in other projects but when I try to use that same code I get that conversion error. How do I set the datetime in SQl Server and ASP.Net when I run queries so that the datetime output is in mm/dd/yyyy?What is the statement where you get the error "Cannot convert datetime into string"|||well anytime I return a dataset and bind it to a datagrid I get a conversion error. Sometimes I even got 'The conversion of a char data type to a datetime data type resulted in an out-of-range'. When I ran a simple query in Query Analyzer like "select convert (datetime,'12/20/2003',101)" the output returns 2003-12-20 00:00:00.000
What I need is to be mm/dd/yyyy. Is there a way to change the default setting in Sql Server to that particular format instead of yyyy-mm-dd. Because I think C# is using mm-dd-yyyy as a default. In the asp net datagrid all I'm doing it just binding the dataset to the grid and I get that error? Thanks for your reply...|||you should do the formatting at the front end...for instance when you bind the result set to the datagrid...you can use the dataformatstring property of the datagrid columns to format it to mm/dd/yyyy format which is much simpler rather than enforcing the sql server to some other format..

hth|||Yah, I used that format and that is not the problem. The problem is that I can't even bind the dataset to the datagrid. Its in the binding that is causing the error. But thanks anyway.|||What is the code that you are using??|||basically I just call pass parameters to a method and it returns a dataset

datagrid.DataSource = ds;
datagrid.DataBind();

public DataSet GetOriginationReport( DateTime dtStartDate, DateTime dtEndDate)
{
// instantiate connection and command objects
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connString2"]);
SqlDataAdapter myCommand = new SqlDataAdapter("rptSelectReport", myConnection);
myCommand.SelectCommand.CommandType = CommandType.StoredProcedure;

myCommand.SelectCommand.Parameters.Add("@.pStartDate", SqlDbType.DateTime );
myCommand.SelectCommand.Parameters.Add("@.pEndDate", SqlDbType.DateTime );

myCommand.SelectCommand.Parameters["@.pStartDate"].Value = dtStartDate;
myCommand.SelectCommand.Parameters["@.pEndDate"].Value = dtEndDate;

DataSet myDS = new DataSet();
myCommand.Fill(myDS,"viewReportPipe");
return myDS;
}

===============================
Stored Procedure
===============================
CREATE PROCEDURE rptSelectReport
(
@.pStartDate DateTime,
@.pEndDate DateTime,
)
As

Select * from viewReportPipe Where AppDate Between @.pStartDate And @.pEndDate|||What result do u get when u run this query in Sql analyzer??
Select * from viewReportPipe Where AppDate Between @.pStartDate And @.pEndDate|||well I get a list of results. The problem is occuring in the binding. When I run the query and bind it to a datagrid it works. But I have sorting enable in the datagrid and when I run that same query and bind it to the grid it fails and gives me that "Cannot convert datetime to string" error. The datefield in the table is "AppDate" and is a datetime datatype and is not null. I know Sequel stores dates internally and is different then what is displayed. I have dates 01/01/1900 as default dates. I just discover that sequel uses that same date as a default date. To me its seems like Sequel is switching datetime formats and C# is not recognizing it as a valid datetime. Thanks for you quick response...|||Also should I changed my select statement and convert the date in the stored procedure
like "select AppDate = convert(datetime,AppDate) from ...|||What value does dtStartDate have??|||dtStartDate has 12/01/2003 12:00:00 AM

and the dtEndDate has 12/31/2003 12:00:00 AM|||I am not sure why you get error??
Cannot convert datetime to string

It must be probably your sorting procedure handling some parameter in wrong manner|||its funny because that what I originally though. I was able to sort by a few columns and it works. But some columns would throw that conversion error. I know I am sorting by the right field name but for some reason and I don't why but Sequel is switching datetime formats. That leads me to my original question that Sequel is returning the recordset as yyyy-mm-dd and and is there a way to change it to mm-dd-yyyy. I notice that other developers are experience the same problem and the answer seems to be that SQl Server and asp.net application have to have the same regional setting(datetime). But I haven't found an answer to change the setting in SQL Server or asp.net application. I know how to change the settings on the server but that doesn't help.|||Post your Sorting code...

Friday, March 9, 2012

MySQL function equivalent

Hi,
In MySQL, from_unixtime(lonvalue) returns the date format in 'YYYY-MM-DD HH:MM:SS'. Is there any equivalent available in SQL Server, to view the date in man readable format, when the input is an long value in milliseconds.
Eg :
SELECT FROM_UNIXTIME(875996580);
Thanks,
Smitha
Take a look at the CONVERT function in Books Online. It has different styles that you can use. Optionally you can format the string using various datepart calls.

select CONVERT(varchar, CURRENT_TIMESTAMP, 121) -- odbc canonical format|||select CONVERT(varchar, '875996580', 121);
Output : 875996580

As I had said earlier, the input is a long value (in milliseconds)|||There is no native function that will understand the Unix format directly. You have to write your own TSQL UDF to do the conversion based on the format.|||

isnt 'from_unixtime(longvalue)' return a date that is longvalue seconds from 1/1/1970 ? not milleseconds?

select dateadd(s, 875996580, '1/1/1970')
yields:
1997-10-04 20:23:00.000

Monday, February 20, 2012

My primitive questions

I want to cerate printable reports and they must be in certain format , I don't have any experience in neither "Crystal reports" and "Reporting services" I wanted to make them by "Crystal reports" but it is very confusing ! so some one advice me to use "Reporting services" and I want to make sure that"Reporting services" can give me what I need befor I start to learn it seriously .

My first question is can I use "Reporting services" with a sql server data base on an ordinary web hosting?

Then I need extremely formatted prints of those reports rather in a web based application. Can "Reporting services" give me web based print like something we have in crystal reports ?

And last question is : where to start learning"Reporting services" fast ?

Thank you in advance .

Have a look at the following:

http://msdn2.microsoft.com/en-gb/sql/aa336316.aspx (SQL Server 2005 Reporting Services has many tutorials)|||

Thank you for links despite I saw them before but Dude give me some basic information before make me to read every thingJ

Please somebody answer my question with yes or no

Thank you .

|||

>My first question is can I use "Reporting services" with a sql server data base on an ordinary web hosting?

If you have SQL Server with the reporting services component plus the web viewer control.

>Then I need extremely formatted prints of those reports rather in a web based application. Can "Reporting services" give me web based print like something we have in crystal reports ?
SSRS can output a number of formats including PDF.