Object reference??

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to assign a value from DataItem to a variable but keeping getting "
Object reference not set to an instance of an object" error message.

what i am missing?

public void create_ddl(object s, RepeaterItemEventArgs e)
{

string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{

DropDownList dd = (DropDownList)e.Item.FindControl("ddl");

if(dd !=null)
{
dd.DataSource = bs.drPriority();
dd.DataTextField = "text";
dd.DataValueField = "value";
dd.DataBind();
dd.Items.FindByValue(dr["Priority"].ToString()).Selected
= true;

}

}
}
 
huzz said:
I am trying to assign a value from DataItem to a variable but keeping
getting " Object reference not set to an instance of an object" error
message.

what i am missing?

public void create_ddl(object s, RepeaterItemEventArgs e)
{

string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();

if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{

DropDownList dd =
(DropDownList)e.Item.FindControl("ddl");

if(dd !=null)
{
dd.DataSource = bs.drPriority();

What is "bs" ??
dd.DataTextField = "text";
dd.DataValueField = "value";
dd.DataBind();

dd.Items.FindByValue(dr["Priority"].ToString()).Selected = true;

}

}
}

It would help if you specify the line the error is in.

Hans Kesting
 
Hi Hans

Here is the line no:
Line 57: string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();


Hans Kesting said:
huzz said:
I am trying to assign a value from DataItem to a variable but keeping
getting " Object reference not set to an instance of an object" error
message.

what i am missing?

public void create_ddl(object s, RepeaterItemEventArgs e)
{

string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();

if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{

DropDownList dd =
(DropDownList)e.Item.FindControl("ddl");

if(dd !=null)
{
dd.DataSource = bs.drPriority();

What is "bs" ??
dd.DataTextField = "text";
dd.DataValueField = "value";
dd.DataBind();

dd.Items.FindByValue(dr["Priority"].ToString()).Selected = true;

}

}
}

It would help if you specify the line the error is in.

Hans Kesting
 
e.Item.DataItem is null. The problem is you are checking for ItemType after
getting the categoryName. However, if you are in a headerTemplate or a
footerTemplate, e.Item.DataItem will be null. Simply move that code inside
your if statement:

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem){
string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();
...
}

should work

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/


huzz said:
Hi Hans

Here is the line no:
Line 57: string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();


Hans Kesting said:
huzz said:
I am trying to assign a value from DataItem to a variable but keeping
getting " Object reference not set to an instance of an object" error
message.

what i am missing?

public void create_ddl(object s, RepeaterItemEventArgs e)
{

string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();

if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{

DropDownList dd =
(DropDownList)e.Item.FindControl("ddl");

if(dd !=null)
{
dd.DataSource = bs.drPriority();

What is "bs" ??
dd.DataTextField = "text";
dd.DataValueField = "value";
dd.DataBind();

dd.Items.FindByValue(dr["Priority"].ToString()).Selected = true;

}

}
}

It would help if you specify the line the error is in.

Hans Kesting
 
karl thanks for your quick response.. after i moved the line of code inside
the if statement and now getting this message

"Specified cast is not valid. "

Line 64: string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();


Karl Seguin said:
e.Item.DataItem is null. The problem is you are checking for ItemType after
getting the categoryName. However, if you are in a headerTemplate or a
footerTemplate, e.Item.DataItem will be null. Simply move that code inside
your if statement:

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem){
string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();
...
}

should work

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/


huzz said:
Hi Hans

Here is the line no:
Line 57: string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();


Hans Kesting said:
huzz wrote:
I am trying to assign a value from DataItem to a variable but keeping
getting " Object reference not set to an instance of an object" error
message.

what i am missing?

public void create_ddl(object s, RepeaterItemEventArgs e)
{

string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();

if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{

DropDownList dd =
(DropDownList)e.Item.FindControl("ddl");

if(dd !=null)
{
dd.DataSource = bs.drPriority();

What is "bs" ??

dd.DataTextField = "text";
dd.DataValueField = "value";
dd.DataBind();

dd.Items.FindByValue(dr["Priority"].ToString()).Selected = true;

}

}
}

