Reputation: 658
To avoid confusion I translated my code to be more english speakers friendly. the File
is actually Ficheiro
which does NOT exist. So I'm dealing with my own class and not File
from System.IO
.
I have a bindingsource
set from a dataset. Controls like textbox
and combobox
works fine if I edit and insert new values. But I have a column that is a varbinary
(to store small pdf files) so I needed a binding control.
My idea was creating a Class called File and inherited from Control class to use databinding
because also TextBox
control uses the same to set databinding
to bindingsource
.
Problem here is when I call dialog function (see below) I have a System.StackOverflowException
on File class, content set.
private void button1_Click(object sender, EventArgs e)
{
f.dialog();
}
Class file here:
class File : Control
{
private byte[] _content
public byte[] Content{
get{
return _content;
}
set{
_content=value;
}
}
//public string name; //not used for now
public string ext;
public string type;
public string fileName;
public File(){
}
public byte[] getBytes()
{
return Content;
}
public void dialog(){
OpenFileDialog fdg = new OpenFileDialog();
fdg.Filter = "PDF Files | *.pdf";
if (fdg.ShowDialog() == DialogResult.OK)
{
if (fdg.fileName.Length > 0)
{
fileName = fdg.FileName;
ext = ".pdf";
type = "pdf";
using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
using (var reader = new BinaryReader(stream))
{
content = reader.ReadBytes((int)stream.Length);
stream.Close();
}
}
}
}
}
}
The way I bind my control to bindingsource:
private void Upload_Load(object sender, EventArgs e)
{
f = new File();
f.DataBindings.Add(new System.Windows.Forms.Binding("Content", this.fBindingSource, "FileContent", false));
// TODO: This line of code loads data into the 'pEGPIDataSet_ficheiro_ds.Ficheiro' table. You can move, or remove it, as needed.
this.fTableAdapter.Fill(this.myautodatasetfromfile.Ficheiro);\
}
EDIT: Now I have another error.
private void fBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.fBindingSource.EndEdit(); //Exception here says that does not allow nulls on the content file Column.
this.tableAdapterManager.UpdateAll(this.myautodatasetfromfile.Ficheiro);
}
I added my control and it has contents set. Why I this Exception?
Upvotes: 0
Views: 254
Reputation: 552
I'm very confusing about your content
property.
First of all I would try to use local member:
private byte[] _content
public byte[] Content{
get{
return _content;
}
set{ //StackOverflowEx here
_content=value;
}
}
Please read here: property setter body
Upvotes: 1