Error while trying to connect to sql server database from C#.Net -
here code
public string connectionstring = configurationmanager.appsettings["myconnection"]; public int get_details(string team1_name, string team2_name) { sqlconnection con = new sqlconnection(connectionstring); string sql = "insert table(team1_name,team2_name) values(@team1_name,@team2_name)"; sqlcommand cmd = new sqlcommand(sql,con); cmd.parameters.addwithvalue("@team1_name", team1_name); cmd.parameters.addwithvalue("@team2_name", team2_name); seek { con.open(); homecoming cmd.executenonquery(); } grab (exception ex) { throw ex; } { con.close(); } } whenever run code, error says
an unhandled exception of type 'system.invalidoperationexception' occurred in dblayer.dll
additional information: connectionstring property has not been initialized.
can help me out?
solution 1: need touse next statement initilialise connectionstring properly.
public string connectionstring =configurationmanager.connectionstrings [configurationmanager.appsettings["myconnection"]].connectionstring; solution 2:
table keyword hence need enclose within square brackets [].
replace this:
string sql = "insert table(team1_name,team2_name) values(@team1_name,@team2_name)"; with :
string sql = "insert [table](team1_name,team2_name) values(@team1_name,@team2_name)"; c# sql-server
No comments:
Post a Comment