It would help if you specify the line the error is in.

Hans Kesting
 
What are you binding your repeater to?

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


huzz said:
karl thanks for your quick response.. after i moved the line of code inside
the if statement and now getting this message

"Specified cast is not valid. "

Line 64: string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();


Karl Seguin said:
e.Item.DataItem is null. The problem is you are checking for ItemType after
getting the categoryName. However, if you are in a headerTemplate or a
footerTemplate, e.Item.DataItem will be null. Simply move that code inside
your if statement:

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem){
string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();
...
}

should work

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/


huzz said:
Hi Hans

Here is the line no:
Line 57: string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();


:

huzz wrote:
I am trying to assign a value from DataItem to a variable but keeping
getting " Object reference not set to an instance of an object" error
message.

what i am missing?

public void create_ddl(object s, RepeaterItemEventArgs e)
{

string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();

if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{

DropDownList dd =
(DropDownList)e.Item.FindControl("ddl");

if(dd !=null)
{
dd.DataSource = bs.drPriority();

What is "bs" ??

dd.DataTextField = "text";
dd.DataValueField = "value";
dd.DataBind();

dd.Items.FindByValue(dr["Priority"].ToString()).Selected = true;

}

}
}

It would help if you specify the line the error is in.

Hans Kesting
 
The repeater is binded to datareader

Karl Seguin said:
What are you binding your repeater to?

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


huzz said:
karl thanks for your quick response.. after i moved the line of code inside
the if statement and now getting this message

"Specified cast is not valid. "

Line 64: string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();


Karl Seguin said:
e.Item.DataItem is null. The problem is you are checking for ItemType after
getting the categoryName. However, if you are in a headerTemplate or a
footerTemplate, e.Item.DataItem will be null. Simply move that code inside
your if statement:

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem){
string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();
...
}

should work

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/


Hi Hans

Here is the line no:
Line 57: string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();


:

huzz wrote:
I am trying to assign a value from DataItem to a variable but keeping
getting " Object reference not set to an instance of an object" error
message.

what i am missing?

public void create_ddl(object s, RepeaterItemEventArgs e)
{

string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();

if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{

DropDownList dd =
(DropDownList)e.Item.FindControl("ddl");

if(dd !=null)
{
dd.DataSource = bs.drPriority();

What is "bs" ??

dd.DataTextField = "text";
dd.DataValueField = "value";
dd.DataBind();

dd.Items.FindByValue(dr["Priority"].ToString()).Selected = true;

}

}
}

It would help if you specify the line the error is in.

Hans Kesting
 
Try casting it to DbDataRecord (I think)

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


huzz said:
The repeater is binded to datareader

Karl Seguin said:
What are you binding your repeater to?

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


huzz said:
karl thanks for your quick response.. after i moved the line of code inside
the if statement and now getting this message

"Specified cast is not valid. "

Line 64: string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();


:

e.Item.DataItem is null. The problem is you are checking for
ItemType
after
getting the categoryName. However, if you are in a headerTemplate or a
footerTemplate, e.Item.DataItem will be null. Simply move that code inside
your if statement:

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem){
string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();
...
}

should work

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/


Hi Hans

Here is the line no:
Line 57: string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();


:

huzz wrote:
I am trying to assign a value from DataItem to a variable but keeping
getting " Object reference not set to an instance of an
object"
error
message.

what i am missing?

public void create_ddl(object s, RepeaterItemEventArgs e)
{

string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();

if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{

DropDownList dd =
(DropDownList)e.Item.FindControl("ddl");

if(dd !=null)
{
dd.DataSource = bs.drPriority();

What is "bs" ??

dd.DataTextField = "text";
dd.DataValueField = "value";
dd.DataBind();

dd.Items.FindByValue(dr["Priority"].ToString()).Selected = true;

}

}
}

It would help if you specify the line the error is in.

