0

I am looking for a solution to retrieve the hex I am storing in my VersionNum column of my database which is of type (timestamp, not null). I am using a SQL Server database.

VersionNum (timestamp, not null)
0x0000000000007DA1

My model class:

public class Message : BoBase
{
        public int MessageId { get; set; }

        public string MessageText { get; set; }
        public int OrderBy { get; set; }

        public DateTime BeginDate { get; set; }
        public DateTime EndingDate { get; set; }

        public bool IsModal { get; set; }
        
        public byte[] VersionNum { get; set; }
}

My DataObject method GetMessages:

List<Message> messages = new List<Message>();

using (SqlConnection conn = new SqlConnection(connectionString))
{
    using (SqlCommand cmd = conn.CreateCommand())
    {
        conn.Open();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "uspMessages_GetList";

        using (SqlDataReader dr = cmd.ExecuteReader())
        {
            while (dr.Read())
            {
                messages.Add(new Message
                        {
                            MessageId = Convert.ToInt32(dr["MessageId"].ToString()),
                            MessageText = dr["Message"].ToString(),
                            OrderBy = Convert.ToInt32(dr["orderBy"].ToString()),
                            IsModal = Convert.ToBoolean(dr["IsModal"]),
                            BeginDate = Convert.ToDateTime(dr["BeginDate"]),
                            EndingDate = Convert.ToDateTime(dr["EndingDate"]),
                            VersionNum = (byte[])dr["VersionNum"]
                        });
            }
        }
    }
}

return messages;

When this query returns with data, it is only returning '{byte[8]}' for the VersionNum column rather than the hex (0x0000000000007DA1) stored in it.

I am wondering if I am not assigning it in my DataReader correctly or there is a method I am missing to get the value of the timestamp type column correctly?

5
  • 3
    Hex is a display thing, byte array is the storage value. If you need to display the byte array as Hex, there's a bunch of code on stackoverflow that does. Or perhaps I misunderstand the issue Commented Sep 21, 2024 at 18:01
  • Have you tried reading the SqlDataReader documentation? There's a GetBytes method that would seem appropriate. Commented Sep 22, 2024 at 8:55
  • @siggemannen that is exactly what I am trying to accomplish, but, I can't see to find any working examples on stack of this.. could you be so kind as to point me to one? I may not be properly searching the issue. Commented Oct 1, 2024 at 13:13
  • stackoverflow.com/questions/311165/… perhaps? But can you explain why you want to display this? Most users don't do binary or hex Commented Oct 1, 2024 at 13:15
  • I am using it for modal message notifications version numbers. When my user clicks an OK button on the modal I am storing the message ID and versionNumber in a UserReadMessages table. In the instance that a message has previously been shown, but an edit has been made to the message, I need that version number to compare to my UseraReadMessages table and display the updated edited version. Commented Oct 1, 2024 at 13:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.