c# - two queries in same method -


i trying execute 2 queries in same method gives me exception. can red of exception declaring new command there way use same command?

string id="hi";         connection.open();         oledbcommand command1 = new oledbcommand();         command1.connection = connection;         string query1 = "select * products category='" + combobox1.text + "' , subcategory = '" + combobox2.text + "' , sizes='" + combobox3.text + "'";         command1.commandtext = query1;         oledbdatareader reader = command1.executereader();         while (reader.read())         {             id = reader[0].tostring();         }         textbox1.text = id;          string query = "insert category_in  (category_id,amount,amount_in) values ('"+ id+"' ,500,300)";         command1.commandtext = query;         command1.executenonquery();         messagebox.show("saved");         connection.close(); 

you need close oledbdatareader object when finished it:

// previous code omitted brevity //  while (reader.read()) {     id = reader[0].tostring(); }  // need close reader // reader.close();  // run other query (omitted brevity) 

and comment on question states, way construct query extremely vulnerable sql inject attack. parameterize query proper way.

as per dmitry's comment, agree. wrap oledbdatareader object in using block call oledbdatareader.dispose() upon completion of block:

using (oledbdatareader reader = command1.executereader())  {     // ... } 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -