{"id":120,"date":"2010-12-18T11:52:00","date_gmt":"2010-12-18T17:52:00","guid":{"rendered":"https:\/\/www.chrystalsander.com\/blog\/?p=120"},"modified":"2010-12-18T11:52:00","modified_gmt":"2010-12-18T17:52:00","slug":"save-dataset-to-comma-delimited-file","status":"publish","type":"post","link":"https:\/\/www.chrystalsander.com\/blog\/?p=120","title":{"rendered":"Save Dataset To Comma-Delimited File"},"content":{"rendered":"<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; color: green; font-family: 'Courier New';\">              \/\/save dataset to file<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; color: green; font-family: 'Courier New';\">                         <\/span>      <\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">        <\/span><span style=\"color: blue;\">                  private<\/span> <span style=\"color: blue;\">void<\/span> saveFile()<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">        <\/span>{<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">            <\/span><span style=\"color: blue;\">try<\/span><\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">            <\/span>{<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                         <\/span>saveFileDialog1.Filter = <span style=\"color: maroon;\">&#8220;Text File|*.csv&#8221;<\/span>;<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                         <\/span>DialogResult dr = saveFileDialog1.ShowDialog();<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                         <\/span><span style=\"color: blue;\">if<\/span> (dr.Equals(DialogResult.OK))<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                         <\/span>{<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                                 <\/span>Stream saveStream = saveFileDialog1.OpenFile();<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                                 <\/span>StreamWriter saveWriter = <span style=\"color: blue;\">new<\/span>              StreamWriter(saveStream);<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                                 <\/span><span style=\"color: blue;\">string<\/span> delim = <span style=\"color: maroon;\">                  &#8220;&#8221;<\/span>;<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\">                         <\/span>      <\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                                 <\/span><span style=\"color: blue;\">foreach<\/span> (DataColumn c <span style=\"color: blue;\">in<\/span> usmDataSet_LocateEst.qLocateEst.Columns)<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                                 <\/span>{<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                                     <\/span>saveWriter.Write(delim);<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                                     <\/span>saveWriter.Write(c.ColumnName);<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                                     <\/span>delim = <span style=\"color: maroon;\">&#8220;,&#8221;<\/span>;<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                                 <\/span>}<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                                 <\/span>saveWriter.WriteLine();<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\">                         <\/span>      <\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                                 <\/span><span style=\"color: blue;\">foreach<\/span> (DataRow r <span style=\"color: blue;\">                  in<\/span> usmDataSet_LocateEst.qLocateEst.Rows)<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                                 <\/span>{<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                                     <\/span>delim = <span style=\"color: maroon;\">&#8220;&#8221;<\/span>;<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\">                         <\/span>      <\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                                     <\/span><span style=\"color: blue;\">foreach<\/span> (DataColumn              c <span style=\"color: blue;\">in<\/span> usmDataSet_LocateEst.qLocateEst.Columns)<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                                     <\/span>{<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                                         <\/span>saveWriter.Write(delim);<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                                         <\/span>saveWriter.Write(r[c]);<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                                         <\/span>delim = <span style=\"color: maroon;\">                  &#8220;,&#8221;<\/span>;<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                                     <\/span>}<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                                     <\/span>saveWriter.WriteLine();<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">          <\/span><span style=\"\">                            <\/span>}<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                                 <\/span>saveWriter.Close();<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                                 <\/span>MessageBox.Show(<span style=\"color: maroon;\">&#8220;File Saved!&#8221;<\/span>,              <span style=\"color: maroon;\">&#8220;Save Results&#8221;<\/span>, MessageBoxButtons.OK, MessageBoxIcon.Information);<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">                         <\/span>}<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">            <\/span>}<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">            <\/span><span style=\"color: blue;\">catch<\/span> (Exception ex)<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">            <\/span>{<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">       <\/span><span style=\"\">                           <\/span>MessageBox.Show(<span style=\"color: maroon;\">&#8220;Error                      Saving File: &#8220;<\/span> + ex.Message, <span style=\"color: maroon;\">&#8220;Error!&#8221;<\/span>,              MessageBoxButtons.OK, MessageBoxIcon.Error);<\/span><\/p>\n<p style=\"margin: 0in 0in 0pt;\">          <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">            <\/span>}<\/span><\/p>\n<p>                <span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">        <\/span>}<\/span><span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">       <\/span><\/span><span style=\"font-size: 10pt; font-family: 'Courier New';\"><span style=\"\">       <\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/\/save dataset to file private void saveFile() { try { saveFileDialog1.Filter = &#8220;Text File|*.csv&#8221;; DialogResult dr = saveFileDialog1.ShowDialog(); if (dr.Equals(DialogResult.OK)) { Stream saveStream = saveFileDialog1.OpenFile(); StreamWriter saveWriter = new StreamWriter(saveStream); string delim = &#8220;&#8221;; foreach (DataColumn c in usmDataSet_LocateEst.qLocateEst.Columns) { saveWriter.Write(delim); saveWriter.Write(c.ColumnName); delim = &#8220;,&#8221;; } saveWriter.WriteLine(); foreach (DataRow r in usmDataSet_LocateEst.qLocateEst.Rows) { delim = &hellip; <a href=\"https:\/\/www.chrystalsander.com\/blog\/?p=120\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Save Dataset To Comma-Delimited File&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/120"}],"collection":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=120"}],"version-history":[{"count":0,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/120\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}