Click here to Skip to main content
15,074,181 members

Comments by Bjørn (Top 14 by date)

Bjørn 1-Aug-18 3:38am View
Hello ranio, I cannot recreate this behaviour. The Base64 representation of 16 bytes must always be 24 characters long (as it uses only 6 of 8 bits) and using your example code I always get exactly 24 characters for the Base64 encoded key which always decode back to exactly 16 bytes. Do you use different code than posted above? Or do you do anything else with Key before it is written into keyStr?
Bjørn 30-Jul-18 11:30am View
Hello ranio, what exactly is your question here? Are you asking why there is a difference in the length of the byte array and the Base64 string?
Bjørn 30-Jan-18 8:04am View
If I read correctly you are trying to use the built-in functionality of DataTable to save changes. You might need to write your custom DataSource to acomplish this kind of task.
Bjørn 22-Apr-15 9:00am View
When you add a click handler (the one automatically created when you double-click the Button in the designer) you can simply copy the "BackColor" property of the button:

private void button1_Click(object sender, EventArgs e)
Color c = button1.BackColor;
pictureBox1.Image = GetColoredBitmap(c, pictureBox1.Width, pictureBox1.Height);
pictureBox1.BackColor = c;

You should also set the "BackColor" property of the PictureBox like in the example above because getting the color of the Image is a bit more complicated.

If you're a bit more advanced, you can also assign the same click handler to all buttons like this:

private void ClickHandler(object sender, EventArgs e)
Color c = ((Control)sender).BackColor;
pictureBox1.Image = GetColoredBitmap(c, pictureBox1.Width, pictureBox1.Height);
pictureBox1.BackColor = c;

The sender is the Button which was clicked. I used a cast to a Control so not only Buttons but any Control types can be used.
Bjørn 22-Apr-15 7:35am View
If you just mean making a PictureBox shoing the color, you can easily set the same property "BackColor":

pictureBox1.BackColor = Color.Red;

If you really want to use a picture (as in using a Bitmap) you'll have to create one manually:

public Bitmap GetColoredBitmap(Color c, int width, int height)
Bitmap bmp = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(bmp))
return bmp;

Then you can set the "Image" property of the PictureBox with that Bitmap. Use the PictureBox's "Width" and "Height" properties to make the Bitmap the same size as the PictureBox:

pictureBox1.Image = GetColoredBitmap(Color.Red, pictureBox1.Width, pictureBox1.Height);
Bjørn 22-Apr-15 7:24am View
Hmm, I think it would be best to debug the code and monitor the parameters while they are collected to see what is being read from the source table. The bug must be somewhere around there. And also check if the parameter names which are automatically created in the code and those in the INSERT and UPDATE statements will match. For every value read from the SELECT statement the parameter name is automatically set:

SELECT variante,charge,fdate FROM tbl2 WHERE fid = {fid}
variante -> @val1
charge -> @val2
fdate -> @val3

SELECT fid,variante,charge,fdate FROM tbl2 WHERE fid = {fid}
fid -> @val1
variante -> @val2
charge -> @val3
fdate -> @val4
Bjørn 21-Apr-15 7:59am View
You can set the property "BackColor" of the Button like this:

button1.BackColor = Color.Red;

or when using the function above:

button1.BackColor = FromHexValue("FF0000");
Bjørn 20-Apr-15 4:45am View
So what exactly is the problem you're having? Converting HEX codes to a System.Drawing.Color? or creating the PictureBoxes or Buttons dynamically? What have you tried yet?
Bjørn 11-Apr-15 9:22am View
Hello again!

Why are you formatting the DateTimes to Strings in the SELECT queries in the first place? If there are DateTimes stored in the database, why not selecting them as DateTimes when they're only temporarily stored in a parameter and then passed back to the other database table? I'd try to replace the

DATE_FORMAT(fdate, '%Y-%M-%D %H:%i:%s')

with simply


in the cmd1.CommandText SELECT command creations for UPDATE and INSERT. You shouldn't change the ID, timestamp extraction, because that's not necessary and "Never touch a running system"!
Bjørn 10-Apr-15 6:24am View
Sorry, but I have no idea. The only thing you can try is allowing and handling NULL values:

while (reader.Read())
{ // I assume the fields are set to NOT NULL
A.Add(new IdTsEntry() {
Id = reader.IsDBNull(0) ? -1L : reader.GetInt64(0),
TimeStamp = reader.IsDBNull(1) ? "" : reader.GetString(1)

and the same for B. This is an inline if clause - if the field is NULL, take the preset value, else read the value.

Otherwise I don't know how to help any further.
Bjørn 9-Apr-15 11:42am View
That sounds very weird. I have one idea what that could cause: I assumed, as for you're working with a MySQL database, that your id is an int type. Is that true? Otherwise that will case a problem. And I see you're using DATE_FORMAT() in the queries below and I'm not familiar what type that function returns as it looks like it could be a string. Do you use that for the Timestamp in the first part too? The data types at this point are very important because I defined them in the code (the class and the reader.Get*() functions) and they must match the types in the databases. If you have to work with a string as timestamp you'll have to change these types. Like using a Guid for id and a string for timestmap:

class IdTsEntry
public Guid Id { get; set; }
public string TimeStamp { get; set; }


while (reader.Read())
{ // I assume the fields are set to NOT NULL
A.Add(new IdTsEntry() {
Id = reader.GetGuid(0),
TimeStamp = reader.GetString(1)

and so on. Another more abstract way to do it, is to use objects but in that case you'll have to make absolutely sure the exact same data types are returned from the two tables because otherwise, if you compare the two tables, no existing entires will be found and inserting them nevertheless will most propably cause an Exception.
Bjørn 9-Apr-15 9:43am View
Hello, have you checked, if your file is recognized as a MJPEG file? I've downloaded a sample MJPEG avi vom because I haven't worked with that yet and your command works fine with it. Perhaps you could try ffprobe.exe to see if ffmpeg can read it at all. You can use something like this:

ffprobe.exe -hide_banner -show_format -show_streams -pretty video_finale.mjpeg

For the sample file I get this information for the video stream:

codec_long_name=MJPEG (Motion JPEG)
Bjørn 9-Apr-15 9:19am View
Hello again, and sorry, I haven't seen your second post. The first thing that struck my eye is that there don't seem to be spaces between FROM, tbl2 and WHERE and that you are using @val7 although it must be @val3 the line below. Please take care you're creating a correct SQL command because the Exception does seem to point that way. There are a few more cases where there seem to be spaces missing when you use your variables. It seem you're getting

SELECT variante,charge,DATE_FORMAT(fdate, '%Y-%M-%D %H:%i:%s') FROMtbl2WHERE fid = {0}

where it should be

SELECT variante,charge,DATE_FORMAT(fdate, '%Y-%M-%D %H:%i:%s') FROM tbl2 WHERE fid = {0}

To avoid further errors, try a more simple approach: Instead of using this line

cmd1.CommandText = String.Format(ct1, id);

use that one:

cmd1.CommandText = "SELECT variante,charge,DATE_FORMAT(fdate, '%Y-%M-%D %H:%i:%s') FROM " + tbl2 + " WHERE fid = " + id.ToString();

and instead of using

cmd2.CommandText = String.Format(ct2, id);


cmd2.CommandText = "UPDATE " + tbl1 + " SET variante = @val1,charge = @val2, fdate = @val3 WHERE seriennummer = " + id.ToString();

That should do the same and you don't need ct1, ct2 and that String.Format() anymore. The same can be done in the second loop.
Bjørn 9-Apr-15 8:56am View
Hello, there must be something else going wrong because that line simply stores a string in a variable - there's no processing there. Posing the code might help and additionally the Exception that you get.

cmd1.CommandText = String.Format(ct1, id);

This line is meant to set the command text to a SELECT command that will give back the one data row for the current id. As I'm looping through the updated element's ids the command basically stays the same - only the WHERE clause has to be modified so I prepare the SELECT command with the placeholder {0} and the String.Format() is meant to replace it with the current id.

Hope that brings some light into that part of the code :D

Another issue that might arise is that I use DbCommand, DbDataReader and so far. I've only worked with MS SQL, SQLite and SqlCe so far and there this seems to work, but there might be some differences when working with MySQL?!