Reading contents.

Rat

Well-known member
Joined
Aug 2, 2005
Messages
72
Location
Somewhere between 0 and 1
Programming Experience
1-3
Reading contents. [ RESOLVED ]

Okay, here's my problem ... i'll try to explain it as clearly as I possibly can.

hopefully, the attached screenshot will make it easier to understand.

I want the FileName corresponding to the ID selected by the user through the ComboBox, to be opened in a RichTextBox ... So, I was thinking if it was possible for me to capture the FileName corresponding to the ID selected by the user into a string, then it would be much easier for me ... what do you guys think ?

Or is there a way where i can directly read instead of using a buffer to store the filename from the "FileName" column ?

Thanks.
 

Attachments

  • Screen.jpg
    Screen.jpg
    47.1 KB · Views: 116
Last edited:
I meant within your app. Do you retrieve the data into a DataTable and then bind the DataGrid to that DataTable, or do you use a DataReader and transfer the values to the DataGrid directly, or something else?
 
jmcilhinney said:
I meant within your app. Do you retrieve the data into a DataTable and then bind the DataGrid to that DataTable, or do you use a DataReader and transfer the values to the DataGrid directly, or something else?

Here's the part that does the binding for the DataGrid and the ComboBox

VB.NET:
[size=2][color=#0000ff]Dim[/color][/size][size=2] connString [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String[/color][/size][size=2] = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\DB\DB.mdb"

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] myConnection [/size][size=2][color=#0000ff]As[/color][/size][size=2] OleDbConnection = [/size][size=2][color=#0000ff]New[/color][/size][size=2] OleDbConnection

myConnection.ConnectionString = connString

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] DataAdapter [/size][size=2][color=#0000ff]As[/color][/size][size=2] OleDbDataAdapter = [/size][size=2][color=#0000ff]New[/color][/size][size=2] OleDbDataAdapter("Select * from XML", myConnection)

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] DataAdapterID [/size][size=2][color=#0000ff]As[/color][/size][size=2] OleDbDataAdapter = [/size][size=2][color=#0000ff]New[/color][/size][size=2] OleDbDataAdapter("Select ID From XML", myConnection)[/size][size=2]

DataAdapter.Fill(DataSet, "XML")

DataAdapter.FillSchema(DataSet.Tables("XML"), SchemaType.Source)

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] DataView [/size][size=2][color=#0000ff]As[/color][/size][size=2] DataView = DataSet.Tables("XML").DefaultView

dgBrowse.DataSource = DataView

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] DataViewID [/size][size=2][color=#0000ff]As[/color][/size][size=2] DataView = DataSet.Tables("XML").DefaultView

cboID.DataSource = DataViewID

cboID.DisplayMember = "ID"
[/size]
 
Hi Rat,
Hope this helps...

in the code which u have shown try puting this line too...after :
cboID.DisplayMember = "ID"

CboID.ValueMember = "FileName"

then in the selectedindexchanged event of the cboID include this :
textbox.text=cboID.selectedvalue

Cheers


 
GKG said:
Hi Rat,
Hope this helps...

in the code which u have shown try puting this line too...after :
cboID.DisplayMember = "ID"

CboID.ValueMember = "FileName"

then in the selectedindexchanged event of the cboID include this :
textbox.text=cboID.selectedvalue

Cheers



textbox.text -> is for ?
 
Hi,
You have mentioned - "I want the FileName corresponding to the ID selected by the user through the ComboBox, to be opened in a RichTextBox ... "

so I wondered if u want to display the selected Filename in the "RichTextBox" then u can display it by using :

textbox.text=cboID.selectedvalue

"textbox" is the name of your "richTextbox".

GKG
 
GKG's method of getting the file name is the best option, but I'm guessing that you actually want to open the file and display its contents in the RichTextBox. To do that by extending GKG's code you would do something like this:
VB.NET:
'Open the selected file.
Dim myStreamReader As New IO.StreamReader(cboID.SelectedValue)

'Read the file directly into the RTB.
myRichTextBox.Text = myStreamReader.ReadToEnd()

'Close the file.
myStreamReader.Close()
Edit:
This assumes that the file name retrieved is the full path.
 
jmcilhinney said:
GKG's method of getting the file name is the best option, but I'm guessing that you actually want to open the file and display its contents in the RichTextBox. To do that by extending GKG's code you would do something like this:
VB.NET:
'Open the selected file.
Dim myStreamReader As New IO.StreamReader(cboID.SelectedValue)

'Read the file directly into the RTB.
myRichTextBox.Text = myStreamReader.ReadToEnd()

'Close the file.
myStreamReader.Close()
Edit:
This assumes that the file name retrieved is the full path.

I get a error declaring myStreamReader ...

VB.NET:
C:\Documents and Settings\030522H\Desktop\FYP\XMLCapture\XMLDBExplorer.vb(157): Overload resolution failed because no accessible 'New' can be called without a narrowing conversion:
	'Public Sub New(path As String)': Argument matching parameter 'path' narrows from 'System.Object' to 'String'.
	'Public Sub New(stream As System.IO.Stream)': Argument matching parameter 'stream' narrows from 'System.Object' to 'System.IO.Stream'.
 
Sorry, the SelectedValue property of the ComboBox is of type Object, so you need to cast it as a String:
VB.NET:
Dim myStreamReader As New IO.StreamReader(CStr(cboID.SelectedValue))
 
The finished code, in the event that someone else needs it.

VB.NET:
Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] btnOpen_Click([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] btnOpen.Click
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] Open [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]New[/color][/size][size=2] frmXMLOpen

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] StreamReader [/size][size=2][color=#0000ff]As[/color][/size][size=2] StreamReader

[/size][size=2][color=#0000ff]Try

[/color][/size][size=2]StreamReader = [/size][size=2][color=#0000ff]New[/color][/size][size=2] StreamReader([/size][size=2][color=#0000ff]CStr[/color][/size][size=2](cboID.SelectedValue))

Open.rtbBuffer.Visible = [/size][size=2][color=#0000ff]True

[/color][/size][size=2]Open.rtbBuffer.Dock = DockStyle.Fill

Open.rtbBuffer.Text = StreamReader.ReadToEnd()

Open.ShowDialog()

[/size][size=2][color=#0000ff]Catch[/color][/size][size=2] ex [/size][size=2][color=#0000ff]As[/color][/size][size=2] Exception

MsgBox(ex.Message.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OKOnly, "Exception Message")

[/size][size=2][color=#0000ff]Finally

[/color][/size][size=2][/size][size=2][color=#0000ff]If[/color][/size][size=2] [/size][size=2][color=#0000ff]Not[/color][/size][size=2] (StreamReader [/size][size=2][color=#0000ff]Is[/color][/size][size=2] [/size][size=2][color=#0000ff]Nothing[/color][/size][size=2]) [/size][size=2][color=#0000ff]Then

[/color][/size][size=2]StreamReader.Close()

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Try

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size]
[size=2][color=#0000ff][/color][/size] 
[size=2][color=#0000ff]
 
Also good to know is that the RichTextBox has a built in method to load a file. It's called LoadFile and in it's simplest overload only requires the path to the file although a second overload will take a RichTextBoxStreamType as a second parameter.
VB.NET:
Open.rtbBuffer.LoadFile(CStr(cboID.SelectedValue))
'OR
Open.rtbBuffer.LoadFile(CStr(cboID.SelectedValue), _
  System.Windows.Forms.RichTextBoxStreamType.UnicodePlainText)
Also of note is the RichTextBox's SaveFile method.
 
Back
Top