ObjectDataSource and null datetime parameter

Discussion in 'ASP .Net' started by martinw, Jan 1, 2010.

  1. martinw

    martinw Guest

    Hi,

    Please could you help me with this problem,

    I am Building a website using ASP.net 2.0 and the .net 3.5.

    I have a GridView bound to an ObjectDataSource whose data source is a
    table adapter that was designed in the table adapter wizard (it has
    generated the Getdata() and Fill() methods).

    In order for me to pull back data that contains null values for
    DateTime and Integers I have had to add the methods below.

    The SetDate routine works but the SetNumber routine fails with the
    error shown underneath the code at the bottom of this page.

    I know null's for DateTimes and Integers is a bit silly but in the
    application I am writing it has to be so.

    What I want to happen is when the application is run and on first
    entry to the page the parameters startDate, endDate, tonality and
    companyName will all be null. After that the user will be able to
    filter down the results by adding date ranges for startDate & endDate,
    an integer value for tonality and a string for companyName. The users
    puts these values in via TextBox1, TextBox2, TextBox3, TextBox4
    accordingly.

    Appreciate any help.

    Happy new year

    Martin.

    ****************


    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Data.SqlClient;
    using System.Data.SqlTypes;

    //using Dundas.Charting.WebControl;

    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void ObjectDataSource1_Selecting(object sender,
    ObjectDataSourceSelectingEventArgs e)
    {
    e.InputParameters["startDate"] = SetDate("TextBox1");
    e.InputParameters["endDate"] = SetDate("TextBox2");
    e.InputParameters["tonality"] = SetNumber("TextBox3.Text");
    e.InputParameters["companyName"] = TextBox4.Text;
    }

    private object SetDate(string controlname)
    {
    TextBox tb = (TextBox)form1.FindControl(controlname);
    return (tb.Text == "") ? (object)null : (object)DateTime.Parse
    (tb.Text); /// Error refers to this line
    }

    private object SetNumber(string controlname)
    {
    TextBox tbi = (TextBox)form1.FindControl(controlname);
    return (tbi.Text == "") ? (object)null : (object)Int32.Parse
    (tbi.Text);
    }

    }

    ***************** This is the relevant part of the Default.aspx page
    ****************************

    <asp:GridView ID="GridView1" runat="server"
    AllowPaging="True"
    AllowSorting="True" AutoGenerateColumns="False"
    DataKeyNames="id"
    DataSourceID="ObjectDataSource1">
    <Columns>
    <asp:BoundField DataField="id" HeaderText="id"
    InsertVisible="False"
    ReadOnly="True" SortExpression="id" />
    <asp:BoundField DataField="publicationdate"
    HeaderText="publicationdate"
    SortExpression="publicationdate" />
    <asp:BoundField DataField="title" HeaderText="title"
    SortExpression="title" />
    <asp:BoundField DataField="url" HeaderText="url"
    SortExpression="url" />
    </Columns>
    </asp:GridView>

    <br />

    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
    OldValuesParameterFormatString="original_{0}"
    onselecting="ObjectDataSource1_Selecting"
    SelectMethod="GetData"

    TypeName="DataSet1TableAdapters.glide_mainGridviewQuery_Volume_TonalityTableAdapter">
    <SelectParameters>
    <asp:ControlParameter ControlID="TextBox1"
    Name="startDate" PropertyName="Text"
    Type="DateTime" />
    <asp:ControlParameter ControlID="TextBox2"
    Name="endDate" PropertyName="Text"
    Type="DateTime" />
    <asp:ControlParameter ControlID="TextBox3"
    Name="tonality" PropertyName="Text"
    Type="Int32" />
    <asp:ControlParameter ControlID="TextBox4"
    Name="companyName"
    PropertyName="Text" Type="String" />
    </SelectParameters>
    </asp:ObjectDataSource>

    ****************************************************************************************


    ////////////////// the error //////////////////////////////

    System.NullReferenceException was unhandled by user code
    Message="Object reference not set to an instance of an object."
    Source="App_Web_7czawdcw"
    StackTrace:
    at _Default.SetNumber(String controlname) in c:\Users\martin
    \Documents\Visual Studio 2008\WebSites
    \DundasGlide1\Default.aspx.cs:line 192
    at _Default.ObjectDataSource1_Selecting(Object sender,
    ObjectDataSourceSelectingEventArgs e) in c:\Users\martin\Documents
    \Visual Studio 2008\WebSites\DundasGlide1\Default.aspx.cs:line 179
    at System.Web.UI.WebControls.ObjectDataSourceView.OnSelecting
    (ObjectDataSourceSelectingEventArgs e)
    at System.Web.UI.WebControls.ObjectDataSourceView.ExecuteSelect
    (DataSourceSelectArguments arguments)
    at System.Web.UI.DataSourceView.Select
    (DataSourceSelectArguments arguments, DataSourceViewSelectCallback
    callback)
    at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
    at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
    at System.Web.UI.WebControls.GridView.DataBind()
    at
    System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound()
    at
    System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls
    ()
    at System.Web.UI.Control.EnsureChildControls()
    at System.Web.UI.Control.PreRenderRecursiveInternal()
    at System.Web.UI.Control.PreRenderRecursiveInternal()
    at System.Web.UI.Control.PreRenderRecursiveInternal()
    at System.Web.UI.Page.ProcessRequestMain(Boolean
    includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
    InnerException:
     
    martinw, Jan 1, 2010
    #1
    1. Advertising

  2. martinw

    Abdul Sami Guest

    You are sending TextboBox3 .text means not the textbox name which is
    TextboBox3.

    Apparently it seems to be the reason
    --
    Abdul Sami


    "martinw" wrote:

    > Hi,
    >
    > Please could you help me with this problem,
    >
    > I am Building a website using ASP.net 2.0 and the .net 3.5.
    >
    > I have a GridView bound to an ObjectDataSource whose data source is a
    > table adapter that was designed in the table adapter wizard (it has
    > generated the Getdata() and Fill() methods).
    >
    > In order for me to pull back data that contains null values for
    > DateTime and Integers I have had to add the methods below.
    >
    > The SetDate routine works but the SetNumber routine fails with the
    > error shown underneath the code at the bottom of this page.
    >
    > I know null's for DateTimes and Integers is a bit silly but in the
    > application I am writing it has to be so.
    >
    > What I want to happen is when the application is run and on first
    > entry to the page the parameters startDate, endDate, tonality and
    > companyName will all be null. After that the user will be able to
    > filter down the results by adding date ranges for startDate & endDate,
    > an integer value for tonality and a string for companyName. The users
    > puts these values in via TextBox1, TextBox2, TextBox3, TextBox4
    > accordingly.
    >
    > Appreciate any help.
    >
    > Happy new year
    >
    > Martin.
    >
    > ****************
    >
    >
    > using System;
    > using System.Configuration;
    > using System.Data;
    > using System.Linq;
    > using System.Web;
    > using System.Web.Security;
    > using System.Web.UI;
    > using System.Web.UI.HtmlControls;
    > using System.Web.UI.WebControls;
    > using System.Web.UI.WebControls.WebParts;
    > using System.Xml.Linq;
    > using System.Data.SqlClient;
    > using System.Data.SqlTypes;
    >
    > //using Dundas.Charting.WebControl;
    >
    > public partial class _Default : System.Web.UI.Page
    > {
    > protected void Page_Load(object sender, EventArgs e)
    > {
    >
    > }
    >
    >
    > protected void ObjectDataSource1_Selecting(object sender,
    > ObjectDataSourceSelectingEventArgs e)
    > {
    > e.InputParameters["startDate"] = SetDate("TextBox1");
    > e.InputParameters["endDate"] = SetDate("TextBox2");
    > e.InputParameters["tonality"] = SetNumber("TextBox3.Text");
    > e.InputParameters["companyName"] = TextBox4.Text;
    > }
    >
    > private object SetDate(string controlname)
    > {
    > TextBox tb = (TextBox)form1.FindControl(controlname);
    > return (tb.Text == "") ? (object)null : (object)DateTime.Parse
    > (tb.Text); /// Error refers to this line
    > }
    >
    > private object SetNumber(string controlname)
    > {
    > TextBox tbi = (TextBox)form1.FindControl(controlname);
    > return (tbi.Text == "") ? (object)null : (object)Int32.Parse
    > (tbi.Text);
    > }
    >
    > }
    >
    > ***************** This is the relevant part of the Default.aspx page
    > ****************************
    >
    > <asp:GridView ID="GridView1" runat="server"
    > AllowPaging="True"
    > AllowSorting="True" AutoGenerateColumns="False"
    > DataKeyNames="id"
    > DataSourceID="ObjectDataSource1">
    > <Columns>
    > <asp:BoundField DataField="id" HeaderText="id"
    > InsertVisible="False"
    > ReadOnly="True" SortExpression="id" />
    > <asp:BoundField DataField="publicationdate"
    > HeaderText="publicationdate"
    > SortExpression="publicationdate" />
    > <asp:BoundField DataField="title" HeaderText="title"
    > SortExpression="title" />
    > <asp:BoundField DataField="url" HeaderText="url"
    > SortExpression="url" />
    > </Columns>
    > </asp:GridView>
    >
    > <br />
    >
    > <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
    > OldValuesParameterFormatString="original_{0}"
    > onselecting="ObjectDataSource1_Selecting"
    > SelectMethod="GetData"
    >
    > TypeName="DataSet1TableAdapters.glide_mainGridviewQuery_Volume_TonalityTableAdapter">
    > <SelectParameters>
    > <asp:ControlParameter ControlID="TextBox1"
    > Name="startDate" PropertyName="Text"
    > Type="DateTime" />
    > <asp:ControlParameter ControlID="TextBox2"
    > Name="endDate" PropertyName="Text"
    > Type="DateTime" />
    > <asp:ControlParameter ControlID="TextBox3"
    > Name="tonality" PropertyName="Text"
    > Type="Int32" />
    > <asp:ControlParameter ControlID="TextBox4"
    > Name="companyName"
    > PropertyName="Text" Type="String" />
    > </SelectParameters>
    > </asp:ObjectDataSource>
    >
    > ****************************************************************************************
    >
    >
    > ////////////////// the error //////////////////////////////
    >
    > System.NullReferenceException was unhandled by user code
    > Message="Object reference not set to an instance of an object."
    > Source="App_Web_7czawdcw"
    > StackTrace:
    > at _Default.SetNumber(String controlname) in c:\Users\martin
    > \Documents\Visual Studio 2008\WebSites
    > \DundasGlide1\Default.aspx.cs:line 192
    > at _Default.ObjectDataSource1_Selecting(Object sender,
    > ObjectDataSourceSelectingEventArgs e) in c:\Users\martin\Documents
    > \Visual Studio 2008\WebSites\DundasGlide1\Default.aspx.cs:line 179
    > at System.Web.UI.WebControls.ObjectDataSourceView.OnSelecting
    > (ObjectDataSourceSelectingEventArgs e)
    > at System.Web.UI.WebControls.ObjectDataSourceView.ExecuteSelect
    > (DataSourceSelectArguments arguments)
    > at System.Web.UI.DataSourceView.Select
    > (DataSourceSelectArguments arguments, DataSourceViewSelectCallback
    > callback)
    > at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
    > at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
    > at System.Web.UI.WebControls.GridView.DataBind()
    > at
    > System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound()
    > at
    > System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls
    > ()
    > at System.Web.UI.Control.EnsureChildControls()
    > at System.Web.UI.Control.PreRenderRecursiveInternal()
    > at System.Web.UI.Control.PreRenderRecursiveInternal()
    > at System.Web.UI.Control.PreRenderRecursiveInternal()
    > at System.Web.UI.Page.ProcessRequestMain(Boolean
    > includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
    > InnerException:
    >
    >
    >
    >
    > .
    >
     
    Abdul Sami, Jan 2, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    734
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    555
    Tim Peters
    Sep 9, 2003
  3. mp
    Replies:
    1
    Views:
    403
    John Machin
    Jul 28, 2006
  4. Martin
    Replies:
    0
    Views:
    350
    Martin
    Dec 27, 2008
  5. Replies:
    2
    Views:
    775
    M.-A. Lemburg
    Jan 6, 2009
Loading...

Share This Page