DataList DataBind - NullReferenceException

G

Guest

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.OnDataBinding(EventArgs e) +9
System.Web.UI.WebControls.BaseDataList.DataBind() +23
ASPNET.StarterKit.Reports.DrillDown.NeedList_ItemCommand(Object Sender,
DataListCommandEventArgs e) in c:\inetpub\wwwroot\tamtest\drilldown.aspx.cs:89
System.Web.UI.WebControls.DataList.OnItemCommand(DataListCommandEventArgs
e) +110
System.Web.UI.WebControls.DataList.OnBubbleEvent(Object source, EventArgs
e) +60
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.DataListItem.OnBubbleEvent(Object source,
EventArgs e) +100
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +121

System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +115
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +138
System.Web.UI.Page.ProcessRequestMain() +1292
 
G

Guest

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.OnDataBinding(EventArgs e) +9
System.Web.UI.WebControls.BaseDataList.DataBind() +23
ASPNET.StarterKit.Reports.DrillDown.NeedList_ItemCommand(Object Sender,
DataListCommandEventArgs e) in c:\inetpub\wwwroot\tamtest\drilldown.aspx.cs:89
System.Web.UI.WebControls.DataList.OnItemCommand(DataListCommandEventArgs
e) +110
System.Web.UI.WebControls.DataList.OnBubbleEvent(Object source, EventArgs
e) +60
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.DataListItem.OnBubbleEvent(Object source,
EventArgs e) +100
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +121

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

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top