0

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 :(

Community
  • 1
  • 1
  • What error message do you get? What's the data type of your `uid` field in the database? Where did you try to use `Convert.ToInt32` as this isn't present in the code you posted. – Bill Tür stands with Ukraine May 28 '15 at 09:48
  • for now its an VARCHAR tried INT and BIGINT aswell i tried int mycardUIDint = Convert.ToInt32(mycardUID, 16); right after string mycardUID = getcardUID(); – Carsten Schildknecht May 28 '15 at 10:14
  • and the "error message" i get while writting to database is he just writes "error" into that field - Without the Convert. An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll is the message displayed with Convert – Carsten Schildknecht May 28 '15 at 10:29
  • So there *is* something written into the database. In this case you'd better debug `getcardUID()` if it really does return the desired id or simply the string "error". – Bill Tür stands with Ukraine May 28 '15 at 13:46
  • it does return the desired id i wrote it for testing to a textblock it displays the id correctly... i can fill the field from the textblock with no issues :( but this shouldnt be the way to fix this – Carsten Schildknecht May 28 '15 at 13:48
  • It looks like you're assigning the value to the integer mycardUIDint, but then using the string mycardUID when you add the parameter. If uid is really an integer column, this will fail. Post the exception message that you're getting. – sarme Jun 03 '15 at 14:10

0 Answers0