database - Passing variables through events c# -


sorry if looks kind of rookie :3 ... don't have experience on c#, code on c#, think it's flexible , friendly object-oriented language.

so well. i'm trying pass variables through button clicks, in order able make insert database values obtained of button clicks. have several buttons recycle "product" value different string, in natural language approach that:

if click "product" variable product gain name of button, following of click "quantity" button , give me quantity number, following program make insert db values. i'm doubtful of how recycle values depending on buttons clicked.

in brief, code have following:

    private string product;     private int quantity;     private char prodchosen;     private char quantitychosen;      private void product_click(object sender, eventargs e)     {       prodchosen = 'y';       product = "beer";     }       private void product2_click(object sender, eventargs e)     {       prodchosen = 'y';       product = "juice";     }      public void bttn1_click(object sender, eventargs e)     {       quantitychosen = 'y';       quantity = 1;       if (prodchosen == 'y' && quanitychosen == 'y')       {        mysqlconnection mcon = new mysqlconnection("server=localhost;database=***;username=root;password=***");        mcon.open();        string insert = "insert accounts product, quantity values" + product + " , " + quantity;          mysqlcommand cmd = new mysqlcommand(insert, mcon);        messagebox.show("product succesfully added");      }      else {        messagebox.show("quantity , product not recognized");      } 

after being inserted database, these values showed in gridview, select statement delimited combobox selectedindex value like:

    private void combobox1_selectedindexchanged(object sender, eventargs e)         {         clientchosen = 'y';         client_id = combobox1.selectedindex;          mysqlconnection mcon = new mysqlconnection("server=localhost;database=***;username=root;password=***");         mcon.open();          char open = 'y';          int selectedindex = combobox1.selectedindex;         object selecteditem = combobox1.selecteditem;          string select2 = "select * account client_id = " + selectedindex + " , open = " + open;          mysqlcommand cmd2 = new mysqlcommand(select2, mcon);          object result = cmd.executenonquery();      } 

so far haven't been able make successful insert, not show messagebox, i'll appreciate comments have issue.

try this

sqlconnection dbconnect = new sqlconnection("server=localhost;database=***;username=root;password=***"); sqlcommand cmd;      try     {         cmd = new sqlcommand("insert accounts(product, quantity) values(@prod, @quan)", dbconnect);         cmd.parameters.addwithvalue("@prod", your_text_box.text);         cmd.parameters.addwithvalue("@quan", your_text_box.text);         dbconnect.open();     } 

this way, insert values database. works , me


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -