Current location: Hot Scripts Forums » Programming Languages » Windows .NET Programming » Error in OleDBConnection : Object reference not set to an instance of an object


Error in OleDBConnection : Object reference not set to an instance of an object

Reply
  #1 (permalink)  
Old 04-09-05, 03:11 AM
pvsunil pvsunil is offline
New Member
 
Join Date: Feb 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Angry Error in OleDBConnection : Object reference not set to an instance of an object

I am running the following code. It executes when the value of variable i is 1 and when the loop continues and value of i becomes 2 i am getting error "Object reference not set to an instance of an object" in the cmdSearch.CommandText line in the code. . There is no problem with connection string. It successfully retrieves when i = 1. Can anybody help me out what is the problem. Please post with corrected code if anybody can help.
===========================================
for (int i=1; i <= objFolder.Items.Count; i++)
{
System.Data.OleDb.OleDbConnection odbSearch = new System.Data.OleDb.OleDbConnection();
System.Data.OleDb.OleDbCommand cmdSearch = new System.Data.OleDb.OleDbCommand();
odbSearch.ConnectionString = strConn;
cmdSearch.Connection = odbSearch;
item = (Outlook.ContactItem) objFolder.Items.Item(i);
string em = item.Email1Address;
cmdSearch.CommandText = "select * from Outlook_Contacts where FirstName='"+fn.ToString().Trim()+"' and LastName='"+ln.ToString().Trim()+"'";
odbSearch.Open();
OleDbDataReader rdrSearch = cmdSearch.ExecuteReader();
while( rdrSearch.Read())
{
RecordFlag = true;
}
odbSearch.Close();
}
===========================================
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 04-22-05, 02:45 PM
ben_ ben_ is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
This error typically means you're trying to access a control that doesn't exist.

What you need to do when you come across this error is go through your code to find what you're referencing that you haven't added to your web/windows form.

In this case I would suggest you look at item andRecordFlag to make sure they're declared somewhere, particularly item.Email1Address.

Also - make sure you close your datareader when you finish as well. Garbage collection will pick it up anyway, but if you re-use the datareader object later in a page/script garbage collection won't get it until it reaches the end of the processing.

rdrSearch.Close();

Ben
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
java set random weight in instance joebloggs2004 Everything Java 1 09-24-04 10:37 AM
Object Oriented Programming Stefan PHP 29 12-30-03 12:22 PM


All times are GMT -5. The time now is 02:05 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.