468,136 Members | 1,438 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,136 developers. It's quick & easy.

DataList DataBind - NullReferenceException

This is killing me, I've been trying to figure this out for 2 days.

When I click on the linkbutton, it executes the GetData(int) method to set
the DataSource. The FAILURE is when it tries to call databind
(sender.DataBind();
in NeedList_ItemCommand method). The error message is at the bottom of this
message (System.NullReferenceException)

Can anyone else see anything wrong with this? (I've included all relevant
code, stored procedure and table)
This datalist is within a selecteditemtemplate...FYI....
---------------------------ASP.NET Code----------------------------

<asp:datalist id="TechList" runat="server" DataSource='<%#
GetData((int)DataBinder.Eval(Container.DataItem, "NeedID")) %>'
RepeatDirection="Vertical" repeatcolumns="1" Width="100%" CellPadding="0"
cellspacing="0" OnItemCommand="NeedList_ItemCommand">
<headertemplate>
</headertemplate>
<itemtemplate>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td width="100">
<asp:linkbutton id="Linkbutton3" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "NeedID") %>'/>
</td>
</tr>
</table>
</itemtemplate>
</asp:datalist>

---------------------------Source Code----------------------------

protected void NeedList_ItemCommand(object Sender, DataListCommandEventArgs
e)
{
// change the selected index of Datalist
string cmd = ((LinkButton)e.CommandSource).CommandName;
DataList sender = (DataList)Sender;
if (cmd == "select")
sender.SelectedIndex = e.Item.ItemIndex;

// re-bind to display info with new selected index.
//sender.DataSource = GetEnablingTechs((int)ViewState[_needID]);
sender.DataSource = GetData((int)ViewState[_needID]);

//******This is where it is failing**********
sender.DataBind();
}


protected DrillDownReportCollection GetData(int needID)
{
// only use the needID in session if it doesn't exist.
if (needID == 0)
needID = (int)ViewState[_needID];

return DrillDownReport.GetData(needID);
}

public static DrillDownReportCollection GetData(int needID)
{
DataSet dsData = SqlHelper.ExecuteDataset(
ConfigurationSettings.AppSettings[Global.CfgKeyConnString],
"WN_GetAllNeeds");
DrillDownReportCollection items = new DrillDownReportCollection();

foreach(DataRow row in dsData.Tables[0].Rows)
{
DrillDownReport item = new DrillDownReport();
item.EnableTechID = Convert.ToInt32(row["NeedID"].ToString());
item.EnableTechName = row["NeedName"].ToString();
items.Add(item);
}

return items;
}

---------------------------Stored Procedure----------------------------

CREATE PROCEDURE WN_GetAllNeeds

AS

SELECT * FROM WN_Needs
GO

---------------------------Table----------------------------

CREATE TABLE [WN_Needs] (
[NeedID] [int] NOT NULL ,
[NeedName] [char] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
)
) ON [PRIMARY]
GO

---------------------------Error Message----------------------------

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:
Line 56: </td>
Line 57: <td>
Line 58: <asp:datalist id="TechList" runat="server"
DataSource='<%# GetData((int)DataBinder.Eval(Container.DataItem, "NeedID"))
%>' RepeatDirection="Vertical" repeatcolumns="1" Width="100%" CellPadding="0"
cellspacing="0" OnItemCommand="NeedList_ItemCommand">
Line 59: <headertemplate>
Line 60:
Source File: c:\inetpub\wwwroot\TamTest\DrillDown.aspx Line: 58

Stack Trace:
[NullReferenceException: Object reference not set to an instance of an
object.]
ASP.DrillDown_aspx.__DataBind__control8(Object sender, EventArgs e) in
c:\inetpub\wwwroot\TamTest\DrillDown.aspx:58
System.Web.UI.Control.OnDataBinding(EventArgs e) +66
System.Web.UI.WebControls.BaseDataList.OnDataBindi ng(EventArgs e) +9
System.Web.UI.WebControls.BaseDataList.DataBind() +23
ASPNET.StarterKit.Reports.DrillDown.NeedList_ItemC ommand(Object Sender,
DataListCommandEventArgs e) in c:\inetpub\wwwroot\tamtest\drilldown.aspx.cs:89
System.Web.UI.WebControls.DataList.OnItemCommand(D ataListCommandEventArgs
e) +110
System.Web.UI.WebControls.DataList.OnBubbleEvent(O bject source, EventArgs
e) +60
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.DataListItem.OnBubbleEve nt(Object source,
EventArgs e) +100
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.LinkButton.OnCommand(Com mandEventArgs e) +121

System.Web.UI.WebControls.LinkButton.System.Web.UI .IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +115
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +138
System.Web.UI.Page.ProcessRequestMain() +1292

Dec 7 '05 #1
1 2579
Previous Message had a mistake, please use this one to answer my question:
This is killing me, I've been trying to figure this out for 2 days.

