Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 4th, 2007, 09:07 PM
Authorized User
 
Join Date: Dec 2006
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to include a new line after every row --Help

This is the code to write a text file form the database.It writtes the data correctly.But,i need a new line after every row.so where shud i include \n in this code.....I am new to the coding....somebody help me.....


public void TextWritter()
        {
            SqlConnection con = null;
            try
            {
                con = new SqlConnection("server=localhost; uid='sa';pwd='buildfolio1819'; database=pubs;");
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("Select * from authors", con);
                DataSet ds=new DataSet();
                da.Fill(ds);
                StringBuilder str = new StringBuilder();
                for(int i=0;i<=ds.Tables[0].Rows.Count - 1; i++)
                for(int j=0;j<=ds.Tables[0].Columns.Count - 1; j++)
// if(j==0)

                str.Append(ds.Tables[0].Rows[i][j].ToString() + "\t");
                //Response.Write(str.ToString());
                StreamWriter writer = new StreamWriter("c:\\qazwsx.txt");
                writer.WriteLine(str.ToString());
                writer.Close();

            }
            catch(Exception ex)
            {
                ex = ex;
            }
            finally
            {
                if(null!=con)
                    if(con.State == System.Data.ConnectionState.Open)
                        con.Close();
            }

        }
__________________
Karthik
[Nothing is impossible]
 
Old January 5th, 2007, 10:43 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

One thing I'd recommend right up front is that you always include { and } around branching logic. It makes it far easier to read and troubleshoot. Also, you can use the StringBuilder.AppendFormat method for cleaner (and more efficient) string concatenation (particularly when using the string builder.

Try this:

for(int i=0;i<=ds.Tables[0].Rows.Count - 1; i++)
{
   for(int j=0;j<=ds.Tables[0].Columns.Count - 1; j++)
   {
      str.AppendFormat("{0}\t", ds.Tables[0].Rows[i][j]);
   }
   str.Append("\n");
}


-Peter
 
Old January 7th, 2007, 09:05 PM
Authorized User
 
Join Date: Dec 2006
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot peter ..........u r idea worked out....

Karthik
[Nothing is impossible]





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to draw indicator line in owc line chart AlexOo General .NET 0 July 9th, 2007 10:32 PM
difference between include file & include virtual crmpicco Classic ASP Basics 2 January 23rd, 2006 11:50 AM
xsl:include within include chuck123ie XSLT 1 January 5th, 2006 11:07 AM
Line/Row Position jmss66 Classic ASP Databases 7 July 28th, 2003 07:56 PM
ODBC ERROR IN ARCHIVE /include/header.asp, line 11 jonnyl Forum and Wrox.com Feedback 1 June 11th, 2003 09:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.