i have created method can search access database using oledbdatareader can't seem work out how can move next record using button.
please help!
private void button2_click(object sender, eventargs e) { system.data.oledb.oledbconnection conn = new system.data.oledb.oledbconnection(); conn.connectionstring = @"provider=microsoft.jet.oledb.4.0;data source=boilersvc_be.mdb"; try { conn.open(); oledbcommand command = new oledbcommand("select equipment.custid custid,equipment.manufacturer manufacturer,equipment.model model, equipment.lastservice lastservice,initial,surname,[address 1],[address 2],[address 3],[post town],[post code],telephone contacts inner join equipment on equipment.custid = contacts.custid surname = '" + textbox12.text + "' or initial = '" + textbox12.text + "' or[post town] = '" + textbox12.text + "' or[post code] = '" + textbox12 + "'", conn); command.parameters.add(new oledbparameter("@name", textbox12)); oledbdatareader reader = command.executereader(); while (reader.read()) { firstname.text = reader["initial"].tostring(); lastname.text = reader["surname"].tostring(); address1.text = reader["address 1"].tostring(); address2.text = reader["address 2"].tostring(); address3.text = reader["address 3"].tostring(); towncity.text = reader["post town"].tostring(); postcode.text = reader["post code"].tostring(); telephone.text = reader["telephone"].tostring(); lstsvcdat.text = reader["lastservice"].tostring(); boilerman.text = reader["manufacturer"].tostring(); boilermod.text = reader["model"].tostring(); } // insert code process data. } { conn.close(); }
you moving next record when call reader.read()
, suggest loading data need rather reading next record database based off of user input.
this how that
should define dtoobject containing of fields first
public class dtoobject { public string firstname { get; set;} public string lastname { get; set;} public string address1 { get; set;} public string address2 { get; set;} public string address3 { get; set;} public string towncity { get; set;} public string postcode { get; set;} public string telephone { get; set;} public string lstsvcdat { get; set;} public string boilerman { get; set;} public string boilermod { get; set;} }
then in method add list of dtos, , method load them valid form inputs, , methods move forward , backwards item
//in class private readonly list<dtoobject> _ls = new list<dtoobject>(); private int _currentindex = 0; private void loaddto(dtoobject object) { firstname.text = reader["initial"].tostring(); lastname.text = reader["surname"].tostring(); ... boilermod.text = reader["model"].tostring(); } private void movetonextitem() { _currentindex++; loaddtoobject(_ls[_currentindex]); } private void movetopreviousitem() { _currentindex--; loaddtoobject(_ls[_currentindex]); }
then inside method read read of them list , initialize value first index
//clear existing data _ls.clear(); while (reader.read()) { var current = new dtoobject(); current.firstname = reader["initial"].tostring(); current.lastname = reader["surname"].tostring(); current.address1= reader["address 1"].tostring(); current.address2= reader["address 2"].tostring(); current.address3= reader["address 3"].tostring(); current.towncity= reader["post town"].tostring(); current.postcode= reader["post code"].tostring(); current.telephone= reader["telephone"].tostring(); current.lstsvcdat= reader["lastservice"].tostring(); current.boilerman= reader["manufacturer"].tostring(); current.boilermod= reader["model"].tostring(); _ls.add(current); } //if there set first values current form if(_ls.any()) { _currentindex = 0; loaddto(_ls.first()); } else { //do notify no records found }
then create event handler
private void nextitembutton_click(object sender, eventargs args) { movetonextitem(); } private void previtembutton_click(object sender, eventargs args) { movetopreviousitem(); }
Comments
Post a Comment