Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NullReferenceException when trying to fill OracleDataAdaptor

Status
Not open for further replies.

pgosse

Programmer
Sep 26, 2001
42
CA
Greetings. I'm fairly new to C# and .NET so I'm hoping someone can shed some light on what I'm doing wrong here. I've googled the error message and understand what is causing the problem, I just can't see it in my code. I honestly do not see which object I'm not instantiating that is causing this error to be raised (well, it's obviously the dataset, but I've instantiated that so I don't get why it's triggering this error).

I've got a select list on a form that I'm trying to populate. The results are returned as a refCursor from an Oracle stored procedure.

However, when I try to fill the dataset I get the following error:

System.NullReferenceException: Object reference not set to an instance of an object. at research.searchGrants.spFillLocation()

The code is below:

try
{
dbConn = new OracleConnection(strLogin);
dbConn.Open();

strSQL = strSchema + "PKGSEARCH_PUBLIC.GRANT_LOCN_LIST";

dbCmd = new OracleCommand(strSQL);
dbCmd.Connection = dbConn;
dbCmd.CommandType = CommandType.StoredProcedure;

dbCmd.Parameters.Add(new OracleParameter("oLOCN_LIST", OracleType.Cursor)).Direction = ParameterDirection.Output;

dbAdp = new OracleDataAdapter();
dbAdp.SelectCommand = dbCmd;

DataSet ds = new DataSet();
dbAdp.Fill(ds);

lstGrantingAgency.DataTextField = "ftvlocn_title";
lstGrantingAgency.DataValueField = "ftvlocn_title";
lstGrantingAgency.DataSource = ds.Tables[0].DefaultView;
lstGrantingAgency.DataBind();

dbConn.Close();
dbConn.Dispose();
dbCmd.Dispose();
}
catch (Exception expGen)
{
lblError.Text = expGen.ToString();
}

Can anyone point me in the right direction to identify the problem here?

As I said I'm new to C# and .NET so any assistance is very much appreciated.

Cheers and thanks in advance,

Pablo
 
my guess would be that strLogin or strSQL is null. Once you try to use them it throws the exception.

This is purely guess though as there's not enough code there to know exactly what's going on. Can you post more code? The entire class if possible.

As an extra note: .Dispose() is something I never call on my components. I let .net take care of the garbage collection. That could be a problem too!

Hope this gives you a step in the right direction.
 
Hi there. The entire class is below. strSQL and strLogin are both set. I tried removing the Dispose() calls but the error still occurs.

Thanks very much for looking over the code. I really appreciate it. I've been beating my head against this error for almost a day and half now and am soon going to ... well ... Thanks again!

Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data.OracleClient;
 
namespace research
{
      ///<summary>
      ///Summary description for searchGrants.
      ///</summary>
      public class searchGrants : System.Web.UI.Page
      {
            protected MetaBuilders.WebControls.MasterPages.Content pageTitle;
            protected MetaBuilders.WebControls.MasterPages.Content pageHeader;
            protected PeterBlum.PetersDatePackage.DateTextBox datStartDate;
            protected PeterBlum.PetersDatePackage.DateTextBox datEndDate;
            protected MetaBuilders.WebControls.MasterPages.Content pageContent;
            protected MetaBuilders.WebControls.MasterPages.ContentContainer MPContainer;
            protected System.Web.UI.WebControls.TextBox lblGrantTitle;
            protected System.Web.UI.WebControls.TextBox lblResearcher;
            protected System.Web.UI.WebControls.TextBox lblKeywords;
            protected System.Web.UI.WebControls.DropDownList lstLocation;
            protected System.Web.UI.WebControls.DropDownList lstGrantingAgency;
            protected System.Data.OracleClient.OracleConnection dbConn;
            protected System.Data.OracleClient.OracleCommand dbCmd;
            protected string strLogin = ConfigurationSettings.AppSettings["strDbLogin"];
            protected string strSchema = ConfigurationSettings.AppSettings["strDbSchema"];
            protected System.Data.OracleClient.OracleCommand dbAdpSelect;
            protected System.Data.OracleClient.OracleDataAdapter dbAdp;
            protected DataSet ds;
            protected System.Web.UI.WebControls.Label lblError;
 