Hans Kesting
 
sorry karl i am very new to asp.net.. what is DbDataReader? can you show me
code?

Many thanks for your help

Karl Seguin said:
Try casting it to DbDataRecord (I think)

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


huzz said:
The repeater is binded to datareader

Karl Seguin said:
What are you binding your repeater to?

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


karl thanks for your quick response.. after i moved the line of code
inside
the if statement and now getting this message

"Specified cast is not valid. "

Line 64: string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();


:

e.Item.DataItem is null. The problem is you are checking for ItemType
after
getting the categoryName. However, if you are in a headerTemplate or a
footerTemplate, e.Item.DataItem will be null. Simply move that code
inside
your if statement:

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem){
string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();
...
}

should work

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/


Hi Hans

Here is the line no:
Line 57: string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();


:

huzz wrote:
I am trying to assign a value from DataItem to a variable but
keeping
getting " Object reference not set to an instance of an object"
error
message.

what i am missing?

public void create_ddl(object s, RepeaterItemEventArgs e)
{

string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();

if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{

DropDownList dd =
(DropDownList)e.Item.FindControl("ddl");

if(dd !=null)
{
dd.DataSource = bs.drPriority();

What is "bs" ??

dd.DataTextField = "text";
dd.DataValueField = "value";
dd.DataBind();

dd.Items.FindByValue(dr["Priority"].ToString()).Selected = true;

}

}
}

It would help if you specify the line the error is in.

Hans Kesting
 
string categoryName =
((DbDataRecord)e.Item.DataItem)["FaultDesc"].ToString();

it's in the System.Data.Common namespace, so either add

using System.Data.Common atop your file, or do

string categoryName =
((System.Data.Common.DbDataRecord)e.Item.DataItem)["FaultDesc"].ToString();


Karl


--
MY ASP.Net tutorials
http://www.openmymind.net/


huzz said:
sorry karl i am very new to asp.net.. what is DbDataReader? can you show me
code?

Many thanks for your help

Karl Seguin said:
Try casting it to DbDataRecord (I think)

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


huzz said:
The repeater is binded to datareader

:

What are you binding your repeater to?

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


karl thanks for your quick response.. after i moved the line of code
inside
the if statement and now getting this message

"Specified cast is not valid. "

Line 64: string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();


:

e.Item.DataItem is null. The problem is you are checking for ItemType
after
getting the categoryName. However, if you are in a
headerTemplate
or a
footerTemplate, e.Item.DataItem will be null. Simply move that code
inside
your if statement:

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem){
string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();
...
}

should work

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/


Hi Hans

Here is the line no:
Line 57: string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();


:

huzz wrote:
I am trying to assign a value from DataItem to a variable but
keeping
getting " Object reference not set to an instance of an object"
error
message.

what i am missing?

public void create_ddl(object s, RepeaterItemEventArgs e)
{

string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();

if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{

DropDownList dd =
(DropDownList)e.Item.FindControl("ddl");

if(dd !=null)
{
dd.DataSource = bs.drPriority();

What is "bs" ??

dd.DataTextField = "text";
dd.DataValueField = "value";
dd.DataBind();

dd.Items.FindByValue(dr["Priority"].ToString()).Selected = true;

}

}
}

It would help if you specify the line the error is in.

Hans Kesting
 
Karl THANK U SO MUCH.. its working now :)

just wondering why it didn't work with DataRowView.. and what is DbDataRecord?

Many thanks again for your help..
PS: your article on databinding is exellent.. :)

Karl Seguin said:
string categoryName =
((DbDataRecord)e.Item.DataItem)["FaultDesc"].ToString();

it's in the System.Data.Common namespace, so either add

using System.Data.Common atop your file, or do

string categoryName =
((System.Data.Common.DbDataRecord)e.Item.DataItem)["FaultDesc"].ToString();


Karl


--
MY ASP.Net tutorials
http://www.openmymind.net/


