tags:

views:

461

answers:

3

Hi there.

I'm using v.s 2008 with c#. I have a .rdlc file and it using a objectdatasource. ods has two datetime parameters. That datetime parameter's format must be dd-MM-yyy. but that time threw an exception. Exception is : An error has occurred during report processing. String was not recognized as a valid DateTime.

My code is :

        int iBaslangicYil = txtIlkTarih.Text.Substring(6, 4).To<int>();
        int iBaslangicGun = txtIlkTarih.Text.Substring(0, 2).To<int>();
        int iBAslangicAy = txtIlkTarih.Text.Substring(3, 2).To<int>();
        DateTime dtBaslangic = new DateTime(iBaslangicYil, iBAslangicAy, iBaslangicGun);

        int iBitisYil = txtIkinciTarih.Text.Substring(6, 4).To<int>();
        int iBitisGun = txtIkinciTarih.Text.Substring(0, 2).To<int>();
        int iBitisAy = txtIkinciTarih.Text.Substring(3, 2).To<int>();
        DateTime dtBitis = new DateTime(iBitisYil, iBitisAy, iBitisGun);

        rvTarihAraliginaGoreSeansSayilari.Visible = true;
        odsTarihAraliginaGoreSeansSayisi.SelectParameters.Add("refTarih1",DbType.DateTime,dtBaslangic.ToString());
        odsTarihAraliginaGoreSeansSayisi.SelectParameters.Add("refTarih2", System.Data.DbType.DateTime, dtBitis.ToString());
        odsTarihAraliginaGoreSeansSayisi.DataBind();
        rvTarihAraliginaGoreSeansSayilari.LocalReport.Refresh();

thanks for your help.

+1  A: 

The second argument for adding SelectParameters to an ObjectDataSource should be a TypeCode enum

odsTarihAraliginaGoreSeansSayisi.SelectParameters.Add("refTarih1",TypeCode.DateTime ,dtBaslangic.ToString());
Russ Cam
There are overloads that take `TypeCode` or `DbType`: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.parametercollection.add.aspx
LukeH
@Luke - thanks. I'm using .NET 2.0 at the moment and that overload doesn't exist in it (.NET 3.5) :)
Russ Cam
A: 

Try setting the format of the string explicitly when you call ToString:

odsTarihAraliginaGoreSeansSayisi.SelectParameters
    .Add("refTarih1", DbType.DateTime, dtBaslangic.ToString("yyyy'-'MM'-'dd");
odsTarihAraliginaGoreSeansSayisi.SelectParameters
    .Add("refTarih2", DbType.DateTime, dtBitis.ToString("yyyy'-'MM'-'dd");
LukeH
A: 

I think the SelectParameters.Add issue has been addressed by other answers; but to make your code easier, you might also want to look at DateTime.ParseExact:

string s = "01-02-2003";
DateTime when = DateTime.ParseExact(s, "dd-MM-yyyy",
    CultureInfo.InvariantCulture); // sine no locale issues in the above
Marc Gravell