When I click on the linkbutton, it executes the GetData(int) method to set
the DataSource. The FAILURE is when it tries to call databind
(sender.DataBind();
in NeedList_ItemCommand method). The error message is at the bottom of this
message (System.NullReferenceException)

Can anyone else see anything wrong with this? (I've included all relevant
code, stored procedure and table)
This datalist is within a selecteditemtemplate...FYI....
---------------------------ASP.NET Code----------------------------

<asp:datalist id="TechList" runat="server" DataSource='<%#
GetData((int)DataBinder.Eval(Container.DataItem, "NeedID")) %>'
RepeatDirection="Vertical" repeatcolumns="1" Width="100%" CellPadding="0"
cellspacing="0" OnItemCommand="NeedList_ItemCommand">
<headertemplate>
</headertemplate>
<itemtemplate>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td width="100">
<asp:linkbutton id="Linkbutton3" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "NeedID") %>'/>
</td>
</tr>
</table>
</itemtemplate>
</asp:datalist>

---------------------------Source Code----------------------------

protected void NeedList_ItemCommand(object Sender, DataListCommandEventArgs
e)
{
// change the selected index of Datalist
string cmd = ((LinkButton)e.CommandSource).CommandName;
DataList sender = (DataList)Sender;
if (cmd == "select")
sender.SelectedIndex = e.Item.ItemIndex;

// re-bind to display info with new selected index.
//sender.DataSource = GetData((int)ViewState[_needID]);
sender.DataSource = GetData((int)ViewState[_needID]);

//******This is where it is failing**********
sender.DataBind();
}


protected DrillDownReportCollection GetData(int needID)
{
// only use the needID in session if it doesn't exist.
if (needID == 0)
needID = (int)ViewState[_needID];

return DrillDownReport.GetData(needID);
}

public static DrillDownReportCollection GetData(int needID)
{
DataSet dsData = SqlHelper.ExecuteDataset(
ConfigurationSettings.AppSettings[Global.CfgKeyConnString],
"WN_GetAllNeeds");
DrillDownReportCollection items = new DrillDownReportCollection();

foreach(DataRow row in dsData.Tables[0].Rows)
{
DrillDownReport item = new DrillDownReport();
item.NeedID = Convert.ToInt32(row["NeedID"].ToString());
item.NeedName = row["NeedName"].ToString();
items.Add(item);
}

return items;
}

---------------------------Stored Procedure----------------------------

CREATE PROCEDURE WN_GetAllNeeds

AS

SELECT * FROM WN_Needs
GO

---------------------------Table----------------------------

CREATE TABLE [WN_Needs] (
[NeedID] [int] NOT NULL ,
[NeedName] [char] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
)
) ON [PRIMARY]
GO

---------------------------Error Message----------------------------

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:
Line 56: </td>
Line 57: <td>
Line 58: <asp:datalist id="TechList" runat="server"
DataSource='<%# GetData((int)DataBinder.Eval(Container.DataItem, "NeedID"))
%>' RepeatDirection="Vertical" repeatcolumns="1" Width="100%" CellPadding="0"
cellspacing="0" OnItemCommand="NeedList_ItemCommand">
Line 59: <headertemplate>
Line 60:
Source File: c:\inetpub\wwwroot\TamTest\DrillDown.aspx Line: 58

Stack Trace:
[NullReferenceException: Object reference not set to an instance of an
object.]
ASP.DrillDown_aspx.__DataBind__control8(Object sender, EventArgs e) in
c:\inetpub\wwwroot\TamTest\DrillDown.aspx:58
System.Web.UI.Control.OnDataBinding(EventArgs e) +66
System.Web.UI.WebControls.BaseDataList.OnDataBindi ng(EventArgs e) +9
System.Web.UI.WebControls.BaseDataList.DataBind() +23
ASPNET.StarterKit.Reports.DrillDown.NeedList_ItemC ommand(Object Sender,
DataListCommandEventArgs e) in c:\inetpub\wwwroot\tamtest\drilldown.aspx.cs:89
System.Web.UI.WebControls.DataList.OnItemCommand(D ataListCommandEventArgs
e) +110
System.Web.UI.WebControls.DataList.OnBubbleEvent(O bject source, EventArgs
e) +60
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.DataListItem.OnBubbleEve nt(Object source,
EventArgs e) +100
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.LinkButton.OnCommand(Com mandEventArgs e) +121

System.Web.UI.WebControls.LinkButton.System.Web.UI .IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +115
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +138
System.Web.UI.Page.ProcessRequestMain() +1292
Dec 7 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by pete K | last post: by
2 posts views Thread by cindy | last post: by
3 posts views Thread by asadikhan | last post: by
3 posts views Thread by Mirek Endys | last post: by
16 posts views Thread by Vikas Kumar | last post: by
5 posts views Thread by Vikas Kumar | last post: by
3 posts views Thread by Crazy Cat | last post: by
27 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.