c# - Why SQL query only works when running from VS but not outside -
i have search textbox , submit button, user can come in name , nail button result. issue running when run website vs, query returns result when create website in web service , access using link outside of localhost link, doesn't work (displays failed: not not found in database).
my localhost link works search query: http://localhost:53647/pdfformsnew/searchcreate.aspx (running through vs)
my iis link accessing outside of vs doesn't work search query: http://devserver-svr-dev:85/searchcreate.aspx
any thought why causing , how prepare it?
i including snippets of code doing query:
string cstring = "data source=svr-db;initial catalog=test-db;user id=myusr;password=p@$$w0rd;"; protected void btnvalidatename_click(object sender, eventargs e) { this.sqlquery(); } public void sqlquery() { tc.text = "here"; conn = new sqlconnection(cstring); conn.open(); //namee = '%'+txtname.text+'%'; namee = '%' + removespecialcharacters(txtname.text) + '%'; sqlcode = "select * [test-db].[dbo].[tablepdftest] [name] @name"; //allow user come in partial using (sqlcommand command = new sqlcommand(sqlcode, conn)) { //command.commandtype = commandtype.text; command.parameters.addwithvalue("name", namee); using (reader = command.executereader()) { if (reader.hasrows) { rptcontent.datasource = reader; rptcontent.databind(); lblisvalid.text = "success: name found in database"; lblisvalid.forecolor = system.drawing.colortranslator.fromhtml("#009900"); //btngeneratepdf.visible = true; } if (!reader.hasrows) { rptcontent.datasource = null; rptcontent.datasourceid = null; rptcontent.databind(); lblisvalid.text = "failed: name not found in database"; lblisvalid.forecolor = system.drawing.colortranslator.fromhtml("#990000"); //btngeneratepdf.visible = false; } } } conn.close(); } public static string removespecialcharacters(string str) { stringbuilder sb = new stringbuilder(); foreach (char c in str) { if ((c >= 'a' && c <= 'z') || (c >= 'a' && c <= 'z') || c == '.') { sb.append(c); } } homecoming sb.tostring(); }
the problem pass invalid string parameter.
the statement "select * [test-db].[dbo].[tablepdftest] [name] @name" , pass
command.parameters.addwithvalue("name", namee);
but should
command.parameters.addwithvalue("@name", namee);
ps.
you should not utilize * in sql query should inquire field consume. not consume of info should utilize count(*) . using count should able gain performance.
c# asp.net sql visual-studio-2012
No comments:
Post a Comment