c# - Issues inserting into sql database from asp.net -


i have lot more columns have here you, i'm trying post primary key column trackingid (nchar[10]) test - if can work, can rest of them work. have windows forms application makes similar query , runs perfectly, seeing how it's both c# i'm having hard time understanding what's going wrong. connection string or sql query? table name full. there 23 columns total, primary key tracking id cannot null. in instances, these queries onclick button event. able see have tried several routes. hoping set parameterized prevent sql injections. first bit of code see windows forms application in c# (while vulnerable) works consistently. below, see asp.net code.

    private void licensesubmitbutton_click(object sender, eventargs e)     {         try         {             system.data.sqlclient.sqlconnection sqlconnection1 =     new system.data.sqlclient.sqlconnection("data source=data99;initial catalog=licenseinventorymgr;integrated security=true;connect timeout=0;trusted_connection=yes");              system.data.sqlclient.sqlcommand cmd = new system.data.sqlclient.sqlcommand();             cmd.commandtype = system.data.commandtype.text;             cmd.commandtext = "insert limtable (software,host,assettag,activationdate,licensenumber) values ('" + softwarecombobox.text + "','" + hostnamebox.text + "','" + assettagcombobox.text + "','" + dateactivateddatetimepicker.value.tostring("yyyy-mm-dd") + "','" + licensekeytextbox.text + "')";             cmd.connection = sqlconnection1;              sqlconnection1.open();             cmd.executenonquery();             sqlconnection1.close();             messagebox.show("successfully submitted!");         }         catch (system.exception )         {             messagebox.show("submission unsuccessful. try saving , refreshing first!");         }     } 

and here asp.net code i've tried.

    protected void submit_click(object sender, eventargs e)     {         try         {             sqlconnection conn = new sqlconnection(@"data source=data99;initial catalog=lfm_archive;integrated security=true");             string sql = "insert full (trackingid) values (@trackingid)";             conn.open();             sqlcommand cmd = new sqlcommand(sql, conn);             cmd.parameters.add("@trackingid", sqldbtype.nchar);             cmd.parameters["@trackingid"].value = 6789;             cmd.executenonquery();             conn.close();              mailmessage m1 = new mailmessage("y@x.net", "x@x.net");             m1.subject = "sql query success";             m1.body = "sql query success :)";             m1.isbodyhtml = true;             smtpclient smtp = new smtpclient();             smtp.host = "mailserver99";             smtp.enablessl = false;             //system.net.networkcredential networkcred = new system.net.networkcredential();             //networkcred.username = "username";             //networkcred.password = "password";             smtp.usedefaultcredentials = true;             //smtp.credentials = networkcred;             smtp.port = 25;             smtp.send(m1);          }         catch         {             mailmessage m2 = new mailmessage("y@x.net", "x@x.net");             m2.subject = "sql query failed";             m2.body = "sql query failed :(";             m2.isbodyhtml = false;             smtpclient smtp = new smtpclient();             smtp.host = "mailserver99";             smtp.enablessl = false;             smtp.usedefaultcredentials = true;             smtp.port = 25;             smtp.send(m2);         }     } 

the end result displays query success or failure in form window pulls from, and/or sends email. when no longer have test it, emails simple notifications form has been posted archive. commented code in asp.net application means tried , when didn't work, commented , tried new method.

it suggested try , put in exception message see better - helps some. submitsuccess.text messages come up. here's results: "incorrect syntax near keyword 'full'."

that makes sound it's query. "full" table name in db.

answered here's worked:

            sqlconnection conn = new sqlconnection(@"data source=data99;initial catalog=lfm_archive;integrated security=true");             string sql = "insert [full] (trackingid) values (@trackingid)";             conn.open();             sqlcommand cmd = new sqlcommand(sql, conn);             cmd.parameters.add("@trackingid", sqldbtype.nchar, 10);             cmd.parameters["@trackingid"].value = empfirst.text;             cmd.executenonquery();             conn.close(); 

the problem full reserved word in sql. see this technet link.

to solve problem, use brackets on reserved keywords so:

string sql = "insert [full] (trackingid) values (@trackingid)"; 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -