Adding cyanide vs nitrite for nucleophilic substitution and why does it change when we use silver salts for it
Potassium cyanide ($\ce{KCN}$) is a predominantly ionic compound ($\ce{K+}$ is Hard Acid while $\ce{CN-}$ is Soft base) and would dissociate completely to give cyanide ions in solution. Cyanide ion ($\ce{CN-}$) is considered an ambient nucleophile, which means that the negative charge can associate with either carbon or nitrogen. However, the negative charge of cyanide ion concentrated on carbon than nitrogen. Thus, reaction with haloalkanes yields cyanides (nitriles) since $\ce{C-C}$ bond is thermodynamically more stable than $\ce{C-N}$ bond (simplest explanation). 

Silver cyanide ($\ce{AgCN}$) is largely covalent and it does not dissociate easily to give $\ce{CN-}$ due to soft-soft interaction ($\ce{Ag+}$ is Soft Acid and $\ce{CN-}$ is Soft base). Thus, negative charge of $\ce{CN-}$ ion is mostly available at nitrogen, and hence the attacking centre of the nucleophile will be $\ce{N}$-atom. As a result, you get nucleophiluc substitution of haloalkanes to give isocyanides (isonitrile).

Same argument can be given for nitrite formation with $\ce{KNO2}$ and nitro formation with $\ce{AgNO2}$. In this case, $\ce{KNO2}$ is a predominantly ionic compound ($\ce{K+^-O-N=O}$) where one of oxygen atoms of nitrite ion has negative charge. Therefore, the nucleophilic attack of $\ce{NO2-}$ on haloalkane would result alkyl nitrite ($\ce{R-O-N=O}$). Silver nitrite ($\ce{AgNO2}$) is, on the other hand, largely covalent and would not dissociate easily to give free $\ce{NO2-}$ ions. As a results, the lone pair on nitrogen acts as an attacking site, forming nitroalkanes (high electronegativity of oxygen compared to nitrogen prevented it from donating its lone pair, as explainnation given by Clayden).