c# - An unhandled exception of type 'System.InvalidOperationException' occurred in MySql.Data.dll -


                           //codigo de pesquisa         string strconexao = "persist security info =false ; server=localhost ; database= iconefileequipamentos; uid=root";         mysqlconnection oconn =new mysqlconnection(strconexao);         stringbuilder strsql =new stringbuilder();         datatable otable =new datatable();         mysqlcommand ocmd =new mysqlcommand();          oconn.open();          if (combobox1.text =="nomeresponsavel")         {             string nomeresponsavel = textbox1.text;              strsql.append("select *");             strsql.append("from responsavelpc");             strsql.append("where nomeresponsavel '" + nomeresponsavel + "'");         }          if (combobox1.text == "nomeequipamento")         {             string nomeequipamento = textbox1.text;             strsql.append("select *");             strsql.append("from responsavelpc");             strsql.append("where nomeequipamento like'" + nomeequipamento +              "'");         }         mysqldataadapter oda = new mysqldataadapter(strsql.tostring(), oconn);         oda.fill(otable); hear appears message put title!         datagridview1.datasource = otable;         oconn.close();                } 

this code shearch form have datagridview1 combobox1 , button textbox1 use have chose have shearch exeample nomeequipamento , put in textbox name of equipamento.sry if code crap iam nobbie :)

i think might because of missed white space. wild guess.

here corrected code. have added comment line

//codigo de pesquisa         string strconexao = "persist security info =false ; server=localhost ; database= iconefileequipamentos; uid=root";         mysqlconnection oconn =new mysqlconnection(strconexao);         stringbuilder strsql =new stringbuilder();         datatable otable =new datatable();         mysqlcommand ocmd =new mysqlcommand();          oconn.open();          if (combobox1.text =="nomeresponsavel")         {             string nomeresponsavel = textbox1.text;              //added white space @ end of each line             strsql.append("select * ");             strsql.append("from responsavelpc ");             strsql.append("where nomeresponsavel '" + nomeresponsavel + "'");         }          if (combobox1.text == "nomeequipamento")         {             string nomeequipamento = textbox1.text;             //added white space @ end of each line             strsql.append("select * ");             strsql.append("from responsavelpc ");             //added white space after             strsql.append("where nomeequipamento '" + nomeequipamento +"'");          }         mysqldataadapter oda = new mysqldataadapter(strsql.tostring(), oconn);         oda.fill(otable); hear appears message put title!         datagridview1.datasource = otable;         oconn.close();                } 

ps: if want implement search feature have use '% search_term %'. missing % sign in code. apart code not @ secure. use parameterized queries instead.


Comments