I started my project to get UID of Mifare 1k Classic Cards - this is working really good. Now I have to put my UID to a mySQL database. I converted my ByteArray to string - this works as well.
private string getcardUID()
{
string cardUID = "";
byte[] receivedUID = new byte[256];
Card.SCARD_IO_REQUEST request = new Card.SCARD_IO_REQUEST();
request.dwProtocol = Card.SCARD_PROTOCOL_T1;
request.cbPciLength = System.Runtime.InteropServices.Marshal.SizeOf(typeof(Card.SCARD_IO_REQUEST));
byte[] sendBytes = new byte[] { 0xFF, 0xCA, 0x00, 0x00, 0x00 };
int outBytes = receivedUID.Length;
int status = Card.SCardTransmit(hCard, ref request, ref sendBytes[0], sendBytes.Length, ref request, ref receivedUID[0], ref outBytes);
if (status != Card.SCARD_S_SUCCESS)
{
cardUID = "Error";
}
else
{
cardUID = BitConverter.ToString(receivedUID.Take(4).ToArray()).Replace("-", string.Empty).ToUpper();
}
return cardUID;
As we know im not able to put the string which finally looks like 0B05C2FD to my database. Tried different Datatypes like INT BIGINT and VARCHAR:
MySqlConnection connection = new MySqlConnection(MyConnectionString);
MySqlCommand cmd;
connection.Open();
string mycardUID = getcardUID();
int mycardUIDint = Convert.ToInt32(mycardUID, 16);
try
{
cmd = connection.CreateCommand();
cmd.CommandText = "INSERT INTO terminal(id,uid,buttoncode,timestamp,transfered,ts)VALUES(@id,@uid,@buttoncode,@timestamp,@transfered,@ts)";
cmd.Parameters.AddWithValue("@id", "");
cmd.Parameters.AddWithValue("@uid", mycardUID);
cmd.Parameters.AddWithValue("@timestamp", DateTime.Now);
cmd.Parameters.AddWithValue("@buttoncode", "1");
cmd.Parameters.AddWithValue("@transfered", "");
cmd.Parameters.AddWithValue("@ts", "");
cmd.ExecuteNonQuery();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
System.Windows.MessageBox.Show(ex.Message);
}
finally
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
}
Is there a solution? I tried to Convert to Int but this isnt possible with the Convert.ToInt32(value, 16); Wrong input <.<
edit: changed code added Convert.ToInt32
edit: I found something (c# bitconverter.ToString convert to hexadecimal string) but im not really sure if i'm able to apply this to my programm im really confused by the guy who asks and im even more confused about the answers :(