            protected System.Web.UI.HtmlControls.HtmlForm frmSearchGrants;
      
            private void Page_Load(object sender, System.EventArgs e)
            {
                  // Put user code to initialize the page here
 
                  spFillLocation();
            }
 
            private void spFillLocation()
            {
                  string strSQL;
 
                  try
                  {
                        dbConn = new OracleConnection(strLogin);
                        dbConn.Open();
 
                        strSQL = strSchema + "PKGSEARCH_PUBLIC.GRANT_LOCN_LIST";
 
                        dbCmd = new OracleCommand(strSQL);
                        dbCmd.Connection = dbConn;
                        dbCmd.CommandType = CommandType.StoredProcedure;
 
                        dbCmd.Parameters.Add(new OracleParameter("oLOCN_LIST", OracleType.Cursor)).Direction = ParameterDirection.Output;
 
                        //dbAdp = new OracleDataAdapter();
                        dbAdp.SelectCommand = dbCmd;
 
                        DataSet ds = new DataSet();
                        dbAdp.Fill(ds);
 
                        lstGrantingAgency.DataTextField = "ftvlocn_title";
                        lstGrantingAgency.DataValueField = "ftvlocn_title";
                        lstGrantingAgency.DataSource = ds.Tables[0].DefaultView;
                        lstGrantingAgency.DataBind();
 
                        dbConn.Close();
                        dbConn.Dispose();
                        dbCmd.Dispose();
                  }
                  catch (Exception expGen)
                  {
                        lblError.Text = expGen.ToString();
                  }
            }
 
            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                  this.dbConn = new System.Data.OracleClient.OracleConnection();
                  this.dbCmd = new System.Data.OracleClient.OracleCommand();
                  this.dbAdpSelect = new System.Data.OracleClient.OracleCommand();
                  this.dbAdp = new System.Data.OracleClient.OracleDataAdapter();
                  // 
                  // dbAdp
                  // 
                  this.dbAdp.SelectCommand = this.dbAdpSelect;
                  this.Load += new System.EventHandler(this.Page_Load);
 
            }
            #endregion
      }
}
 
Additionally, I've pinpointed the exact lines of code causing the problem.

Code:
lstGrantingAgency.DataTextField = "ftvlocn_title";
lstGrantingAgency.DataValueField = "ftvlocn_title";
lstGrantingAgency.DataSource = ds.Tables[0].DefaultView;
lstGrantingAgency.DataBind();

If I comment out those lines the error does not occur (but obviously the problem is not solved, since it is these lines which should fill the list). Not sure if that provides any additional insight.

Cheers and thanks again.

Pablo
 
lstGrantingAgency.DataSource = ds.Tables[0].DefaultView;

put a break point on this line. Determine what tables are in ds. Is ds.Tables[0] null? how about .DefaultView under tables[0]?
 
if you really want to make sure your disposible objects are disposed, use [tt]using[/tt] blocks.

Code:
using ( OracleConnection cnx = new OracleConnection(...) )
{
 using ( OracleCommand cmd = new OracleCommand(...) )
 {
  ...
 }
}

the way it stands, if an error is thrown, [tt].Close[/tt] and [tt].Dispose[/tt] will not be called anyway.

mr s. <;)

 
The problem exists between the keyboard and the chair.

After a decent night's sleep I looked at this code again.

It turns out that when I was building the form I didn't pay close enough attention to which tab of the tool box was active. It was HTML, not Web Forms, so I had inadvertantly dragged two standard select lists on to the form, not the DropDownList control.

Yeah, that might make a bit of a difference ...

Misterstick, thanks for the tip on using using. That makes good sense.

Thanks everyone for your help.

Cheers,
Pablo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top