where to add connection string of SQL server in asp.net page -


i having asp.net page containing textboxes namely username , password , button named cmdlogin. want when enter data in text boxes data should saved database.

in sql server of visual studio, have created table , have given

insert cmd_login values("......."). 

now problem when entered data in textbooxes not saved in database table. can do.

i have put connection string class file. need put connection string in web.config?

my code

using system; using system.collections.generic; using system.linq; using system.text; using system.data.sqlclient; public class db {     public static sqlconnection getconnection()     {         sqlconnection cn = new sqlconnection();         cn.connectionstring =@"data                 source=.\sqlexpress;attachdbfilename=e:\talat\myrealsacaproject\app_data\sacalogin.mdf;inte    grated security=true;user instance=true;";         cn.open();         return cn;     }     public static void saveadmin(admin a)     {         sqlconnection cn = getconnection();         string sql = "insert admin_login values(@[user-name],@password)";         sqlcommand cmd = new sqlcommand(sql, cn);         cmd.parameters.addwithvalue("@[user-name]",a.username);         cmd.parameters.addwithvalue("@password", a.password);         cmd.executenonquery();         cn.close();     }     } 

try simple code:

sqlconnection _conn = new sqlconnection(_connstring); _conn.open(); sqlcommand _cmd = new sqlcommand(); _cmd.commandtype = commandtype.storedprocedure; _cmd.parameters.clear(); _cmd.connection = _conn; _cmd.commandtext = "insert [tablename] value ([fieldvalue])"; //here textbox     int _execute = _cmd.executenonquery();  bool _result = false; if(_execute != 1)     _result = true;  _conn.close(); 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -