I am trying to insert a Murmur3 hashed value into an nvarchar
column.
This is my code:
public int createUserAccount(string email, string password)
{
try
{
using (SqlCommand com = new SqlCommand("[dbo].[CreateUserAccount]"))
{
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@email", email.Trim().ToLower());
int HashedPass = HashAlgorithms.Murmur3.Murmur3_1.Hash(
new System.IO.MemoryStream(Utility.GetBytes(password)));
com.Parameters.AddWithValue("@password", HashedPass.ToString());
com.Parameters.AddWithValue("@appId", appId);
return selectInt(com);
}
}
catch (Exception)
{
return -1;
}
}
int selectInt(SqlCommand com)
{
try
{
using (SqlConnection con = new SqlConnection(connectionString))
{
com.Connection = con;
con.Open();
object o = com.ExecuteScalar();
con.Close();
if (o != null)
{
return int.Parse(o.ToString());
}
else
{
return 0;
}
}
}
catch (Exception e)
{
throw e;
}
}
When I checked the value of password
parameter that is passed through com
object to selectInt()
method, it's showing correct hashed value like -32xxxx.
But still I get this error:
Cannot insert the value NULL into column 'Password', table 'Accounts'; column does not allow nulls. INSERT fails. Line 17
Please tell me why this is happening?
It is a SQL error so check in your stored procedure because you are trying to insert null value in the 'Password' column. Also check if your procedure has any triggers or if it is calling other procedures and if the insert is being made through the trigger or the called procedure.