Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void flowButton3_Click(object sender, EventArgs e)
{
 string pq = "sold";
 try{
  OleDbConnection con = new 
  OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Login App\SmartShop.accdb");

  OleDbCommand update = new OleDbCommand("UPDATE Items SET[status] = '" + pq + "',  WHERE [ItemID] IN (" + String.Join(",", Array.ConvertAll(strarray, z => "'" + z + "'")) + ")");
  con.Open();
  update.ExecuteNonQuery();
  con.Close();
 }
 catch
 {
  throw;
 }
}
Posted
Updated 11-May-15 0:18am
v2

Yes it is because you didn`t introduce your connection to command object. which means you need to do like this:

C#
string queryString = "SELECT OrderID, CustomerID FROM Orders";
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        OleDbCommand command = new OleDbCommand(queryString, connection);
        connection.Open();
        OleDbDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
            Console.WriteLine(reader.GetInt32(0) + ", " + reader.GetString(1));
        }
        // always call Close when done reading.
        reader.Close();
    }


So, if I want to write your code I prefer the below one:
C#
private void flowButton3_Click(object sender, EventArgs e)
{
 string pq = "sold";
 try{
  OleDbConnection con = new 
  OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Login App\SmartShop.accdb");
 String update_sql = "UPDATE Items SET[status] = '" + pq + "',  WHERE [ItemID] IN (" + String.Join(",", Array.ConvertAll(strarray, z => "'" + z + "'")) + ")";// see your sql command
  OleDbCommand update = new OleDbCommand(update_sql,con);// see your ADO.net object
  con.Open();
  update.ExecuteNonQuery();
  con.Close();
 }
 
 catch (Exception ex)
 {
//do something in your catch body like the below 
    Console.WriteLine(ex.Message);
 }
 
}


One more update, try to don`t use blank char in Database path and try to use several objects and variables for storage input parameters because they can help you in truck, maintenance and code readability.
For more information read the blow link:
https://msdn.microsoft.com/en-us/library/dw70f090(v=vs.110).aspx[^]
R,
Aydin
 
Share this answer
 
v5
Comments
Sinisa Hajnal 11-May-15 6:29am    
Add finally block and dispose of your connection and command objects properly.
Aydin Homay 11-May-15 6:33am    
Yes it is good idea but I left it as a practice for our friend ;-)
Sinisa Hajnal 11-May-15 7:36am    
And the comment was intended for him since you copied his method.
Aydin Homay 11-May-15 8:39am    
Yes I did, I copied his code because I just want to use his hand write what are you looking in this answer feel free and tell me :-)
Dakota335 12-May-15 2:01am    
Thanks so much. Solved all my problems :)
You guys rock ^_^
The problem is that even you have an opened connection, update object does not know about it nothing...
You should add this line after opening connection:
C#
update.Connection = con;

https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.connection(v=vs.110).aspx[^]
 
Share this answer
 
You haven't told the OleDbCommand what connection to use.

Try this :
C#
private void flowButton3_Click(object sender, EventArgs e)
{
    string pq = "sold";
    try
    {
        OleDbConnection con = new
        OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Login App\SmartShop.accdb");

        OleDbCommand update = new OleDbCommand("UPDATE Items SET[status] = '" + pq + "',  WHERE [ItemID] IN (" + String.Join(",", Array.ConvertAll(strarray, z => "'" + z + "'")) + ")",con); // see the connection here.
        con.Open();
        update.ExecuteNonQuery();
        con.Close();
    }
    catch
    {
        throw;
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900