ObjectDataSource and null datetime parameter

M

martinw

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:
 
A

Abdul Sami

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

Apparently it seems to be the reason
--
Abdul Sami


martinw said:
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:




.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top