Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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();

}

===========================================

  • Administrators
Posted

Usually the array items are numbered starting from 0, this means if your array has 2 items then they will be 0 and 1. The .Length property returns the number of items (2 in this case). Try changing the loop start to

for (int i=0; i <= objFolder.Items.Count - 1; i++) 

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...