openFileDialog problem

  • Thread starter Thread starter Cory
  • Start date Start date
C

Cory

I have a very strange problem with my C# code. I trying to
add a file to my dataset. I use a openFileDialog object to
get my file. I put the file pathname in a textbox. Then i
click my upload button which adds my file to the dataset
and then writes the dataset to a XML file(ds.writeXML
("test.xml");).I know the file is added to the dataset but
it doesn't get written to the XML file. The strange thing
is if i type the file pathname in the textbox it works
perfect.Any help would be appreciated.

Thanks in advance

Cory
 
Cory,

Can you show some of your code? Assuming that the dialog will update the textbox correctly, there should be no difference. Are you sure that the contents of the textbox are the same either way?
 
This is the code in question

//browse button
private void cmdBrowse_Click(object sender, System.EventArgs e)
{
if(openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
txtFileName.Text=openFileDialog1.FileName;
}
}


//upload button
private void button1_Click(object sender, System.EventArgs e)
{
String fileName = txtFileName.Text;

if(fileName.Equals(""))
{
MessageBox.Show("File Name cannot be blank");
txtFileName.Select();
txtFileName.Focus();
}
else
{

//Get the fileData
string fileData="";
FileInfo fi= new FileInfo(fileName);
FileStream fs=fi.OpenRead();
int nBytes=(int)fi.Length;
byte[] ByteArray=new byte[nBytes];
int nBytesRead=fs.Read(ByteArray, 0, nBytes);
fs.Close();
fileData=Convert.ToBase64String(ByteArray);


CSupFile mySupFile=new CSupFile();
mySupFile.setAuditID(auditId);
mySupFile.setAuditDate(auditdate);
mySupFile.setQuestionID(questionId);
mySupFile.setName(fileName);
mySupFile.setType("b");
mySupFile.setSupData(fileData);
if(mySupFile.saveFile(data)==true)
{
this.Close();
this.Dispose();
}
else
{
MessageBox.Show("Supporting document could not be saved. Please try
again.");
}
}
}

//adding the file to the dataset
public Boolean saveFile(CData data)
{
Boolean saveSuccess=false;

try
{
DataView vueAudits = new
DataView(data.dsFrom.Tables["audit_response_controls"],"","" ,
DataViewRowState.CurrentRows);

DataRowView rowAudits=vueAudits[0];

DataView vueAudit =
rowAudits.CreateChildView("audit_response_controls_audit_response_contro
l");
vueAudit.RowFilter="audit_id = " + auditID + " and audit_date='" +
auditDate + "'";

DataRowView rowAudit=vueAudit[0];
DataView
vueAuditResponses=rowAudit.CreateChildView("audit_response_control_audit
_responses");

if(vueAuditResponses.Count>0)
{
DataRowView rowAuditResponses=vueAuditResponses[0];
DataView
vueAuditResponse=rowAuditResponses.CreateChildView("audit_responses_audi
t_response");
vueAuditResponse.RowFilter="question_id=" + questionID;

if(vueAuditResponse.Count>0)
{
DataRowView rowQuestion=vueAuditResponse[0];
DataView
vueQuestion=rowQuestion.CreateChildView("audit_response_sup_docs");

DataRowView rowSupFiles;

if(vueQuestion.Count==0)
{
rowSupFiles=vueQuestion.AddNew();
rowSupFiles.EndEdit();
}
else
{
rowSupFiles=vueQuestion[0];
}


DataView vueSupFile=rowSupFiles.CreateChildView("sup_docs_sup_doc");
DataRowView rowSupFile;

rowSupFile=vueSupFile.AddNew();
rowSupFile.EndEdit();

DataView vueFile=rowSupFile.CreateChildView("sup_doc_file");
DataRowView rowFile;
rowFile=vueFile.AddNew();
rowFile["name"]=name;
rowFile["type"]=type;
rowFile["data"]=supdata;
rowFile.EndEdit();


data.saveFromPocketPC();

saveSuccess=true;
}
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
return saveSuccess;
}

//writing to xml file
public void saveFromPocketPC()
{
dsFrom.WriteXml("fromPocketPC.xml");
}
 
Back
Top