c# - need help on Button event -
i have used 3-tier architecture insert database.i dont know code should write on button click event in .cs file.can some1 please me?
extension.cs
public static dbparameter createandsetparameter(this dbcommand cmd,string parametername,dbtype dbtype,int size,parameterdirection direction) { dbparameter p = cmd.createparameter(); p.parametername = parametername; p.dbtype =dbtype; p.size = size; p.direction = direction; cmd.parameters.add(p); return p; } public static dbparameter createandsetparameter(this dbcommand cmd, string parametername, dbtype dbtype, int size,object value) { dbparameter p = cmd.createparameter(); p.parametername = parametername; p.dbtype = dbtype; p.size = size; p.direction = parameterdirection.input; p.value = value; cmd.parameters.add(p); return p; }
datageneric.cs
public static int addslider(string @imgname,string @imgalt) { int rows=-1; string query="insert [slider](imagename,imagealt) values(@imgname,@imagealt)"; dbcommand cmd = datageneric.createcommand(); cmd.commandtext = query; cmd.createandsetparameter("@imgname",dbtype.string,50,imgname); cmd.createandsetparameter("@imgalt", dbtype.string, 50, imgalt); rows = datageneric.executenonquery(cmd); return rows; }
design:
imagename:<asp:textbox id="txt_name" runat="server" /><br /> imagealt:<asp:textbox id="txt_alt" runat="server" /> <asp:button id="btn" runat="server" onclick="btn_click" text="insert" />
code:
protected void btn_click(object sender, eventargs e) { ???????????? }
protected void btn_click(object sender, eventargs e) { datageneric.addslider(txt_name.text,txt_alt.text); }
as addslider static method can directly call method using class name
modify addslider methos
public static int addslider(string imgname,string imgalt) { int rows=-1; sqlconnection conn = new sqlconnection("connection string"); conn.open(); string query="insert [slider](imagename,imagealt) values(@imgname,@imagealt)"; dbcommand cmd = datageneric.createcommand(); cmd.connection = conn; cmd.commandtext = query; cmd.createandsetparameter("@imgname",dbtype.string,50,imgname); cmd.createandsetparameter("@imgalt", dbtype.string, 50, imgalt); rows = datageneric.executenonquery(cmd); conn.close(); return rows; }
Comments
Post a Comment