Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is My Design:

https://www4.0zz0.com/2020/05/13/03/372544645.jpg[^]

and this is my database design:

https://www10.0zz0.com/2020/05/13/03/171174839.jpg[^]

with CmbInfoType selected index changed event
the text string from the data reader turns into code
which open file from resources into richtextbox

What I have tried:

tried this code but gives error:
(Failed to Compile Script)

Imports System.CodeDom.Compiler
Imports System.Data.OleDb
Imports System.Reflection
Public Class Form1

   Dim cnn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Informations.accdb")
   Dim da As OleDbDataAdapter
   Dim dr As OleDbDataReader

   Public Function GenerateScript(code As String) As IScript
       Using provider As New VBCodeProvider()
           Dim parameters As New CompilerParameters()
           parameters.GenerateInMemory = True
           parameters.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location)
           parameters.ReferencedAssemblies.Add("System.Data.dll")
           parameters.ReferencedAssemblies.Add("System.Xml.dll")
           Dim interfaceNamespace As String = GetType(IScript).Namespace
           Dim codeArray() As String = New String() {"Imports " & interfaceNamespace & Environment.NewLine & code}
           Dim results As CompilerResults = provider.CompileAssemblyFromSource(parameters, codeArray)
           If results.Errors.HasErrors Then
               Throw New Exception("Failed to compile script")
           Else
               Return CType(results.CompiledAssembly.CreateInstance("Script"), IScript)
           End If
       End Using
   End Function

   Public Interface IScript
       Function DoWork() As String
   End Interface

   Private Sub CmbInfoCategory_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CmbInfoCategory.SelectedIndexChanged

       CmbInfoType.Items.Clear()

       If CmbInfoCategory.SelectedItem = ("Diaries") Then

           Dim dt1 As New DataTable
           dt1.Clear()
           Dim sql As String = "SELECT * FROM Diaries"
           da = New OleDbDataAdapter(sql, cnn)
           da.Fill(dt1)
           For ii As Integer = 0 To dt1.Rows.Count - 1
               CmbInfoType.Items.Add(dt1(ii)(0))
           Next

       End If

   End Sub

   Private Sub CmbInfoType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CmbInfoType.SelectedIndexChanged

       Using cnn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Informations.accdb")
           cnn.Open()

           Try
               Dim sql As String = "SELECT * From Diaries WHERE DiaryName= @myname"
               Dim cmd As New OleDbCommand(sql, cnn)
               cmd.Parameters.AddWithValue("@myname", CmbInfoType.SelectedItem)
               dr = cmd.ExecuteReader
               If dr.Read = False Then
                   MsgBox("No Patient is Registered Under This Number")
               Else

                   Dim script As IScript = GenerateScript(dr.Item(1).ToString())
                   script.DoWork()

               End If
           Catch ex As Exception
               MsgBox(ex.Message)
           End Try

           cnn.Close()
       End Using

   End Sub
End Class
Posted
Updated 14-May-20 19:01pm

1 solution

You're doing it wrong!

If you want to load text from file in assemblies and display it in richtextbox depending on selection in combobox, you have to "translate" text into assembly resource.
How? Follow the instruction: .net - How to read embedded resource text file - Stack Overflow[^]

BTW: i'd suggest to update FileLink field to the file name only, because you know where the text have to be displayed (RichTextBox).
 
Share this answer
 
Comments
Doctor GME 15-May-20 10:58am    
tried this method:

Private Sub CmbInfoType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CmbInfoType.SelectedIndexChanged

Using cnn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Informations.accdb")
cnn.Open()

Try
Dim sql As String = "SELECT * From Diaries WHERE DiaryName= @myname"
Dim cmd As New OleDbCommand(sql, cnn)
cmd.Parameters.AddWithValue("@myname", CmbInfoType.SelectedItem)
dr = cmd.ExecuteReader
If dr.Read = False Then
MsgBox("No Patient is Registered Under This Number")
Else

Dim assmbly As Assembly = Assembly.GetExecutingAssembly()
Dim reader As New StreamReader(assmbly.GetManifestResourceStream(dr.Item(1).ToString))
RichTextBox1.Rtf = reader.ReadToEnd()

End If
Catch ex As Exception
MsgBox(ex.Message)
End Try

cnn.Close()
End Using

End Sub

but gives error:
value cannot be null parameters name: stream

also changed the file link field as you suggested
any ideas!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900