c# - What's wrong with my IF statement? -


i'm creating auditting table, , have easy insert , delete auditting methods done. i'm bit stuck on update method - need able current values in database, new values in query parameters, , compare 2 can input old values , changed values table in database.

here code:

    protected void sqldatasource1_updating(object sender, sqldatasourcecommandeventargs e)     {         string[] fields = null;         string fieldsstring = null;         string fieldid = e.command.parameters[5].value.tostring();         system.security.principal.  windowsprincipal p = system.threading.thread.currentprincipal system.security.principal.windowsprincipal;         string[] namearray = p.identity.name.split('\\');         string name = namearray[1];         string querystringupdatecheck = "select vaxcode, reference, costcentre, department, reportingcategory nominalcode id = @id";         string querystring = "insert audit (source, action, itemid, item, userid, timestamp) values (@source, @action, @itemid, @item, @userid, @timestamp)";         using (sqlconnection connection = new sqlconnection("con string = deleted privacy"))         {             sqlcommand commandcheck = new sqlcommand(querystringupdatecheck, connection);             commandcheck.parameters.addwithvalue("@id", fieldid);             connection.open();             sqldatareader reader = commandcheck.executereader();             while (reader.read())             {                 (int = 0; < reader.fieldcount - 1; i++)                 {                     if (reader[i].tostring() != e.command.parameters[i].value.tostring())                     {                         fields[i] = e.command.parameters[i].value.tostring() + "old value: " + reader[i].tostring();                     }                     else                     {                      }                 }             }                 fieldsstring = string.join(",", fields);                  reader.close();              sqlcommand command = new sqlcommand(querystring, connection);             command.parameters.addwithvalue("@source", "nominal");             command.parameters.addwithvalue("@action", "update");             command.parameters.addwithvalue("@itemid", fieldid);             command.parameters.addwithvalue("@item", fieldsstring);             command.parameters.addwithvalue("@userid", name);             command.parameters.addwithvalue("@timestamp", datetime.now);             try             {                 command.executenonquery();             }             catch (exception x)             {                 response.write(x);             }                         {                 connection.close();             }         }     } 

the issue i'm having fields[] array null. though vs debug window shows e.command.parameter.value[i] , reader[i] different, fields variable seems it's never input into.

thanks

you never set fields[] else null, null when trying access it. need create array before can assign values it. try:

           sqldatareader reader = commandcheck.executereader();            fields = new string[reader.fieldcount] 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -