1

How to save images and formated text with rich text box in SQL and show on crystal report VB.NET

I try below code to insert images in rich text box.

 Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click

    Dim ff As New OpenFileDialog
    ff.Filter = "Image Files|*.gif;*.jpg;*.png;*.bmp"
    ff.ShowDialog()

    Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(ff.FileName)
    Dim orgData = Clipboard.GetDataObject
    Clipboard.SetImage(img)
    Me.rtb.Paste()

    Clipboard.SetDataObject(orgData)
End Sub

And here i insert it into into SQL

Dim sname As Integer = Integer.Parse(txt1.Text)
    Dim sfname As Integer = 2
    Dim scnic As String = rtb.rtf
    query &= "INSERT INTO tencmpC1 (qnumber,topic,Umcq)"


    query &= "VALUES (@qnumber, @topic,@Umcq )"


    Using conn As New SqlConnection(strConn)
        Using comm As New SqlCommand()
            With comm
                .Connection = conn
                .CommandType = CommandType.Text
                .CommandText = query
                .Parameters.AddWithValue("@qnumber", sname)
                .Parameters.AddWithValue("@topic", sfname)
                .Parameters.AddWithValue("@Umcq", scnic)
         conn.Open()

                comm.ExecuteNonQuery()

                txt10.Text = "question saved "
            End With



        End Using

I am using nvarchar(max) as "datatype to SQL field" for RTB text. I am using textformate for crystal report "crRTFText". Please guide me what and where i have to do changes? Thanks in advance

sana rabia
  • 21
  • 1
  • 4
  • Can you take the RTF from the `RichTextBox` control as a string variable and send it directly to the crystal report control without going through the database and have it work? In other words. Is the problem some compatibility between the two controls or is the problem related to saving and reloading the RTF in the database? You need to narrow down your problem. – Steven Doggart Jul 29 '15 at 16:05
  • I take the RTF from the RichTextBox control as a RTF and send it indirectly to the crystal report control with going through the SQL. When i insert only formatted text its work fine. But when i try to insert images it not display that? As mentioned above i want to save and retrive both images + formated text? – sana rabia Jul 30 '15 at 11:08
  • You're not understanding my point. You need to isolate where your problem is. Is your problem that the Crystal Report control is not properly displaying the RTF, or is your problem that when you save the RTF to the DB and then read it back again, the value of the RTF string has changed? – Steven Doggart Jul 30 '15 at 11:11
  • Sorry but who can i insert my rtf data directly into crystal report with out saving it in SQL and establishing connection adding field into crystal report? – sana rabia Jul 30 '15 at 11:47
  • I've never used Crystal Reports before, so I'm not familiar with it, but I'd be shocked if there is no way to connect it to any other kind of data source besides a SQL Server Database. – Steven Doggart Jul 30 '15 at 11:49

1 Answers1

0

You can't store binary data (in this case the picture you're inserting to the RTB) in character fields. Depending on the SQL backend you're working with you'll need to store the data in a BLOB/CLOB column.

See here for an example.

Community
  • 1
  • 1
Eric Walker
  • 1,042
  • 10
  • 15
  • Respected sir this link not help full for me. I want to insert images as RTF is this possible or not with varchar or varbinary data type "store images as rtf is this possible"? – sana rabia Jul 30 '15 at 13:19
  • You can insert an image into an RTF and store the resulting data in a column for binary datatypes (varbinary/blob/clob etc., depending on your particular SQL engine) You don't want to separate the two though, it'll just add more work and potential for bugs. You just store the whole document in a column of binary datatype (preferrably a variable, rather than fixed length column unless you know exactly how large your biggest RTF will be beforehand) – Eric Walker Jul 30 '15 at 13:38