c# - Three-tier architecture implementation in Windows form application -
i trying insert data database using three-tier architecture, stuck , cannot proceed further.
this code
first ui part:
public void assignfield() { string maritalcondition = ""; string sex = ""; assignobj.registered_date = datetimepicker1_date.value; assignobj.first_name = txt_firstname.text; if (combobox2_maritalstatus.selectedindex == 0) { maritalcondition = "single"; } else maritalcondition = "married"; assignobj.marital_status = maritalcondition; if (radiobutton_male.checked == true) sex = "male"; else sex = "female"; assignobj.gender = sex; this.txt_age.text = convert.toint32(age).tostring(); } private void btnregister_click(object sender, eventargs e) { assignfield(); } next middle tier:
public class customerdatatype { private datetime registered_date; private string first_name; private int age; private string marital_status; private string gender; public datetime registered_date { { return registered_date; } set { registered_date = value; } } public string first_name { { return first_name; } set { first_name = value; } } public int age { { return age; } set { age = value; } } public string marital_status { { return marital_status; } set { marital_status = value; } } public string gender { { return gender; } set { gender = value; } } public void insertinfo() { customerdataaccess insertobj = new customerdataaccess(registered_date, first_name, age, marital_status, gender); insertobj.insertcustomerinfo(); } } and last data access tier:
public class customerdataaccess { public customerdataaccess(datetime registered_date, string first_name, int age, string marital_status, string gender) { this.registrationdate = registered_date; this.fname = first_name; this.userage = age; this.marriagestatus = marital_status; this.usergender = gender; } sqlconnection con; sqlcommand cmd; datetime registrationdate; string fname = ""; int userage; string marriagestatus; string usergender; public void insertcustomerinfo() { try { con = new sqlconnection("data source=lakhe-pc;initial catalog=sahakari;integrated security=true"); con.open(); cmd = con.createcommand(); cmd.commandtext = "sp_registercust"; cmd.commandtype = commandtype.storedprocedure; cmd.parameters.add("@registered_date", sqldbtype.datetime); cmd.parameters["@registered_date"].value = registrationdate; cmd.parameters.add("@first_name", sqldbtype.varchar); cmd.parameters["@first_name"].value = fname; cmd.parameters.add("@age", sqldbtype.int.tostring()); cmd.parameters["@age"].value = userage; cmd.parameters.add("@marital_status", sqldbtype.varchar); cmd.parameters["@marital_status"].value = marriagestatus; cmd.parameters.add("@gender", sqldbtype.varchar); cmd.parameters["@gender"].value = usergender; cmd.executenonquery(); con.close(); } catch (exception ex) { messagebox.show(ex.message); } } here stored procedure, there no problem , and sql server can insert data table easily. windows form, not insert data in table. plz me.
i'll below
ui
customerhandler custhandler = new customerhandler(); // create customer object , pass insert method if (custhandler.insertcustomer(new customer(){ firstname = txt_firstname.text, registered_date =datetimepicker1_date.value, //decalare other parameters.... )) { // insert success, show message or update label succcess message } in bl
public class customerhandler { // in bl may have call several dal methods perform 1 task // here have added validation , insert // in case of validation fail method return false public bool insertcustomer(customer customer) { if (customerdataaccess.validate(customer)) { customerdataaccess.insertcustomer(customer); return true; } return false; } } in dal
// class going use transfer data across layers public class customer { public datetime registered_date { get; set; } public string firstname { get; set; } //so on... } public class customerdataaccess { public static void insertcustomer(customer customer) { using (var con = new sqlconnection("data source=lakhe-pc;initial catalog=sahakari;integrated security=true")) using (var cmd = con.createcommand()) { con.open(); cmd.commandtext = "sp_registercust"; cmd.commandtype = commandtype.storedprocedure; cmd.parameters.addwithvalue("@registered_date", customer.registered_date); cmd.parameters.addwithvalue("@firstname", customer.firstname); // on... cmd.executenonquery(); } } internal static bool validate(customer customer) { // validations before insert } }
Comments
Post a Comment