c# - autoincrement number in textbox -
how autoincrement value in textbox1(which id) everytime click save button? problem value in textbox1 increments everytime run winform.
for example, run winform , last value id has 2, textbox1 generate number 3 , after click save button doesn't generate next number instead increment if close form , rerun again. plsss help..
in form load
private void form1_load(object sender, eventargs e) { int a; string path1 = "data source=localhost; initial catalog= ss; username=root; password=''"; mysqlconnection con = new mysqlconnection(path1); con.open(); string query = "select max(id) inc"; mysqlcommand cmd = new mysqlcommand(query, con); mysqldatareader dr = cmd.executereader(); if (dr.read()) { string val = dr[0].tostring(); if (val == "") { textbox1.text = "1"; } else { = convert.toint32(dr[0].tostring()); = + 1; textbox1.text = a.tostring(); } } }
in save button
private void button1_click(object sender, eventargs e) { string path = "data source=localhost; initial catalog= ss; username=root; password=''"; mysqlconnection sqlconn = new mysqlconnection(path); //communicator //constructors mysqlcommand sqlcomm = new mysqlcommand(); mysqldatareader sqldr; sqlconn.open(); sqlcomm.connection = sqlconn; sqlcomm.commandtype = commandtype.text; sqlcomm.commandtext = "select * inc id=" + textbox1.text + ""; sqldr = sqlcomm.executereader(); sqldr.read(); if (sqldr.hasrows) { textbox3.text = sqldr[0].tostring(); } sqlconn.close(); if (textbox1.text == textbox3.text) { messagebox.show("id exists!"); } else { string path1 = "data source=localhost; initial catalog= ss; username=root; password=''"; mysqlconnection sqlconnn = new mysqlconnection(path1); //communicator //constructors mysqlcommand sqlcommm = new mysqlcommand(); sqlconnn.open(); sqlcommm.connection = sqlconnn; sqlcommm.commandtype = commandtype.text; sqlcommm.commandtext = "insert inc (id,lastname) values ("+ textbox1.text +", '" + textbox2.text + "')"; sqlcommm.executenonquery(); sqlconnn.close(); messagebox.show("record saved!"); } }
what need update textbox1.text after saving record
... sqlcommm.executenonquery(); sqlconnn.close(); messagebox.show("record saved!"); //update textbox1.text ....
you have generate next value next saving!
Comments
Post a Comment