Writing selected items from Select to file

liunx

Guest
Hi I am currently working with the StreamWriter to write a file with a string of selected fields in a file.
However I am having a hard time getting the values from my selects from my codebehind.

I am trying this:


....definition of streamwriter etc.
string query = "";
try
{
for (i=0; i<FieldBox.Items.Count; i++)
query = FieldBox.Items.Value.ToString();
}
.....exception handling + writing fileThe details of your problem are too vague for me to give you a definite answer, however I did notice something about your code. The string "Query" is only going to have the last listitem value of "Fieldbox" because you are setting it "=" and so each iterations replaces the value of the last. If your idea was to append each listitem value to the end of "Query" then you want to use concatenation. I know if VB it would be like this "&=" rather than "=", I imagine C# is like "+=" rather than "=".

Does that help?Sorry for the late reply.

I am having trouble getting anything at all from the select to be written to a file.
The try fails and my exception kicks in.
I am working with codebehind - could this have something to do with the viewstate that Ive heard a bit about?

the code is like this in my aspx.cs file:

private void SaveButton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
FileStream fs = File.Create(Server.MapPath("test.txt"));
StreamWriter sw = new StreamWriter(fs);
string query = "";
try
{
for (i=0; i<FieldBox.Items.Count; i++)
query = FieldBox.Items.Value.ToString();
}
catch (Exception ex)
{
query = "Query was empty";
}
sw.Write(query);
sw.Close();
fs.close();
}

The thing I get every time is a file with the text "Query was empty" - which means that filewriting works but the exception kicks in.
 
Back
Top