c# - comparing array of chars to database records and read the record -


i trying change database mysql sql server 2008 express vs2010 c# project. after change connection string , queries, program produces error 'null reference exception unhandled' on "cmr.close()". here code , place error occurred :

namespace jawirdrsql {     /// <summary>     /// interaction logic mainwindow.xaml     /// </summary>     public partial class mainwindow : window     {         sqlconnection sc = new sqlconnection("data source=user-pc\\sqlexpress;initial catalog=firstdb;integrated security=true");         sqlcommand cmd;         sqldatareader cmr;         public mainwindow()         {             initializecomponent();         }           //string sc;         string strvalue;         private void button1_click(object sender, routedeventargs e)         {             strvalue = textbox1.text;                char[] strval = strvalue.tochararray();              array.reverse(strval);              foreach(char obj in strval)             {                 try                 {                    sc.open();                     cmd = new sqlcommand ("select jawireader (stringr)" + ((char)obj), sc);                     cmd.executenonquery();                      if (cmr.read())                     {                         label1.content += cmr["stringj"].tostring();                      }                     else                     {                         messagebox.show("tidak sah");                     }                  }                 catch (exception ex)                 {                     messagebox.show(ex.message);                 }                 cmr.close(); // error occurs here                 sc.close();             }         } 

i trying produce program received string break char , compared values in database. after program output string of values database.

since define as;

sqldatareader cmr; 

it null default. that's why nullreferenceexception when try call;

cmr.close(); 

because never initialize reader. don't need use executenonquery case. generate cmr executereader method like;

cmd = new sqlcommand ("select jawireader (stringr)" + ((char)obj), sc); cmr = cmd.executereader(); 

and please use using statement dispose connections, commands , reader.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -