Answer the question
In order to leave comments, you need to log in
How to handle an exception?
Hello!
There is an application and a server, both written in C#. The form has a picturebox and a button, when clicked, an sql-query with SELECT is generated, this query is sent to the server, then the server pulls the picture from the database as a byte array, sends this byte array back to the application, and after the application receives this byte array , you need to display this array of bytes into a picture in the picturebox.
In this piece of code, a request is sent to the server to take a picture:
stream = Client.GetStream();
string Message3 = $"SELECT Image FROM users WHERE Login = '{LoginField.Text}'";
byte[] command3 = Encoding.UTF8.GetBytes(Message3);
stream.Write(command3, 0, command3.Length);
byte[] cmd = new byte[64];
StringBuilder builder = new StringBuilder();
int bytes = 0;
do
{
bytes = stream.Read(cmd, 0, cmd.Length);
builder.Append(Encoding.UTF8.GetString(cmd, 0, bytes));
}
while (stream.DataAvailable);
string message = builder.ToString();
Console.WriteLine(message);
stream.Flush();
if (message.Contains("SELECT"))
{
if (message.Contains("Image"))
{
SqliteConnection connect = new SqliteConnection("Data source = accounts.db");
connect.Open();
SqliteCommand command = new SqliteCommand(message, connect);
command.ExecuteNonQuery();
SqliteDataReader Reader = command.ExecuteReader();
while (Reader.Read())
{
if ((string)Reader["Image"] == "")
{
string foto = null;
byte[] image = Encoding.UTF8.GetBytes(foto);
Console.WriteLine(image);
stream.Write(image, 0, image.Length);
}
else
{
string foto = (string)Reader["Image"];
byte[] image = Convert.FromBase64String(foto);
Console.WriteLine(image);
stream.Write(image, 0, image.Length);
}
}
}
byte[] GetData3 = new byte[128];
MemoryStream memoryStream = new MemoryStream(GetData3);
pictureBox1.Image = Image.FromStream(memoryStream);
pictureBox1.Image = Image.FromStream(memoryStream);
System.Argument.Expection: Invalid parameter.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question