Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
how i create txt to store up to 100 entries and continue in a new txt, I use c #
 
this is my code:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using IntranetLima.Entities;
using IntranetLima.Business;
using System.Drawing;
using System.Collections;
 
namespace IntranetLima.WebApp.Compras
{
    public partial class ConsulRuc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack && !Page.IsCallback)
            {
                Populate_Company();
            }  
        
        }
 
        private void Populate_Company()
        {
            ListItem item;
            item = new ListItem("<--Seleccione-->", "-1");
 
            CompanyController ctrl = new CompanyController();
 
            DataTable arr = ctrl.CompanySelect();
 
            ddlEmpresa.DataSource = arr;
            ddlEmpresa.DataTextField = "COMPANY";
            ddlEmpresa.DataValueField = "COMPANY";
 
            ddlEmpresa.DataBind();
 
            ddlEmpresa.Items.Insert(0, item);
        }
 
        protected string get_company()
        {
            string company = ddlEmpresa.SelectedValue;
            return company;
        }
 
        protected void btnConsulta_Click(object sender, EventArgs e)
        {
 
            ConsulRucController ctrl = new ConsulRucController();
 
            string compania = get_company();
 
            Select_Ruc();
 

 
        }
 
        private void Select_Ruc()
        {
            string strFilePath = null;
            StreamWriter stm = default(StreamWriter);
            int i=0;
            DataTable dt = new DataTable();
            ListItem item;
            item = new ListItem(txtAnio.Text);
 
            ConsulRucController ctrl = new ConsulRucController();
 
            DataTable arr = ctrl.ConsultaRuc(ddlEmpresa.SelectedValue, txtAnio.Text, ddlMes.SelectedValue);
            // Creando el archivo txt consulta de RUC
            strFilePath = "c://IPPRUC_FEB_2012_01txt";
            stm = new StreamWriter(strFilePath, false);
 
            for (i = 0; i <= dt.Columns.Count - 2; i++)
            {
                stm.Write(dt.Columns[i].ColumnName + ",");                      
            
            }
 
            stm.Write(dt.Columns[i].ColumnName);
            stm.WriteLine();
 
            foreach (DataRow row in dt.Rows)
            {
                object[] array = row.ItemArray;
 
                for (i = 0; i <= array.Length - 2; i++)
                {
                    stm.Write(array[i].ToString() + ",");
          
                }
            
                stm.Write(array[i].ToString());  
                            
            }
 
            stm.Close();
            //WebMsgBox("job done :D");

 
        } 
        //catch(Exception ex){

           
        //    }

 

            //jaime
            //txt_estado_actual.Text = Convert.ToString(arr.Rows[0][1]);
            
            
            
            //txt_empresa.Text = Convert.ToString(arr.Rows[0][3]);

            //txt_estado_actual.DataBind();

 
        }
    
    
    
    }
 

 
this is my new code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using IntranetLima.Entities;
using IntranetLima.Business;
using System.Drawing;
using System.Collections;
 
namespace IntranetLima.WebApp.Compras
{
    public partial class ConsulRuc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack && !Page.IsCallback)
            {
                Populate_Company();
            }  
        
        }
 
        private void Populate_Company()
        {
            ListItem item;
            item = new ListItem("<--Seleccione-->", "-1");
 
            CompanyController ctrl = new CompanyController();
 
            DataTable arr = ctrl.CompanySelect();
 
            ddlEmpresa.DataSource = arr;
            ddlEmpresa.DataTextField = "COMPANY";
            ddlEmpresa.DataValueField = "COMPANY";
 
            ddlEmpresa.DataBind();
 
            ddlEmpresa.Items.Insert(0, item);
        }
 
        protected string get_company()
        {
            string company = ddlEmpresa.SelectedValue;
            return company;
        }
 
        protected void btnConsulta_Click(object sender, EventArgs e)
        {
 
            ConsulRucController ctrl = new ConsulRucController();
 
            string compania = get_company();
 
            Select_Ruc();
            
 
        }
 
        private void Select_Ruc()
        {
            string strFilePath = null;
            StreamWriter stm = default(StreamWriter);
            int i=0;
            //DataTable dt = new DataTable();
            ListItem item;
            item = new ListItem(txtAnio.Text);
 
            ConsulRucController ctrl = new ConsulRucController();
 
            DataTable arr = ctrl.ConsultaRuc(ddlEmpresa.SelectedValue, txtAnio.Text, ddlMes.SelectedValue);
            // Creando el archivo txt consulta de RUC
            //strFilePath = "c://IPPRUC_FEB_2012_01.txt";
            //strFilePath = ("c://IPPRUC_" & ddlMes.SelectedValue.ToString & txtAnio.Text & ".txt");
            
 
            for (i = 0; i <= arr.Columns.Count - 2; i++)
            {
                 // este stm.Write(arr.Columns[i].ColumnName + ",");
                //stm.WriteLine(arr.Columns[i].ColumnName + ",");    
            
            }
 
              //este stm.Write(arr.Columns[i].ColumnName);
            //stm.WriteLine(arr.Columns[i].ColumnName);
            //stm.WriteLine();
            int temp = 1;
            //int cont = 0;
            int cont = -1;
            
            strFilePath = "c://IPPRUC_" + ddlMes.SelectedValue + "_" + txtAnio.Text + "_" + temp + ".txt";
            stm = new StreamWriter(strFilePath, false);
            foreach (DataRow row in arr.Rows)
            {
                cont++;
                if(cont % 100==0){
                    stm.Close();
                    txtAnio.Text = txtAnio.Text + "-" + cont.ToString();
                    temp++;
                    strFilePath = "c://IPPRUC_" + ddlMes.SelectedValue + "_" + txtAnio.Text + "_" + temp + ".txt";
                    stm = new StreamWriter(strFilePath, false);
                }
                
                object[] array = row.ItemArray;
 
                //for (i = 0; i <= array.Length - 2; i++)
                for (i = 1; i <= array.Length - 2; i++)
                {
                    stm.WriteLine(array[i].ToString() + ",");
          
                }
 
                stm.WriteLine(array[i].ToString());
 
          
                
                {
                
                
                
                }
 

                
            }
 

 

 
            
            //WebMsgBox("job done :D");

            //WebMsgBox.Show("Se creo el txt");
        } 
        //catch(Exception ex){

        //    }

            //jaime
            //txt_estado_actual.Text = Convert.ToString(arr.Rows[0][1]);
                        
            //txt_empresa.Text = Convert.ToString(arr.Rows[0][3]);

            //txt_estado_actual.DataBind();
        }    
    }
the first txt is in black and the last txt too. The others ones are ok (100 registers)
Posted 27-Nov-12 11:14am
Edited 29-Nov-12 10:08am
v6
Comments
Sergey Alexandrovich Kryukov at 27-Nov-12 17:33pm
   
You got my permission. Next! --SA
CPallini at 27-Nov-12 18:06pm
   
5++.
Sergey Alexandrovich Kryukov at 27-Nov-12 19:01pm
   
Thank you, Carlo. --SA
Marcus Kramer at 27-Nov-12 17:34pm
   
Simple. Maintain a counter of entries you have made. When you hit 100, move on to the next txt and start counting over again.
Nelek at 27-Nov-12 17:58pm
   
This is not how it works. You are supposed to do your tasks. If you have an error that you can not solve, you are welcome to ask that concrete issue and you will get an answer. Please read: Homework[^] what have you tried?[^]
HabeshaMan at 29-Nov-12 15:51pm
   
is the first txt black or blank?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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;
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            string sFile_dir = "C:\\CP";
            int FileIndex = 1;
            int RowCount = 1;
            for (int i = 1; i < 10000; i++)
            {
                if(RowCount % 100 == 0 ) { ++FileIndex; }
                writeToFile(sFile_dir, "File" + FileIndex, "File Content-" + i);
            RowCount ++; 
            }
        }
 

        public static void writeToFile(string sFile_dir, string sFileName, string sContent)
        {
            string FILE_NAME = sFile_dir + "\\" + sFileName + ".csv";
            if (!Directory.Exists(sFile_dir))
            {
                Directory.CreateDirectory(sFile_dir);
            }
            if (!File.Exists(FILE_NAME))
            {
                // Create a file to write to.
                using (StreamWriter sw = File.CreateText(FILE_NAME))
                {
                    if (!File.Exists(FILE_NAME))
                    {
                        return;
                    }
                }
            }
            using (StreamWriter sw = File.AppendText(FILE_NAME))
            {
                sw.WriteLine(sContent);
                sw.Flush();
                sw.Close();
            }
        }//End  writeToFile()
    }
}
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 397
1 OriginalGriff 350
2 thatraja 205
3 Emre Ataseven 165
4 Shai Vashdi 150
0 Sergey Alexandrovich Kryukov 8,894
1 OriginalGriff 5,200
2 Peter Leow 3,955
3 Maciej Los 3,535
4 Abhinav S 3,208


Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 29 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid