only last value is sent
I was wondering if anyone could tell me why it only appends the last row of data in my listbox to my xmlserializer to write it to my xml file....the infomation comes in the listBox but only the last row of data is written to the xml file.....why is that? and how would i go about fixing that...any help would be great!
empinfo info =new empinfo();
string connect = System.Configuration.ConfigurationSettings.AppSettings["connstring"];
SqlConnection sql =new SqlConnection(connect);
string cmd = "Select * From Employees";
SqlCommand scmd =new SqlCommand(cmd, sql);
sql.Open();
SqlDataReader dr = scmd.ExecuteReader();
if(dr.HasRows)
{
while(dr.Read())
{
//string lname = "";
//string fname = "";//string title = "";//string titof = "";//string et = "";//string info = "";string info1 = "";info.d_titof = dr.GetValue(4).ToString();
info.d_fname = dr.GetValue(2).ToString();
info.d_lname = dr.GetValue(1).ToString();
info.d_title = dr.GetValue(3).ToString();
info.d_ext = dr.GetValue(13).ToString();
info1 = info.d_titof + " " + info.d_fname + " " + info.d_lname + " " + info.d_title + " " + info.d_ext;
listBox1.Items.Add(info1).ToString();
try{
XmlSerializer xs =
new XmlSerializer(typeof(empinfo));Stream st =
new FileStream("c:\\vic1.xml", FileMode.Create, FileAccess.Write, FileShare.None);xs.Serialize(st, info);
st.Close();
}
catch(Exception es){
MessageBox.Show(es.ToString(), "Erorr");
}
}
sql.Close();
dr.Close();
scmd.Dispose();
In the code sample above, you are opening the output file (vic1.xml) in the the loop and you pass in the FileMode.Create, so each record ovewrites the previous data.
You should also consider to write a wrapper (root) element before serializing the row collection, beacuse if you have more than one row, you will endup with an invalid xml instance: it will have multiple root elements (rows).
Thanks,
Elena
I took it out the while loop and it still only appends the final row in the listBox.....I also changed the filemode to append instead of create....
empinfo info =
new empinfo();listBox1.SelectionMode = SelectionMode.MultiSimple;
string connect = System.Configuration.ConfigurationSettings.AppSettings["connstring"];SqlConnection sql =
new SqlConnection(connect);string cmd = "Select * From Employees";SqlCommand scmd =
new SqlCommand(cmd, sql);sql.Open();
SqlDataReader dr = scmd.ExecuteReader();
if(dr.HasRows){
while(dr.Read()){
string info1 = "";//string lname = "";//string fname = "";//string title = "";//string titof = "";//string et = "";//string info = "";info.d_titof = dr.GetValue(4).ToString();
info.d_fname = dr.GetValue(2).ToString();
info.d_lname = dr.GetValue(1).ToString();
info.d_title = dr.GetValue(3).ToString();
info.d_ext = dr.GetValue(13).ToString();
info1 += info.d_titof + " " + info.d_fname + " " + info.d_lname + " " + info.d_title + " " + info.d_ext;
listBox1.Items.Add(info1).ToString();
MessageBox.Show(info1);
}
}
sql.Close();
scmd.Dispose();
try{
XmlSerializer xs =
new XmlSerializer(typeof(empinfo));Stream st =
new FileStream("c:\\vic1.xml", FileMode.Append, FileAccess.Write, FileShare.None);xs.Serialize(st, info);
st.Close();
}
catch(Exception es){
MessageBox.Show(es.ToString(), "Erorr");
}
}