mysql - c# Auto populate result of a textboxt.Text from database, base on 2 different combo box -


namespace training { public partial class addingnewdata : form {     public addingnewdata()     {         initializecomponent();         fillcombo1();         fillcombo2();         autopopulatedays(); }  string original_city, destination_city;  void fillcombo1() {     string constring = "datasource=localhost;port=3306;username=root;password=root";     string query = "select * itemdelivery.fee group orig_city;";     mysqlconnection condatabase = new mysqlconnection(constring);     mysqlcommand cmddatabase = new mysqlcommand(query, condatabase);     mysqldatareader myreader;      try     {         condatabase.open();         myreader = cmddatabase.executereader();          while (myreader.read())         {             string storig = myreader.getstring("orig_city");             combobox1.items.add(storig);         }      }     catch (exception ex)     {         messagebox.show(ex.message);     } }  void fillcombo2() {     string constring = "datasource=localhost;port=3306;username=root;password=root";     string query = "select * itemdelivery.fee group dest_city;";     mysqlconnection condatabase = new mysqlconnection(constring);     mysqlcommand cmddatabase = new mysqlcommand(query, condatabase);     mysqldatareader myreader;      try     {         condatabase.open();         myreader = cmddatabase.executereader();          while (myreader.read())         {             string stdest = myreader.getstring("dest_city");             combobox2.items.add(stdest);         }      }     catch (exception ex)     {         messagebox.show(ex.message);     } }  private void combobox1_selectedindexchanged(object sender, eventargs e) {     original_city = combobox1.text; }  private void combobox2_selectedindexchanged(object sender, eventargs e) {     destination_city = combobox2.text; }  private void txt_deliverytime_textchanged(object sender, eventargs e) { }  void autopopulatedays() {     string constring = "datasource=localhost;port=3306;username=root;password=root";     string query = "select `del_time` `itemdelivery.fee` `orig_city` = @oc , `dest_city`= @dc";     mysqlconnection condatabase = new mysqlconnection(constring);     mysqlcommand cmddatabase = new mysqlcommand(query, condatabase);      try     {         condatabase.open();         cmddatabase.parameters.addwithvalue("@oc", original_city);         cmddatabase.parameters.addwithvalue("@dc", destination_city);          object result = cmddatabase.executescalar();         if (result != null)             txt_deliverytime.text = result.tostring();      }     catch (exception ex)     {         messagebox.show(ex.message);     }   } } } 

so, have 2 combo boxes, after selecting 2 of them, put values in original_city , destination_city string, using combobox1_selectedindexchanged , combobox2_selectedindexchanged.

then on method autopopulatedays(), tried auto-populate txt_deliverytime.text single integer value matching value of original_city , destination_city on database using query.

but, why failed? error got "no database selected" weird me, , when select 2 combo box choices, txt_deliverytime.text doesn't auto-populated.

========================================================================

// update version // connected database, , ignoring code before(above), // code meant auto-populate txt_deliverytime.text when // combobox1.selectitem , combobox2.selectitem // why it's still wrong?.        private void combobox1_selectedindexchanged(object sender, eventargs e)     {         original_city = combobox1.selecteditem.tostring();      }      private void combobox2_selectedindexchanged(object sender, eventargs e)     {         destination_city = combobox2.selecteditem.tostring();      // if combobox1 not empty , combobox2 not empty, run code         if (combobox1.selecteditem.tostring() != null)         {              string constring = "datasource=localhost;port=3306;username=root;password=root";             string query = "select del_time itemdelivery.fee orig_city='" + original_city + "' , dest_city='" + destination_city + "';";              // if run query on mysql, show column name del_time single row,               // show value, want value txt_deliverytime.text              mysqlconnection condatabase = new mysqlconnection(constring);              mysqlcommand cmddatabase = new mysqlcommand(query, condatabase);             condatabase.open();              string getvalue = cmddatabase.executescalar().tostring();             if (getvalue != null)             {                 txt_deliverytime.text = getvalue.tostring();         // meant change here, seems not successful             }             condatabase.close();          }     } 

first use valid connectionstring:

server=myserveraddress;port=1234;database=mydatabase;uid=myusername; 

pwd=mypassword;

then suppose not actual code, because there missing curly bracket

public partial class addingnewdata : form {     public addingnewdata()     {         initializecomponent();         fillcombo1();         fillcombo2();         autopopulatedays(); } 

and should `autopopulatedays' when combobox1 or combobox2 selected item change, not when fill combos.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -