Reply to topic
Error in OleDBConnection : Object reference not set to an in
pvsunil


Joined: 09 Apr 2005
Posts: 2
Location: Bangalore
Reply with quote
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();
}
===========================================
Josh
Forum Regular

Joined: 01 Apr 2004
Posts: 1029
Location: Felton, Delaware
Reply with quote
Have you declared your datareader? rdrSearch in your code?

Can you provide the full error given to you, and any other code or controls that might be related?
pvsunil


Joined: 09 Apr 2005
Posts: 2
Location: Bangalore
Reply with quote
This is the class level declaration part appears on top.

OleDbConnection odbSearch=null;
OleDbCommand cmdSearch=null;
OleDbDataReader rdrSearch;

The error is coming in the command line which is executing when i=2. When i =1 i am getting the value without any problem.
Josh
Forum Regular

Joined: 01 Apr 2004
Posts: 1029
Location: Felton, Delaware
Reply with quote
Try using...

Code:
for (int i=1; i <= objFolder.Items.Count - 1; i++) {
    ... other code here
}
Error in OleDBConnection : Object reference not set to an in
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic