Naidu AMMAN

Naidu AMMAN

  • NA
  • 37
  • 35.3k

how to save Textfile by using thi code

Sep 30 2013 8:22 AM
Hi friends, please tell me that how to save text file by using this code in c# windows form application.And my  Data base is mysql and
In select query i taken to tables by using left joins .please solve this problem for me ,thank u. 


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using DataAccessLayer;
using MySql.Data.MySqlClient;
using System.Text.RegularExpressions;


namespace prjtims
{
    public partial class frmtotremits : Form
    {
        private StreamWriter filewriter;

        public frmtotremits()
        {
            InitializeComponent();
            // btnok.Enabled = false;
        }

        private void btnok_Click(object sender, EventArgs e)
        {

            string str_output;
            IDBManager dbManager = new DBManager(DataProvider.MySql);
            dbManager.ConnectionString = "Server=localhost;Database=aeonrtc;Uid=root;Pwd=root";
            try
            {
                DataSet etm_ticket_data;
                dbManager.Open();
                etm_ticket_data = dbManager.ExecuteDataSet(CommandType.Text, "select distinct count(*),etm_ticket_data.etd_waybill_no, sum(etm_ticket_data.etd_adultfare),sum(etm_ticket_data.etd_childfare),etm_ticket_data.etd_service_code,etm_ticket_data.etd_ticket_type1,etm_ticket_data.etd_ticket_type2,etm_ticket_data.etd_vehicleno,etm_ticket_data.etd_trip_kms, etm_ticket_data.etd_trip_no,etm_ticket_data.etd_tripcollamount,etm_ticket_data.etd_conductorno,etm_ticket_data.etd_conductorname,etm_issue_details.eid_ebtm_no,  etm_issue_details.eid_issue_date, etm_issue_details.eid_driver_name,etm_issue_details.eid_driver_pfno,etm_issue_details.eid_depot_code, etm_issue_details.eid_driver2_pfno from etm_ticket_data left join etm_issue_details on etm_ticket_data.etd_waybill_no=etm_issue_details.eid_waybill_no where etm_issue_details.eid_issue_date between '01-08-2013 00:42:09' and '01-08-2013 13:38:32' and etm_ticket_data.etd_service_code group by etm_ticket_data.etd_ticket_type1,etm_ticket_data.etd_service_code order by etm_ticket_data.etd_service_code;");

                str_output = "WAYBILL No" + etm_ticket_data.Tables[0].Rows[0][13];
                str_output = "IN DATE" + etm_ticket_data.Tables[0].Rows[0][14];
                str_output = "SERVICE CODE  " + etm_ticket_data.Tables[0].Rows[0][4];
                str_output = "VEHICLE No" + etm_ticket_data.Tables[0].Rows[0][7];
                str_output = "OPR KMS" + etm_ticket_data.Tables[0].Rows[0][8];
                str_output = "TRIPS OPR" + etm_ticket_data.Tables[0].Rows[0][9];
                str_output = "CONDUCTOR No" + etm_ticket_data.Tables[0].Rows[0][11];
                str_output = "CONDUCTOR NAME" + etm_ticket_data.Tables[0].Rows[0][12];
                str_output = "DRIVER No" + etm_ticket_data.Tables[0].Rows[0][16];
                str_output = "DRIVER NAME" + etm_ticket_data.Tables[0].Rows[0][15];
                str_output = "DRIVER 2 NO" + etm_ticket_data.Tables[0].Rows[0][18];
                str_output = "TIM No" + etm_ticket_data.Tables[0].Rows[0][13];
                str_output = "DEPOT TIM No" + etm_ticket_data.Tables[0].Rows[0][17];

                string file_name = "C:\\test1.txt";
                System.IO.StreamReader objReader;
                objReader = new System.IO.StreamReader(file_name);
                cmb_srvc_no.Text = objReader.ReadToEnd();
                objReader.Close();
                           

                DialogResult result;
                string fileName;
                using (SaveFileDialog fileChooser = new SaveFileDialog())
                {
                    fileChooser.CheckFileExists = false;
                    result = fileChooser.ShowDialog();
                    fileName = fileChooser.FileName;
                }
                if (result == DialogResult.OK)
                {
                    if (fileName == string.Empty)
                    {
                        MessageBox.Show("Invalid File Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {

                        FileStream flstrm = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
                 
                        dbManager.Close();
                    }
                }
            }





            catch (Exception ex)
            {
                //Usual Code
            }
            finally
            {
                dbManager.Dispose();

                MessageBox.Show("Successfully saved");
            }
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmtotremits_Load(object sender, EventArgs e)
        {
            int i = 0;

            string str_output;
            IDBManager dbManager = new DBManager(DataProvider.MySql);
            dbManager.ConnectionString = "Server=localhost;Database=aeonrtc;Uid=root;Pwd=root";

            DataSet etm_ticket_data;
            dbManager.Open();

            etm_ticket_data = dbManager.ExecuteDataSet(CommandType.Text, "select distinct etm_ticket_data.etd_service_code from etm_ticket_data left join etm_issue_details on etm_ticket_data.etd_waybill_no=etm_issue_details.eid_waybill_no where etm_issue_details.eid_issue_date between '01-08-2013 00:42:09' and '01-08-2013 13:38:32' order by etm_ticket_data.etd_service_code;");

            for (i = 0; i < etm_ticket_data.Tables[0].Rows.Count; i++)
            {
                cmb_srvc_no.Items.Add(etm_ticket_data.Tables[0].Rows[i][0]);
            }
            dbManager.Close();
            dbManager.Dispose();

            if (cmb_srvc_no.Items.Count > 0)
                cmb_srvc_no.SelectedIndex = 0;


        }

        private void lblDT_Validating(object sender, CancelEventArgs e)
        {
            Regex reg = new Regex(@"^(\d{1,2})/(\d{1,2})/(\d{4})$");
            Match m = reg.Match(dtpkr_issue_date.Text);
            String.Format("{0:d/M/yyyy HH:mm:ss}", lblDT); // "9/3/2008 16:05:07"
            if (m.Success)
            {
                int dd = int.Parse(m.Groups[1].Value);
                int mm = int.Parse(m.Groups[2].Value);
                int yyyy = int.Parse(m.Groups[3].Value);
                e.Cancel = dd < 1 || dd > 31 || mm < 1 || mm > 12 || yyyy > 2011;
            }
            else e.Cancel = true;
            if (e.Cancel)
            {
                if (MessageBox.Show("Wrong date format. The correct format is dd/mm/yyyy\n+ dd should be between 1 and 31.\n+ mm should be between 1 and 12.\n+ yyyy should be before 2013", "Invalid date", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.Cancel)
                    e.Cancel = false;

            }
        }
    }
}