c# - Problem in using multiple sql queries on single button click -


i using following code , works fine.

protected void button2_click(object sender, eventargs e)     {         string = dropdownlist1.selecteditem.value;         string b = dropdownlist3.selecteditem.value.padleft(3, '0');               string c = textbox2.text.padleft(5,'0').tostring();         string d = textbox3.text.tostring();         string digit = a+ b  + c + d;         try         {          myconn = new odbcconnection("driver={mysql odbc 3.51 driver};server=localhost;database=testcase;user=root;password=root;option=3;");          myconn.open();             //**             string sql = "select * testcase.main reg_no =?";                         //**             odbccommand cmd = new odbccommand(sql, myconn);                         //**             cmd.parameters.addwithvalue("?", digit);             myreader = cmd.executereader();             //**             while (myreader.read())             {                 string f = myreader["pet_name"].tostring();                 string g = myreader["res_name"].tostring();                  label9.visible = true;                 label9.text = f;                  label10.visible = true;                 label10.text = "vs";                  //label11.visible = true;                 label11.text = g;              }              myreader.close();         }         catch (exception e1)         {             response.write(e1.tostring());         }                 {             if (myreader != null && !myreader.isclosed)             {                 myreader.close();                }              if (myconn != null && myconn.state == connectionstate.open)             {                 myconn.close();             }         } 

i want add sql query using 2 tables fetch data , want display in dropdownlist after fetching data database.

how should proceed?? should create totally new connection? tried many different ways like, creating new connection , new reader , entire try , catch block coded again on running website t taking long load contents.

what did modified portion(below code) , used entire try catch again dint work.

while (myreader1.read())             {                 string f = myreader1["ret"].tostring();               dropdownlist1.items.add(f);              } 

please help.

yes should create new connection , new reader. it's practice open , close connection possible. if content taking awhile load up, it's not connection creation. @ indexing of sql tables or how you're loading content.

from looks of it, if query returns multiple results (which i'm guessing it's not?), overwriting same label on , on agin.

you should wrap odbcconnection object & myreader object in using statements. way don't need .close statements & block. it's bit cleaner.

      private void dosomething()     {         string = dropdownlist1.selecteditem.value;         string b = dropdownlist3.selecteditem.value.padleft(3, '0');         string c = textbox2.text.padleft(5, '0').tostring();         string d = textbox3.text.tostring();         string digit = + b + c + d;         string sql = "select * testcase.main reg_no =?";          try         {             using (odbcconnection myconn = new odbcconnection("driver={mysql odbc 3.51   driver};server=localhost;database=testcase;user=root;password=root;option=3;"))  using(odbccommand cmd = new odbccommand(sql, myconn))                 {      myconn.open();                     //**                     cmd.parameters.addwithvalue("?", digit);                     using (odbcreader myreader = cmd.executereader())                     {                         //**                         while (myreader.read())                         {                             string f = myreader["pet_name"].tostring();                             string g = myreader["res_name"].tostring();                              label9.visible = true;                             label9.text = f;                              label10.visible = true;                             label10.text = "vs";                              //label11.visible = true;                             label11.text = g;                          }                     }                 }             }         }         catch (exception e1)         {             response.write(e1.tostring());         }     }  } 

edit - fixed nested statement


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -