cell cell - 3 months ago 24
C# Question

How to Store a SByte[] array into Microsoft SQL Server

I want to store a Sbyte Array into Microsoft SQL Server .

[-18,17,-117,24,-57,19,-85,-87,-73,-76,8,18,-78,-71,-128,-36]


But I am getting an exception Failed to convert parameter value from a SByte[] to a Byte[].

AccountsController class

[Route("Login")]
[HttpPost]
public async Task<UserSession> Login()
{
string input = await Request.Content.ReadAsStringAsync();
dynamic data = Newtonsoft.Json.Linq.JObject.Parse(input);
LoginUser user = new LoginUser();
user.Email= data.email;
user.Password = data.password;
user.Token = data.token.ToObject<byte[]>();
byte[] mSessionID = new Session().getToken(user.Token);
/* using (System.Data.SqlClient.SqlConnection _conn = new System.Data.SqlClient.SqlConnection
("data source=devtest.dataquest.com.au;" + "initial catalog=Safe2Pay;" + "user id=sa;password=ipsec@21;"));
using (System.Data.SqlClient.SqlCommand _cmdLoginDetails = new System.Data.SqlClient.SqlCommand(
"INSERT into Login (" + "Email,Token,UserLogged)" + "VALUES(@user.Email,@Token,@UserLogged)", _conn))
{
System.Data.SqlClient.SqlParameter paramEmail = _cmdLoginDetails.Parameters.Add("@user.Email", System.Data.SqlDbType.NVarChar);
System.Data.SqlClient.SqlParameter paramToken = _cmdLoginDetails.Parameters.Add("@Token", System.Data.SqlDbType.Binary);
System.Data.SqlClient.SqlParameter paramUserLogged = _cmdLoginDetails.Parameters.Add(@UserLogge)

} */
_conn = new System.Data.SqlClient.SqlConnection("data source=devtest.dataquest.com.au;" + "initial catalog=Safe2Pay;" + "user id=sa;password=ipsec@21;");
System.Data.SqlClient.SqlCommand _cmdLoginDetails = new System.Data.SqlClient.SqlCommand("INSERT INTO Login ("+
"Email, Token, UserLogged)"+
"VALUES(@Email,@Token,@UserLogged)",_conn);
_cmdLoginDetails.Parameters.Add("@Email", System.Data.SqlDbType.NVarChar, 30).Value = user.Email;
_cmdLoginDetails.Parameters.Add("@Token", System.Data.SqlDbType.VarBinary).Value = mSessionID;
_cmdLoginDetails.Parameters.Add("@UserLogged", System.Data.SqlDbType.Bit).Value = 1;
_conn.Open();
_cmdLoginDetails.ExecuteNonQuery();
_conn.Close();
return new UserSession
{
SessionID = mSessionID
};
}


ByteArrayFormattor class

public override bool CanConvert(Type objectType)
{
return objectType == typeof(byte[]);
}


Session.class

public byte[] getToken(byte[] token)
{
new RNGCryptoServiceProvider().GetBytes(token);
return token;
}

Answer

Please find the updated code for Login. Though it is not saving an Sbyte into Database (Works fine)

[Route("Login")]
        [HttpPost]
        public async Task<UserSession> Login()
        {
            string input = await Request.Content.ReadAsStringAsync();
            dynamic data = Newtonsoft.Json.Linq.JObject.Parse(input);
            LoginUser loginuser = new LoginUser();
            loginuser.Email= data.email;
            loginuser.Password = data.password;
            var token = data.token;
            int count = 0;
            foreach (var tokens in token){
                int i = tokens;
                loginuser.Token[count++] = (byte)(i);   
            }
            byte[] mSessionID = new Session().getToken(loginuser.Token);
            _conn = new System.Data.SqlClient.SqlConnection("data source=devtest.dataquest.com.au;" + "initial catalog=Safe2Pay;" + "user id=sa;password=ipsec@21;");
            System.Data.SqlClient.SqlCommand _cmdLoginDetails = new System.Data.SqlClient.SqlCommand("INSERT INTO Login ("+
              "Email, Token, UserLogged)"+
                "VALUES(@Email,@Token,@UserLogged)",_conn);
            _cmdLoginDetails.Parameters.Add("@Email", System.Data.SqlDbType.NVarChar, 30).Value = user.Email;
            _cmdLoginDetails.Parameters.Add("@Token", System.Data.SqlDbType.Binary).Value = user.Token;
            _cmdLoginDetails.Parameters.Add("@UserLogged", System.Data.SqlDbType.Bit).Value = 1;
            _conn.Open();
            _cmdLoginDetails.ExecuteNonQuery();
            _conn.Close();
            return new UserSession
            {
                SessionID = mSessionID
            };        
        } 
Comments