0

I am using the following query:

string query = @"SELECT r.id, user_name, user_phone, date_create, REPLACE( date_payment,  '0000-00-00 00:00:00',  'Не оплачено' ) as 
                date_payment, payment_method, amount, rs.name_ru
                FROM request AS r, request_status AS rs
                WHERE r.status = rs.id";

And i am binding datatemplate in the following way:

DataTemplate>
    <TextBlock VerticalAlignment="Center" Text="{Binding date_payment}" Width="135" />
</DataTemplate>

Its throwing correct output except the "date_payment" Its output value comes "System.Byte[]". please help!!!

Thank from MBen

class ByteArrayToString : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                var listOfBytes = value as Byte[];
                string output = "";
                output = System.Text.Encoding.UTF8.GetString(listOfBytes);
                return output;
            }

            return "";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return DependencyProperty.UnsetValue;
        }
    }
1
  • What is date_payment ? seems like Date and why does it need to be byte[] ? Commented Jul 16, 2012 at 9:01

2 Answers 2

2

date_payment is an Array, and you didn't provide any way for WPF for displaying it, so it calls ToString . You can provide a data converter for it.

Add a resource to your Window or page :

   <Window.Resources>
        <local:ByteArrayToString x:Key="ByteArrayConverter" />
    </Window.Resources>

Use it in your TextBlock as such :

<TextBlock VerticalAlignment="Center" Text="{Binding date_payement, Converter={StaticResource ByteArrayConverter}}" Width="135" />

Now you need to add a new class that does the conversion :

    class ByteArrayToString : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                var listOfBytes = value as Byte[];
                string output ="";
                output = listOfBytes.Aggregate(output, (current, elemt) => current + elemt.ToString());
                return output;
            }

            return "";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return DependencyProperty.UnsetValue;
        }
    }
Sign up to request clarification or add additional context in comments.

5 Comments

two seconds :). writing it up
local is just namepsace where I defined the ValueConverter : xmlns:local="clr-namespace:WpfApplication5". If you notice I named the class ByteArrayToString in the C# code I pasted
i get in column integer, so "28347563728282"... pleae help :))))))))
you need to change the converter the display the Bytes as you want. The one I gave you just concatenates everything. Change this line output = listOfBytes.Aggregate(output, (current, elemt) => current + elemt.ToString()); to format it the way you want.
i change output = System.Text.Encoding.UTF8.GetString(listOfBytes); and she works!!!!!! thank so very very very much!!!
0

This thing happened to me to. It seems this is due to a bug in the connector. Try to cast the date column to CHAR. It worked for me.

Comments

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.