c# - Calling class from code behind -
i still new asp.net , i'm learning how call classes. i've looked around tutorials, not specific asp.net 4.0 i'm not sure if should apply them.
right now, trying connect sql database. webconfig file has been set connectionstring "dbconnectionstring" , working when i've tested gridviews. i'd access database code behind.
i've seen ways accomplish this, i'd efficient, resuable way. i've tried adopt answer listed here: how create sql connection c# code behind, access sql server conditionally redirect? however, i'm getting error.
i doing c# website, not web application. here code behind login.aspx.cs:
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using sqlcomm; // how connect class page???? public partial class login : system.web.ui.page { protected void page_load(object sender, eventargs e) { } protected void btnsubmit_click(object sender, eventargs e) { if (page.isvalid) { //no code written yet } } }
my class in app_code folder, file name sqlcomm.cs:
using system; using system.collections.generic; using system.linq; using system.web; using system.data; using system.data.sqlclient; using system.web; /// <summary> /// sql query class /// </summary> public class sqlcomm { // connection string static string databaseconnectionstring = system.configuration.configurationmanager.connectionstrings["dbconnectionstring"].connectionstring; // execute sql command no value return public static void sqlexecute(string sql) { using (sqlconnection conn = new sqlconnection(databaseconnectionstring)) { sqlcommand cmd = new sqlcommand(sql, conn); cmd.connection.open(); cmd.executenonquery(); } } // execute sql query , return value public static object sqlreturn(string sql) { using (sqlconnection conn = new sqlconnection(pjsql.dbcs())) { conn.open(); sqlcommand cmd = new sqlcommand(sql, conn); object result = (object)cmd.executescalar(); return result; } } // retrieve entire table or part of public static datatable sqldatatable(string sql) { using (sqlconnection conn = new sqlconnection(databaseconnectionstring)) { sqlcommand cmd = new sqlcommand(sql, conn); cmd.connection.open(); datatable temptable = new datatable(); temptable.load(cmd.executereader()); return temptable; } } // execute stored procedure 1 parameter // returning value or executing no returns public static object sqlstoredprocedure1param(string storedprocedure, string prmname1, object param1) { using (sqlconnection conn = new sqlconnection(databaseconnectionstring)) { sqlcommand cmd = new sqlcommand(storedprocedure, conn); cmd.commandtype = commandtype.storedprocedure; cmd.parameters.add(new sqlparameter(prmname1, param1.tostring())); cmd.connection.open(); object obj = new object(); obj = cmd.executescalar(); return obj; } } }
as can see, haven't written code use class yet, "using sqlcomm;" line giving me error:
compiler error message: cs0246: type or namespace name 'sqlcomm' not found (are missing using directive or assembly reference?)
as i'm still new, i'm unsure go here. seem have included contained in answer on page linked above. else , missing?
edit: read post regarding similar issue: asp.net 2.0 web site can't access classes in app_code because doing website , not web application, app_code folder being handled differently when ftp files over?
using sqlcomm
means want use namespace sqlcomm
, doesn't exist. think want (somewhere in codebehind):
datatable dt = sqlcomm.sqldatatable(...) // call 1 of static methods.
Comments
Post a Comment