-
Oct 14th, 2017, 10:33 AM
#1
Thread Starter
Addicted Member
-
Oct 14th, 2017, 08:51 PM
#2
Re: Selected row in datagridview to other form (including picture in one cell)?
You can pass data between forms in multiple ways, jmc explains one of my favorite ways here http://jmcilhinney.blogspot.com.au/2...orms-part.html
If you can't find a way to pass the picture then I wouldn't use two forms, I would add a panel, with the necessary controls to display the data, to the form. Keep the panel hidden until you want to use it.
-
Oct 14th, 2017, 09:28 PM
#3
Re: Selected row in datagridview to other form (including picture in one cell)?
You might follow the CodeBank link in my signature below and check out my thread on Editing A Grid Row In A Dialogue. In short, you can pass the DataRow bound to your grid row to the dialogue and it pulls the data out of each field and displays them in its controls. If the user clicks the Cancel button then the form simply closes and you're done. If the user clicks OK then the dialogue pushes the data from its controls back into the DataRow and then closes. Mpte that the dialogue has exactly ZERO interaction with the database. It is the responsibility of the calling form, which retrieved the data in the first place, to save any changes back to the database. It knows to do that when the ShowDialog call on the dialogue returns OK.
As for the image, it is stored in the DataRow as a Byte array. To display it in a PictureBox, you'll need to create an Image object from that Byte array. If you want to be able to replace it, you'll need to create a Byte array from the new Image. To learn how to do that, check out my CodeBank thread on Saving Images In Databases.
-
Oct 15th, 2017, 11:18 AM
#4
Thread Starter
Addicted Member
Re: Selected row in datagridview to other form (including picture in one cell)?
Perfect, I'll look into it and then if I have problems getting it to work I'll get back to you.
Thanks so far.
-
Jan 1st, 2018, 11:41 AM
#5
Thread Starter
Addicted Member
Re: Selected row in datagridview to other form (including picture in one cell)?
Hi again,
I got most of it to work but when trying to save an update from the second form I get this error. Can't figure it out. :-(
This is the code for the save updatebutton:
Code:
Private Sub ButtonSaveUpdate_Click(sender As Object, e As EventArgs) Handles ButtonSaveUpdate.Click
MysqlConnUp = New MySqlConnection
MysqlConnUp.ConnectionString = "server=My.server;user=my-username;password=*************;database=MyDatabase"
Dim mstream As New System.IO.MemoryStream
PictureBoxBillede.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim arrImage() As Byte = mstream.GetBuffer()
mstream.Close()
If CheckBoxFornyelse.CheckState = CheckState.Checked Then
forny = "Ja"
Else
forny = "Nej"
End If
If CheckBoxNyProeve.CheckState = CheckState.Checked Then
nyProeve = "Ja"
Else
nyProeve = "Nej"
End If
Dim Sql As String
Sql = "UPDATE HVS.CERTIFIKAT SET fornavn='" & TextBoxFornavn.Text & "',efternavn='" & TextBoxEfternavn.Text & "',adresse='" & TextBoxAdresse.Text & "',postnummer='" & MaskedTextBoxPostnr.Text & "',`by`='" & TextBoxBy.Text & "',tlf='" & TextBoxTlf.Text & "',mobil='" & TextBoxMob.Text & "',email='" & TextBoxEmail.Text & "',CPR='" & MaskedTextBoxCpr.Text & "',certifikat='" & ComboBoxCertifikat.Text & "',proevedato='" & DateTimePickerUdstedelse.Text & "',udloebsdato='" & TextBoxUdloeb.Text & "',kraever_fornyelse='" & forny & "',kraever_ny_proeve='" & nyProeve & "', Billede=@Billede" & "WHERE Id LIKE '" & MaskedTextBoxId.Text & "'"
Try
With COMMANDUPDATE
.Connection = MysqlConnUp
.CommandText = Sql
.Parameters.AddWithValue("@Billede", arrImage)
End With
COMMANDUPDATE.ExecuteNonQuery()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
MsgBox(ex.ToString)
Finally
MysqlConnUp.Dispose()
End Try
Listview.Show()
Me.Close()
End Sub
Last edited by lovschal; Jan 1st, 2018 at 01:42 PM.
-
Jan 1st, 2018, 06:17 PM
#6
Re: Selected row in datagridview to other form (including picture in one cell)?
If you can't figure that one out then you can't really have done much research on the subject. A NullReferenceException means that you are trying to access a member of an object that doesn't exist. As there's only one reference on the line that generates the exception, the source is obvious.
Also, building a SQL statement that way, i.e. with string concatenation, is a very, VERY bad idea. You are doing the right thing and using a parameter in one case but, it appears, only because you have to. You should be using a parameter for every one of those values. You clearly know how to use parameters so I'm not going to explain how it's done.
-
Jan 1st, 2018, 06:57 PM
#7
Thread Starter
Addicted Member
Re: Selected row in datagridview to other form (including picture in one cell)?
Yes, just changed it to parameters instead. It’s also easier to find my way around the gode that way.
Found the fault aften a while, I forgot New in the generation of the COMMANDUPDATE. Added that and it worked. Shouldn’t be coding in the middel of the night. ;-)
Thanks anyway for the help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|