c# - how to return a class method's values to the respective pages that called it -
so i've got 2 pages in application. have mysql class containing connection parameters , query carried out. thing is, need both pages use same method in class , return data form triggered it.
this way works enough 1 page, don't know how use 2 or more of them.
//mysql library using mysql.data.mysqlclient; using mysql.data; namespace masca.users.mysql { public class registrationsql { // sql class knows registration page public users.registrationpages.registrationform registration; // sql class knows registration form , datagrid page public users.registrationpages.registrationformgrid registrationformgrid;
this method wish globally accessible.
// guess part cant figure out // set it's supposed ruturn strings right? // don't how add page return strings if calls method. public void foward (users.registrationpages.registrationform registrationform) { //declarations string datasource = "localhost"; string port = "3306"; string username = "root"; string password = "password"; //connection string sqlcon = "datasource = " + datasource + ";" + "port=" + port + ";" + "username=" + username + ";" + "password=" + password + ";"; //query string query = "select * temp.signup tag = (select min(tag) temp.signup tag > '" + registrationform.tag.text + "' );"; //mysql declarations mysqlconnection con = new mysqlconnection(sqlcon); mysqlcommand cmd = new mysqlcommand(query, con); mysqldatareader rdr; //excecution try { //if connection open con.open(); { rdr = cmd.executereader(); while (rdr.read()) { //string population // getstring & getcolumnvalueasstring predefined functions // check if field null or integer before returning string string sid = getstring(rdr, "id"); string smember = getcolumnvalueasstring(rdr, "member"); string stag = getcolumnvalueasstring(rdr, "tag"); string sfirst = getstring(rdr, "first"); string ssecond = getstring(rdr, "second"); string sthird = getstring(rdr, "third"); string sfourth = getstring(rdr, "surname"); string sdob = rdr.getstring("dob"); string ssex = rdr.getstring("sex"); string susername = getstring(rdr, "username"); string spassword = getstring(rdr, "password"); string ssecurity = getstring(rdr, "question"); string sanswer = getstring(rdr, "answer"); string spemail = getstring(rdr, "pemail"); string swemail = getstring(rdr, "wemail"); string sdoc = rdr.getstring("doc"); string scell = getstring(rdr, "cell"); string shome = getstring(rdr, "home"); string sext = getcolumnvalueasstring(rdr, "ext"); string sstreet = getstring(rdr, "street"); string ssurbub = getstring(rdr, "surbub"); string scity = getstring(rdr, "city"); string sregion = getstring(rdr, "region"); string sdept = rdr.getstring("dept"); string sbank = getstring(rdr, "bank"); string sbranch = getstring(rdr, "branch"); string saccount = getstring(rdr, "account"); // bindings // thing // if page calls method besides 'registrationform' // how direct strings 1 instead of 'registrationform' registrationform.id.text = sid; registrationform.member.text = smember; registrationform.tag.text = stag; registrationform.first.text = sfirst; registrationform.second.text = ssecond; registrationform.third.text = sthird; registrationform.surname.text = sfourth; registrationform.dob.text = sdob; registrationform.sex.text = ssex; registrationform.username.text = susername; registrationform.password.text = spassword; registrationform.question.text = ssecurity; registrationform.answer.text = sanswer; registrationform.pemail.text = spemail; registrationform.wemail.text = swemail; registrationform.doc.text = sdoc; registrationform.cell.text = scell; registrationform.home.text = shome; registrationform.ext.text = sext; registrationform.street.text = sstreet; registrationform.surbub.text = ssurbub; registrationform.city.text = scity; registrationform.region.text = sregion; registrationform.dept.text = sdept; registrationform.bank.text = sbank; registrationform.branch.text = sbranch; registrationform.account.text = saccount; } // close connection con.close(); } } catch (exception ex) { moderndialog.showmessage(ex.message , "sql related error", messageboxbutton.ok); } }
this how call method in 'registrationform'
private void next_click(object sender, routedeventargs e) { registratonsql.foward(this); }
this allow browsing of records using textboxes in page. clue on how go this?
if understand correctly, said have class accesses data, half of problem sorted. real problem caused because have mixed concerns in 1 class. in development, our aim each class have single purpose , current problem exactly reason why.
you need split out data access code 1 class , getting data ready display in ui in other classes. registrationpages
class should data , have methods names getthisdata
, getthatdata
(obviously not that).
your other classes both need instance of data access class access data , wrap ready display and/or editing... perhaps this:
registrationpages dataaccess = new registrationpages(); somecollectiontype data = dataaccess.getthisdata(); observablecollection<somedatatype> dataforui = preparedataforui(data); someuiproperty = dataforui;
while other class this:
registrationpages dataaccess = new registrationpages(); someothercollectiontype data = dataaccess.getthatdata(); observablecollection<someotherdatatype> dataforui = preparedataforui(data); someotheruiproperty = dataforui;
Comments
Post a Comment