title: Problems Inserting Byte[] into SqlDataSource ASP.Net
body: <p>Maybe I'm just not googling the right parameters, but I just can't seem to figure this one out.<br>
  I've created a hashed password, which from my understanding, should be a byte array.
  Then I need to insert that into the SqlDataSource and there's where my problem lies.</p>
  
  <p>My database definition is as follows;</p>
  
  <pre> 
  **Column**  || **Data Type**
  ----------------------------  
  Name        || varchar(50)  
  Username    || varchar(50)  
  HashedPwd   || varbinary(50)  
  Salt        || varbinary(50)  
  UserType    || varchar(50) </pre>
  
  <p>My code is as follows;  </p>
  
  <pre><code>HashAlgorithm hashing = new SHA256Managed();
  byte[] passwordPlainText = (new System.Text.ASCIIEncoding()).GetBytes(txtPassword.ToString());
  byte[] salt = (new System.Text.ASCIIEncoding()).GetBytes(RNGCryptoServiceProvider.Create().ToString());
  // Combine salt and password before hashing.
  byte[] saltAndPwd = new byte[passwordPlainText.Length + salt.Length];
  Array.Copy(passwordPlainText, saltAndPwd, passwordPlainText.Length);
  Array.Copy(salt, 0, saltAndPwd, passwordPlainText.Length, salt.Length);
  byte[] hashedPwd = hashing.ComputeHash(saltAndPwd);
  
  database.InsertCommandType = SqlDataSourceCommandType.Text;
  database.InsertCommand = "INSERT INTO userlist (Name, Username, HashedPwd, Salt, Type) VALUES (@insName, @insUsername, @insHashedPass, @insSalt, @insType);";
  database.InsertParameters.Add("insName", txtName.Text);
  database.InsertParameters.Add("insUsername", txtUsername.Text);
  database.InsertParameters.Add("insHashedPass", (new System.Text.ASCIIEncoding()).GetString(hashedPwd));
  database.InsertParameters.Add("insSalt", (new System.Text.ASCIIEncoding()).GetString(salt));
  database.InsertParameters.Add("insType", txtType.Text);
  database.Insert();
  </code></pre>
  
  <p>The database stuff is what I've gleaned from research on the internet and seeing what options I'm given with the handy autocomplete feature in Visual Studio.<br>
  FYI I'm using Visual Studio 2010 Version 10.0.4, .Net Version 4.0.3<br>
  It seems there's some external libraries for SQL connections, but I would prefer to just use the inbuilt stuff the .Net provides.</p>
  
  <p>At the moment, I recieve this error when running the insert algorithm;<br>
  <strong>Implicit conversion from data type nvarchar to varbinary is not allowed. Use the CONVERT function to run this query.</strong><br>
  I'm not sure how I would use the convert function.<br>
  The Add() method can take string, string like I'm currently doing or it can also take string, DbType, string.  I've tried <code>database.InsertParameters.Add("insSalt", DbType.Binary, salt);</code> but I can't even compile/build the website. I get; "Cannot convert from byte[] to String".<br>
  If I change it to <code>salt.ToString()</code> I get; "Failed to convert parameter value from a String to a Btye[]" on the Insert() line.  </p>
  
  <p>So yeah...really lost, any help would be greatly appreciated.</p>