leontas
New member
Good Evening to all from Athens,Greece
I'm having a small issue with my program (using VS2008 and programming in vb.net)
My purpose is to save all contents (text+format+images) to my SQL DB field (VARBINARY (MAX) )
Below is my code for the save button:
Now if I want to update it, it works fine, but if I try to save a new record then nothing get saved (0 records updated)
Is there something missing?
Thank you in advance for your time
I'm having a small issue with my program (using VS2008 and programming in vb.net)
My purpose is to save all contents (text+format+images) to my SQL DB field (VARBINARY (MAX) )
Below is my code for the save button:
VB.NET:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] btnSave_Click([/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] sender [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] System.Object, [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] e [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] System.EventArgs) [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] btnSave.Click[/COLOR]
[/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] stream [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] FileStream = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] cn [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SqlConnection = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] cmd [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SqlCommand = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]FCNentryRichTextBox.SaveFile([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"temp.rtf"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])
stream = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] FileStream([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"temp.rtf"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], FileMode.Open, FileAccess.Read)
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] size [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = Convert.ToInt32(stream.Length)
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] rtf [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [Byte]() = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [Byte](size - 1) {}
stream.Read(rtf, 0, size)
cn = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SqlConnection(cnstring)
cn.Open()
cmd = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SqlCommand([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"UPDATE tblFCN SET FCNEntry=@FCNEntry WHERE FCNid= '"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] & FCNidTextBox.Text & [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"'"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], cn)
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] paramRTF [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SqlParameter([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"@FCNEntry"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], SqlDbType.Image, rtf.Length, ParameterDirection.Input, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], 0, _
0, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], DataRowVersion.Current, rtf)
cmd.Parameters.Add(paramRTF)
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] rowsUpdated [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = Convert.ToInt32(cmd.ExecuteNonQuery())
MessageBox.Show([String].Format([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"{0} rows updated"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], rowsUpdated))
[/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] stream [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]IsNot[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]stream.Close()
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] cmd [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]IsNot[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]cmd.Parameters.Clear()
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] cn [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]IsNot[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]cn.Close()
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][/COLOR][/SIZE]
Now if I want to update it, it works fine, but if I try to save a new record then nothing get saved (0 records updated)
Is there something missing?
Thank you in advance for your time
