I'm trying to develop a notes app that allows users to store pictures into a database, and it works only if the image is uploaded once, but once the value of the image field is changed from one image to another, or from null to an image, I can no longer convert the image successfully upon logging out of the application. The byte[] to Image conversion works during the current state of the application (before logging out), but once I log out, the only images that successfully work are the ones that are only uploaded initially. This leads me to believe that something is wrong with my save note function if opened.
SqlConnection sqlcon = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;
AttachDbFilename=C:\Users\salil\Desktop\Coding Stuf\Notes-App\NotesAppCsharp\loginInfo.mdf;
Integrated Security=True;Connect Timeout=30");
string query;
byte[] convert = null;
if (curImage != null)
{
convert = ConvertImageToBinary(curImage);
}
if (this.openedNote == true)
{
table.Rows.RemoveAt(openedIndex);
using (sqlcon)
{
sqlcon.Open();
query = "UPDATE [dbo].[Table1] SET NoteName = '" + noteName + "', NoteContent = '" + noteContent +
"', ImageName = '" + convert + "' WHERE noteId = '" + noteIndex + "'";
using (SqlCommand command = new SqlCommand(query, sqlcon))
{
command.ExecuteNonQuery();
this.openedNote = false;
sqlcon.Close();
}
}
}
else
{
using (sqlcon)
{
sqlcon.Open();
if (curImage == null)
{
query = "INSERT INTO [dbo].[Table1] (userId, NoteName, NoteContent) VALUES (@userId, @noteName, @content)";
} else
{
query = "INSERT INTO [dbo].[Table1] (userId, NoteName, NoteContent, ImageName) VALUES (@userId, @noteName, @content, @image)";
}
using (SqlCommand command = new SqlCommand(query, sqlcon))
{
command.Parameters.AddWithValue("@userId", this.userId);
command.Parameters.AddWithValue("@noteName", noteName);
command.Parameters.AddWithValue("@content", noteContent);
if (curImage != null)
{
command.Parameters.AddWithValue("@image", convert);
}
command.ExecuteNonQuery();
sqlcon.Close();
}
}
}
SqlConnection sqlcon1 = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;
AttachDbFilename=C:\Users\salil\Desktop\Coding Stuf\Notes-App\NotesAppCsharp\loginInfo.mdf;
Integrated Security=True;Connect Timeout=30");
sqlcon1.Open();
string query1 = "Select noteId from [dbo].[Table1] Where NoteName = '" + noteName + "' AND NoteContent = '" + noteContent + "'";
SqlDataAdapter sda = new SqlDataAdapter(query1, sqlcon1);
DataTable temp = new DataTable();
sda.Fill(temp);
sqlcon1.Close();
int lastRow = temp.Rows.Count;
int noteId = (int) temp.Rows[lastRow - 1][0];
table.Rows.Add(this.userId, noteName, noteContent, convert, noteId);
This is the button open function:
private void buttonOpen_Click(object sender, EventArgs e)
{
int index = noteNames.CurrentCell.RowIndex;
if (index > -1)
{
string noteName = table.Rows[index].ItemArray[1].ToString();
string noteContent = table.Rows[index].ItemArray[2].ToString();
Image noteImage;
if (table.Rows[index][3] == DBNull.Value)
{
labelUploaded.Text = "No";
noteImage = null;
}
else
{
noteImage = ConvertBinaryToImage((byte []) table.Rows[index][3]);
labelUploaded.Text = "Yes";
}
textBoxNoteName.Text = noteName;
textBoxNoteContent.Text = noteContent;
this.openedNote = true;
this.openedIndex = index;
this.noteIndex = (int) table.Rows[index][4];
this.curImage = noteImage;
}
}
These are the image to byte[] and byte[] to image conversion methods:
byte [] ConvertImageToBinary(Image img)
{
using (MemoryStream m = new MemoryStream())
{
img.Save(m, img.RawFormat);
byte[] data = m.ToArray();
Convert.ToBase64String(data);
return data;
}
}
Image ConvertBinaryToImage(byte [] data)
{
string base64string = Convert.ToBase64String(data);
Image convert = Image.FromStream(new MemoryStream(Convert.FromBase64String(base64string)));
return convert;
}
And lastly, this is the upload image function:
private void uploadImageButton_Click(object sender, EventArgs e)
{
string fileName;
using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "JPEG|*.jpg|*.png|PNG", ValidateNames = true, Multiselect = false})
{
if(ofd.ShowDialog() == DialogResult.OK)
{
fileName = ofd.FileName;
Image image = Image.FromFile(fileName);
curImage = image;
ImageViewer newViewer = new ImageViewer(image);
newViewer.Show();
}
labelUploaded.Text = "Yes";
}
}
This is the image I would be trying to upload
The initial value in the SQL database which allows for successful reads after the user logs out
Thanks in advance to whoever can help me out!