Showing posts with label tags. Show all posts
Showing posts with label tags. Show all posts

Thursday, March 22, 2012

Object reference not set to an instance of an object On Code Page Register Tag?

why am i getting the "Object reference not set to an instance of an
object" error on all page registeration tags in my project.
<%@dotnet.itags.org. Page Language="vb" AutoEventWireup="false"
Codebehind="bid_history.aspx.vb"
Inherits="dnfExchange.marketing_bid_history" %looks like you have renamed your file at some point,

change inherits="dnfExchange.marketing_bid_history"

to

inherits="dnfExchange.bid_history"
Okay, let's put on our diagnostic hats here. "Object Reference not set to an
instance of an object" is the message, so let's parse it: "Object Reference"
is a reference to an object. So, how many objects are referenced in your
page registration tag? I will give you a hint: One. "Not set to an instance
of an object" means that the object doesn't exist, or can't be found where
you said it was. So, obviously, the object doesn't exist, or is not where
you say it is. What are the possibilities? You spelled the class name wrong,
the DLL is not in the bin folder, or you don't have a "Src" reference in
your code and you're not using a compiled DLL.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.

<jw56578@.gmail.com> wrote in message
news:1116257283.912003.295390@.g47g2000cwa.googlegr oups.com...
> why am i getting the "Object reference not set to an instance of an
> object" error on all page registeration tags in my project.
> <%@. Page Language="vb" AutoEventWireup="false"
> Codebehind="bid_history.aspx.vb"
> Inherits="dnfExchange.marketing_bid_history" %

Object reference not set to an instance of an object On Code Page Register Tag?

why am i getting the "Object reference not set to an instance of an
object" error on all page registeration tags in my project.
<%@dotnet.itags.org. Page Language="vb" AutoEventWireup="false"
Codebehind="bid_history.aspx.vb"
Inherits="dnfExchange.marketing_bid_history" %>looks like you have renamed your file at some point,
change inherits="dnfExchange.marketing_bid_history"
to
inherits="dnfExchange.bid_history"
Okay, let's put on our diagnostic hats here. "Object Reference not set to an
instance of an object" is the message, so let's parse it: "Object Reference"
is a reference to an object. So, how many objects are referenced in your
page registration tag? I will give you a hint: One. "Not set to an instance
of an object" means that the object doesn't exist, or can't be found where
you said it was. So, obviously, the object doesn't exist, or is not where
you say it is. What are the possibilities? You spelled the class name wrong,
the DLL is not in the bin folder, or you don't have a "Src" reference in
your code and you're not using a compiled DLL.
HTH,
Kevin Spencer
Microsoft MVP
.Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.
<jw56578@.gmail.com> wrote in message
news:1116257283.912003.295390@.g47g2000cwa.googlegroups.com...
> why am i getting the "Object reference not set to an instance of an
> object" error on all page registeration tags in my project.
> <%@. Page Language="vb" AutoEventWireup="false"
> Codebehind="bid_history.aspx.vb"
> Inherits="dnfExchange.marketing_bid_history" %>
>

Object reference not set to an instance of an object, no clue why

Edited by SomeNewKid. Please post code between<code> and</code> tags.



I am trying to create a portal of sorts and I'm very new to asp.net but not to programming in general. This is supposed to simply update the dataSet (which is a global DataSet) with the information in a form and it gives me an error when I try to access a row in the table (index is also a global variable (int)) I've used the same exact code in another function and it works fine, any ideas?

Thanks,
Kasper


public void btnSubmit_Click(object sender, EventArgs e)
{
string edTitle = txtTitle.Value;
string edArticle = txtArticle.Value;
string edDate = DateTime.Now.Month + "." + DateTime.Now.Day + "." + DateTime.Now.Year;

DataTable dataTable = dataSet.Tables["articles"];
DataRow edRow = dataTable.Rows[index]; <--Error here
edRow["article"] = edArticle;
edRow["title"] = edTitle;
edRow["date"] = edDate;

dgUpdatedArticles.DataSource = dataTable;
dgUpdatedArticles.DataBind( );
pnlUpdatedArticles.Visible = true;

}

Hi,

Try printing the value of Index within the function.

I hope it is getting null value.

thanks.
Where are you declaring 'index'? is it a global variable?
Yes, it is global, I'm declaring it at the top of the class outside of any functions.

On top of that, I've tried putting 0 in for the Rows value but it still does nothing.
Here's the rest of the relevant code if you care.


protected System.Web.UI.WebControls.DataGrid dgArticles;
protected System.Web.UI.WebControls.DataGrid dgUpdatedArticles;
protected System.Web.UI.WebControls.Panel pnlUpdatedArticles;
protected System.Web.UI.WebControls.Panel pnlUpdateForm;
protected System.Web.UI.WebControls.Panel pnlPreview;
protected System.Web.UI.WebControls.Button btnSubmit;
protected System.Web.UI.WebControls.Button btnPreview;
protected System.Web.UI.WebControls.Label lblTitle;
protected System.Web.UI.WebControls.Label lblArticle;
protected System.Web.UI.WebControls.Label lblDate;
protected System.Web.UI.HtmlControls.HtmlInputText txtTitle;
protected System.Web.UI.HtmlControls.HtmlTextArea txtArticle;

