c# - NullReferenceException i am familiar with them, but cannot solve on this occasion -


im new asp.net familiar null reference exceptions cant solve one. im trying collect data webform , pass database through connection string, when exception occurs.

using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.data.sqlclient; using system.configuration;   public partial class register : system.web.ui.page {     protected void page_load(object sender, eventargs e)     {         try         {             if (ispostback)             {                 sqlconnection studconna = new sqlconnection(configurationmanager.connectionstrings["studconnection"].connectionstring);                 studconna.open();                 string checkuser = "select count(*) studtable name='" + textboxname.text + "'";                 sqlcommand studcoma = new sqlcommand(checkuser, studconna);                 int temp = convert.toint32(studcoma.executescalar().tostring());                 if (temp == 1)                 {                     response.write("user exists");                 }                 studconna.close();             }          }         catch (exception ex)         {             response.write("error:" + ex.tostring());         }     }       protected void button1_click(object sender, system.eventargs e)     {         try         {             sqlconnection studconn = new sqlconnection(configurationmanager.connectionstrings["studconnectionstring"].connectionstring);             studconn.open();             string insertquery = "insert studtable (name,email,age,continent,school,password) values (@name,@email,@age,@cont,@school,@pass)";             sqlcommand studcom = new sqlcommand(insertquery, studconn);             studcom.parameters.addwithvalue("@name", textboxname.text);             studcom.parameters.addwithvalue("@email", textboxemail.text);             studcom.parameters.addwithvalue("@age", textboxage.text);   studcom.parameters.addwithvalue("@cont",dropdowncont.selecteditem.tostring());             studcom.parameters.addwithvalue("@school", textboxschool.text);             studcom.parameters.addwithvalue("@pas", textboxpass.text);              studcom.executenonquery();             response.redirect("backend.aspx");             response.write("your registration sucessful");              studconn.close();         }         catch (exception ex)         {             response.write("error:" +ex.tostring());         }     } } 

the null reference occurs @ line 19

line 17:                 if (ispostback) line 18:                 { line 19:                     sqlconnection studconna = new sqlconnection(configurationmanager.connectionstrings["studconnection"].connectionstring); line 20:                     studconna.open(); line 21:                     string checkuser = "select count(*) studtable name='" + textboxname.text + "'"; 

i believe issue in syntax of connection string im not sure,

can me solve this?

check configuration file following key:

<connectionstrings>  <add name="studconnection" connectionstring="your details" /> </connectionstrings> 

if doesn't exist, add right key , retry.

you can check issue following code:

   if (ispostback)     {         // if line fails, don't have proper connection string         // element in config file.         debug.assert(configurationmanager.connectionstrings["studconnection"] != null);          sqlconnection studconna = new sqlconnection(configurationmanager.connectionstrings["studconnection"].connectionstring);         studconna.open();         string checkuser = "select count(*) studtable name='" + textboxname.text + "'";         sqlcommand studcoma = new sqlcommand(checkuser, studconna);         int temp = convert.toint32(studcoma.executescalar().tostring());         if (temp == 1)         {             response.write("user exists");         }         studconna.close();     } 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -