Hello everyone!
I have this code

string constring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DataBasePath + ";Persist Security Info=False;Jet OLEDB:Database Password="";
conn = new OleDbConnection(constring);
conn.Open()
string searchstring = "SELECT * FROM MyTable WHERE field2=@field2";
OleDbCommand cmdsearch = new OleDbCommand(searchstring, conn);
OleDbDataReader reader;
cmdsearch.Parameters.AddWithValue("@field2", dgrDoc[1, index].Value.ToString());
reader = cmdsearch.ExecuteReader();

How can I check if MyTable contains rows from dgrDoc, and if it does, to update in MyTable the row affected, else to insert new data in MyTable.
I use

if(reader.Read())
{
if(!reader.IsDBNul(1))
{
//update comand
}
else
{
//insert command
}
}

but it takes only rows contained in MyTable.
Can anyone help me? Thanks!

This is solved. Here is the code:

int index=0;
while(index<dgrDoc.Rows.Count){   //here I used for(index=0...) and I got error
  string constring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DataBasePath + ";Persist Security Info=False;Jet OLEDB:Database Password="";
  conn = new OleDbConnection(constring);
  conn.Open()
  string searchstring = "SELECT * FROM MyTable WHERE field2=@field2";
  OleDbCommand cmdsearch = new OleDbCommand(searchstring, conn);
  OleDbDataReader reader;
  cmdsearch.Parameters.AddWithValue("@field2", dgrDoc[1, index].Value.ToString());
  reader = cmdsearch.ExecuteReader();
  if(reader.Read()){
    //update command
    index++;
  }
  else{
    //insert command
    index++;
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.