DataSet dataSet = new DataSet( );
DataSet origDataSet = new DataSet( );
int index = 0;

public void Page_Load(Object sender, System.EventArgs e)
{
if(!IsPostBack)
{
string strConnection = "driver={MySQL ODBC 3.51 Driver}; server=localhost; uid=kasper; pwd=blacksun; database=kasper_uf_db";

string strCommand = "SELECT * FROM articles ORDER BY id DESC";

OdbcDataAdapter dataAdapter_Load = new OdbcDataAdapter(strCommand, strConnection);

DataSet dataSet_Load = new DataSet( );

dataAdapter_Load.Fill(dataSet_Load, "articles");
OdbcCommandBuilder bldr = new OdbcCommandBuilder(dataAdapter_Load);

DataTable dataTable_Load = dataSet_Load.Tables[0];
dgArticles.DataSource = dataTable_Load;
dgArticles.DataBind( );
Response.Write("!IsPostBack\n<br />\n<br />");
} else {
Response.Write("IsPostBack\n<br />\n<br />");
}

/*string strConn = "driver={MySQL ODBC 3.51 Driver}; server=localhost; uid=kasper; pwd=blacksun; database=kasper_uf_db";
*OdbcConnection objConn = new OdbcConnection(strConn);
*objConn.Open();
*
*string strComm = "SELECT article FROM articles ORDER BY id DESC";
*OdbcCommand objComm = new OdbcCommand(strComm, objConn );
*
*OdbcDataReader objDataReader = objComm.ExecuteReader( );
*
*while(objDataReader.Read())
*{
*string article = (string) objDataReader["article"];
*Response.Write(article);
*Response.Write("\n<br />\n<br />\n<br />\n<br />\n");
*}*/
}

public void SelectedIndexChanged_Handler(object sender, EventArgs e)
{
pnlPreview.Visible = false;
index = (int) dgArticles.SelectedIndex;
if(index != -1)
{
int articleID = (int) dgArticles.DataKeys[index];

string strConn = "driver={MySQL ODBC 3.51 Driver}; server=localhost; uid=kasper; pwd=blacksun; database=kasper_uf_db";
OdbcConnection connection = new OdbcConnection(strConn);
connection.Open( );

OdbcCommand command = new OdbcCommand( );
command.Connection = connection;
command.CommandText = "SELECT * FROM articles WHERE id=" + articleID;

OdbcDataAdapter dataAdapter = new OdbcDataAdapter( );
dataAdapter.SelectCommand = command;
dataAdapter.TableMappings.Add("Table", "articles");
dataAdapter.Fill(origDataSet);
dataSet = origDataSet;

DataTable dataTable = origDataSet.Tables["articles"];
DataRow articleRow = dataTable.Rows[0];
string title = (string) articleRow["title"];
string article = (string) articleRow["article"];

txtTitle.Value = title;
txtArticle.Value = article;
pnlUpdateForm.Visible = true;
}
}

public void btnPreview_Click(object sender, EventArgs e)
{
string prvwTitle = txtTitle.Value;
string prvwArticle = txtArticle.Value;
string today = DateTime.Now.Month + "." + DateTime.Now.Day + "." + DateTime.Now.Year;

lblTitle.Text = prvwTitle;
lblDate.Text = today;
lblArticle.Text = prvwArticle;

pnlPreview.Visible = true;

}
public void btnSubmit_Click(object sender, EventArgs e)
{
string updateTitle = txtTitle.Value;
string updateArticle = txtArticle.Value;
string updateDate = DateTime.Now.Month + "." + DateTime.Now.Day + "." + DateTime.Now.Year;

DataTable newDataTable = dataSet.Tables["articles"];
DataRow updateRow = newDataTable.NewRow( );
updateRow["article"] = updateArticle;
updateRow["title"] = updateTitle;
updateRow["date"] = updateDate;

dgUpdatedArticles.DataSource = newDataTable;
dgUpdatedArticles.DataBind( );
pnlUpdatedArticles.Visible = true;

}

I realize that there are some issues with how I'm handling data etc. but like I said, I'm new to ASP.NET and this is just the beginning of my article portal, once I get over this hurdle I will fix those things.

Thanks,
Kasper
Hi,
try, dataset.Tables.Count to make sure your dataset is being filled properly
Thanks jimiz, that worked. It returned 0. My best guess is that even though it's global, its value is erased at the end of the method in which it is set. I have it setup so that origDataSet and dataSet are initialized as 'new DataSet( );' so they are global, then, when they select an article, the DataSets are filled with the info for the article and a table from one of the DataSets is bound to a datagrid. Then later, in this btnSubmit_Click() function, the other DataSet is updated with their changes and is bound to another DataGrid that has the updated articles in it.

So basically, on DataSet holds the original data, and one holds the updated data, and they are both assigned to different DataGrids.

Any ideas why both DataSets come up as empty in this function? I know that the function that fills them is being called before this one, so why aren't they filled?

Thanks,
Kasper
Nothing? Nothing at all?
Just guessing


DataRow edRow = dataTable.Rows[index]; <--Error here

To


DataRow edRow = dataTable["articles"].Rows[index];