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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object reference not set to an instase of an object

Status
Not open for further replies.

cyrus71

Programmer
Feb 6, 2006
34
SE
I have been working on my web form in 6 days and i am still in the begining.
here is my code and the Error is "Object reference not set to an instase of an object"

public partial class SearchUC : System.Web.UI.UserControl, ISelectedTitleFeed
{
private DataSet DS;

protected void SearchButton_Click(object sender, EventArgs e)
{
DS = minWebService(); // a reference to a web service, returns a dataset

foreach (DataRow DR in DS.Tables["items"].Rows)
{
DropDownlist1.Items.Add(DR["title"].ToString());
}
}

protected void DropDownList1_Changed(object sender, EventArgs e)
{
string temp = string.empty;
foreach (DataRow DR in DS.Tables["items"].Rows) // here i get the error.
temp += DR["title"].ToString();
Label1.Text = temp;
}
}

I must say that i click on Button first then i choose a item in the DropDownlist
 
DS = minWebService(); // a reference to a web service, returns a dataset

foreach (DataRow DR in DS.Tables["items"].Rows) p
Since minWebService is a remote invocation, you probably ought to check DS for null before using it.
Code:
DS = minWebService(); // a reference to a web service, returns a dataset
if (null == DS)
   return;
foreach (DataRow DR in DS.Tables["items"].Rows)
Same applies in your Changed event, if it's possible for DS to be null (uninitialized, returned as null, etc), you need to check for it.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Some other ideas, maybe you want to check to see if the dataset actually has any data before you try to interate through the datareader.

if(DS.HasRows())....

As an aside, I noticed you are appending strings. You might benefit in this case from using the string builder class if you have alot of rows in your datareader and simply appending to your string builder object through each pass in the datareader.

Also setting your label1.Text to the string variable AFTER iterating through your data reader may provide some performance gain.

Then I noticed you are using the datareader item to string method. Could simply calling DR.GetString(item) be faster? Supposedly accessing the item number of the data reader is faster than accessing the item name.

The wisest people are those who are smart enough to realize they don't know it all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top