c# mysql unable to output query to a textbox -
here code:
private void searchindatabase() { mysqlconnection c = new mysqlconnection("datasource=localhost; username=root; password=123456; port=3306"); mysqlcommand mcd; mysqldatareader mdr; string query; try { c.open(); query = "select * test.classmates first_name ='"+searchname.text+"'"; mcd = new mysqlcommand(query, c); mdr = mcd.executereader(); if(mdr.read()) { firstname.text = mdr.getstring("first_name"); middlename.text = mdr.getstring("middle_name"); lastname.text = mdr.getstring("last_name"); age.text = mdr.getstring("age"); } else { messagebox.show("result not found"); } } catch(exception error) { messagebox.show("error: "+error.message); } { c.close(); } }
i ask if have missed on or doing wrong. if have free time, appreciate if comment perfect way implement problem: want data mysql put in textbox.
according msdn need pass column number parameter
public override string getstring(int i)
so try pass column number (starts 0) of column name. assuming first_name first column of table then
firstname.text = mdr.getstring(0);
update
try use mysqlconnectionstringbuilder
mysqlconnectionstringbuilder conn_string = new mysqlconnectionstringbuilder(); conn_string.server = "serverip/localhost"; conn_string.userid = "my_user"; conn_string.password = "password"; conn_string.database = "my_db"; mysqlconnection conn = new mysqlconnection(conn_string.tostring();
Comments
Post a Comment