huzz said:
sorry karl i am very new to asp.net.. what is DbDataReader? can you show me
code?

Many thanks for your help

Karl Seguin said:
Try casting it to DbDataRecord (I think)

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


The repeater is binded to datareader

:

What are you binding your repeater to?

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


karl thanks for your quick response.. after i moved the line of code
inside
the if statement and now getting this message

"Specified cast is not valid. "

Line 64: string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();


:

e.Item.DataItem is null. The problem is you are checking for
ItemType
after
getting the categoryName. However, if you are in a headerTemplate
or a
footerTemplate, e.Item.DataItem will be null. Simply move that code
inside
your if statement:

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem){
string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();
...
}

should work

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/


Hi Hans

Here is the line no:
Line 57: string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();


:

huzz wrote:
I am trying to assign a value from DataItem to a variable but
keeping
getting " Object reference not set to an instance of an
object"
error
message.

what i am missing?

public void create_ddl(object s, RepeaterItemEventArgs e)
{

string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();

if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{

DropDownList dd =
(DropDownList)e.Item.FindControl("ddl");

if(dd !=null)
{
dd.DataSource = bs.drPriority();

What is "bs" ??

dd.DataTextField = "text";
dd.DataValueField = "value";
dd.DataBind();

dd.Items.FindByValue(dr["Priority"].ToString()).Selected =
true;

}

}
}

It would help if you specify the line the error is in.

Hans Kesting
 
Thanks for the kind words, but clearly my databinding arcticle was missing a
key piece of information :)

As I say in the article, when you bind to a dataset, datatable or dataview,
the individual items are of type DataRowView.

What I didn't say, is when you bind to a datareader, the individual items
are of type DbDataRecord. DbDataRecord is the underlaying common type that
all rows of an DataReader (OledbDataReader, SqlDataReader, OracleDataReader)
are based on :)

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


huzz said:
Karl THANK U SO MUCH.. its working now :)

just wondering why it didn't work with DataRowView.. and what is DbDataRecord?

Many thanks again for your help..
PS: your article on databinding is exellent.. :)

Karl Seguin said:
string categoryName =
((DbDataRecord)e.Item.DataItem)["FaultDesc"].ToString();

it's in the System.Data.Common namespace, so either add

using System.Data.Common atop your file, or do

string categoryName =
((System.Data.Common.DbDataRecord)e.Item.DataItem)["FaultDesc"].ToString();


Karl


--
MY ASP.Net tutorials
http://www.openmymind.net/


huzz said:
sorry karl i am very new to asp.net.. what is DbDataReader? can you
show
me
code?

Many thanks for your help

:

Try casting it to DbDataRecord (I think)

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


The repeater is binded to datareader

:

What are you binding your repeater to?

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


karl thanks for your quick response.. after i moved the line
of
code
inside
the if statement and now getting this message

"Specified cast is not valid. "

Line 64: string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();


:

e.Item.DataItem is null. The problem is you are checking for
ItemType
after
getting the categoryName. However, if you are in a headerTemplate
or a
footerTemplate, e.Item.DataItem will be null. Simply move
that
code
inside
your if statement:

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem){
string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();
...
}

should work

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/


Hi Hans

Here is the line no:
Line 57: string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();


:

huzz wrote:
I am trying to assign a value from DataItem to a
variable
but
keeping
getting " Object reference not set to an instance of an
object"
error
message.

what i am missing?

public void create_ddl(object s, RepeaterItemEventArgs e)
{

string categoryName =
((DataRowView)e.Item.DataItem).Row["FaultDesc"].ToString();

if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{

DropDownList dd =
(DropDownList)e.Item.FindControl("ddl");

if(dd !=null)
{
dd.DataSource = bs.drPriority();

What is "bs" ??

dd.DataTextField = "text";
dd.DataValueField = "value";
dd.DataBind();

dd.Items.FindByValue(dr["Priority"].ToString()).Selected =
true;

}

}
}

It would help if you specify the line the error is in.

Hans Kesting
 
Back
Top