String Alignment problem

R

Rahul

Friends,

My Problem is related to writing a text file from a database
using .Net. in a particular string format.
I have a dataset with a table, let suppose in this table there are
three fields.
- Code
- Description
- Amount

At the time of writing a text file I use in a for loop -

String.Format("{0,10}{1, 40} {2,-10}", DS.Table(0).Row(i)
("Code").ToString(), DS.Table(0).Row(i)("Description").ToString(),
DS.Table(0).Row(i)("Amount").ToString())


But the problem is related to the alignment. Data comes like in
following format:

01Code 01Desc 2000
02Code 02Desc 3000
03CodeABC 03Desc 4000
04xyz 04Desc 5000


I want Data comes in following proper alignment format.

01Code 01Desc 2000
02Code 02Desc 3000
03CodeABC 03Desc 4000
04xyz 04Desc 5000


So anybody knows the solutions of this problem, please and please
reply imediately, its urgent.



Rahul
 
G

GeezerButler

Friends,

My Problem is related to writing a text file from a database
using .Net. in a particular string format.
I have a dataset with a table, let suppose in this table there are
three fields.
- Code
- Description
- Amount

At the time of writing a text file I use in a for loop -

String.Format("{0,10}{1, 40} {2,-10}", DS.Table(0).Row(i)
("Code").ToString(), DS.Table(0).Row(i)("Description").ToString(),
DS.Table(0).Row(i)("Amount").ToString())

But the problem is related to the alignment. Data comes like in
following format:

01Code 01Desc 2000
02Code 02Desc 3000
03CodeABC 03Desc 4000
04xyz 04Desc 5000

I want Data comes in following proper alignment format.

01Code 01Desc 2000
02Code 02Desc 3000
03CodeABC 03Desc 4000
04xyz 04Desc 5000

So anybody knows the solutions of this problem, please and please
reply imediately, its urgent.

Rahul

For the formatting option that you have your output will be like:
01Code 01Desc2000
and not
01Code 01Desc 2000

Also, some of the strings may not be getting formatted properly if
length of any string in your dataset exceeds the width that you
specify in the format option.
Can you show me with your real data?
 
R

Rahul

For the formatting option that you have your output will be like:
01Code 01Desc2000
and not
01Code 01Desc 2000

Also, some of the strings may not be getting formatted properly if
length of any string in your dataset exceeds the width that you
specify in the format option.
Can you show me with your real data?- Hide quoted text -

- Show quoted text -

thanks for reply
I have put '-' on the place of space for better understanding.
This comes for Northwind database, product table
the real data is :

alice mutton----------------------------alice mutton

aniseed syrup---------------------------aniseed syrup

boston crab meat------------------------boston crab meat

camembert pierrot-----------------------camembert pierrot

carnarvon tigers------------------------carnarvon tigers

chai------------------------------------chai

chang-----------------------------------chang

chartreuse verte------------------------chartreuse verte

chef anton's cajun seasoning------------chef anton's cajun seasoning

chef anton's gumbo mix------------------chef anton's gumbo mix

chocolade-------------------------------chocolade

côte de blaye---------------------------côte de blaye

escargots de bourgogne------------------escargots de bourgogne

filo mix--------------------------------filo mix

flotemysost-----------------------------flotemysost

geitost---------------------------------geitost

genen shouyu----------------------------genen shouyu

gnocchi di nonna alice------------------gnocchi di nonna alice

gorgonzola telino-----------------------gorgonzola telino

grandma's boysenberry spread------------grandma's boysenberry spread

gravad lax------------------------------gravad lax

guaraná fantástica----------------------guaraná fantástica

gudbrandsdalsost------------------------gudbrandsdalsost

gula malacca----------------------------gula malacca

gumbär gummibärchen---------------------gumbär gummibärchen

gustaf's knäckebröd---------------------gustaf's knäckebröd

ikura-----------------------------------ikura

inlagd sill-----------------------------inlagd sill

ipoh coffee-----------------------------ipoh coffee

jack's new england clam chowder---------jack's new england clam
chowder

konbu-----------------------------------konbu

lakkalikööri----------------------------lakkalikööri

laughing lumberjack lager---------------laughing lumberjack lager

longlife tofu---------------------------longlife tofu

louisiana fiery hot pepper sauce--------louisiana fiery hot pepper
sauce

louisiana hot spiced okra---------------louisiana hot spiced okra

manjimup dried apples-------------------manjimup dried apples

mascarpone fabioli----------------------mascarpone fabioli

maxilaku--------------------------------maxilaku

mishi kobe niku-------------------------mishi kobe niku

mozzarella di giovanni------------------mozzarella di giovanni

nord-ost matjeshering-------------------nord-ost matjeshering

northwoods cranberry sauce--------------northwoods cranberry sauce

nunuca nuß-nougat-creme-----------------nunuca nuß-nougat-creme

original frankfurter grüne soße---------original frankfurter grüne
soße

outback lager---------------------------outback lager

pâté chinois----------------------------pâté chinois

pavlova---------------------------------pavlova

perth pasties---------------------------perth pasties

queso cabrales--------------------------queso cabrales

queso manchego la pastora---------------queso manchego la pastora

raclette courdavault--------------------raclette courdavault

ravioli angelo--------------------------ravioli angelo

rhönbräu klosterbier--------------------rhönbräu klosterbier

röd kaviar------------------------------röd kaviar

rogede sild-----------------------------rogede sild

rössle sauerkraut-----------------------rössle sauerkraut

sasquatch ale---------------------------sasquatch ale

schoggi schokolade----------------------schoggi schokolade

scottish longbreads---------------------scottish longbreads

singaporean hokkien fried mee-----------singaporean hokkien fried mee

sir rodney's marmalade------------------sir rodney's marmalade

sir rodney's scones---------------------sir rodney's scones

sirop d'érable--------------------------sirop d'érable

spegesild-------------------------------spegesild

steeleye stout--------------------------steeleye stout

tarte au sucre--------------------------tarte au sucre

teatime chocolate biscuits--------------teatime chocolate biscuits

thüringer rostbratwurst-----------------thüringer rostbratwurst

tofu------------------------------------tofu

tourtière-------------------------------tourtière

tunnbröd--------------------------------tunnbröd

uncle bob's organic dried pears---------uncle bob's organic dried
pears

valkoinen suklaa------------------------valkoinen suklaa

vegie-spread----------------------------vegie-spread

wimmers gute semmelknödel---------------wimmers gute semmelknödel

zaanse koeken---------------------------zaanse koeken


Rahul
 
G

Guest

thanks for reply
I have put '-' on the place of space for better understanding.
This comes for Northwind database, product table

I think it's because of the size of the strings.

When you have

String.Format("{0,10}....

it does mean the length of the Code-field cannot be more than 10
characters, in other case it would shift the whole line.

Try to change

String.Format("{0,10}{1, 40} {2,-10}",
Left(DS.Table(0).Row(i)("Code").ToString(), 10),
Left(DS.Table(0).Row(i)("Description").ToString(), 40),
DS.Table(0).Row(i)("Amount").ToString())

I think it should help
 
G

Guest

You should organize the output into a table with 3 columns. The easiest way
is to use GridView control databound to your dataset.

He is writing a text file.
 
G

GeezerButler

thanks for reply
I have put '-' on the place of space for better understanding.
This comes for Northwind database, product table
the real data is :

alice mutton----------------------------alice mutton

aniseed syrup---------------------------aniseed syrup

boston crab meat------------------------boston crab meat

camembert pierrot-----------------------camembert pierrot

carnarvon tigers------------------------carnarvon tigers

chai------------------------------------chai

chang-----------------------------------chang

chartreuse verte------------------------chartreuse verte

chef anton's cajun seasoning------------chef anton's cajun seasoning

chef anton's gumbo mix------------------chef anton's gumbo mix

chocolade-------------------------------chocolade

côte de blaye---------------------------côte de blaye

escargots de bourgogne------------------escargots de bourgogne

filo mix--------------------------------filo mix

flotemysost-----------------------------flotemysost

geitost---------------------------------geitost

genen shouyu----------------------------genen shouyu

gnocchi di nonna alice------------------gnocchi di nonna alice

gorgonzola telino-----------------------gorgonzola telino

grandma's boysenberry spread------------grandma's boysenberry spread

gravad lax------------------------------gravad lax

guaraná fantástica----------------------guaraná fantástica

gudbrandsdalsost------------------------gudbrandsdalsost

gula malacca----------------------------gula malacca

gumbär gummibärchen---------------------gumbär gummibärchen

gustaf's knäckebröd---------------------gustaf's knäckebröd

ikura-----------------------------------ikura

inlagd sill-----------------------------inlagd sill

ipoh coffee-----------------------------ipoh coffee

jack's new england clam chowder---------jack's new england clam
chowder

konbu-----------------------------------konbu

lakkalikööri----------------------------lakkalikööri

laughing lumberjack lager---------------laughing lumberjack lager

longlife tofu---------------------------longlife tofu

louisiana fiery hot pepper sauce--------louisiana fiery hot pepper
sauce

louisiana hot spiced okra---------------louisiana hot spiced okra

manjimup dried apples-------------------manjimup dried apples

mascarpone fabioli----------------------mascarpone fabioli

maxilaku--------------------------------maxilaku

mishi kobe niku-------------------------mishi kobe niku

mozzarella di giovanni------------------mozzarella di giovanni

nord-ost matjeshering-------------------nord-ost matjeshering

northwoods cranberry sauce--------------northwoods cranberry sauce

nunuca nuß-nougat-creme-----------------nunuca nuß-nougat-creme

original frankfurter grüne soße---------original frankfurter grüne
soße

outback lager---------------------------outback lager

pâté chinois----------------------------pâté chinois

pavlova---------------------------------pavlova

perth pasties---------------------------perth pasties

queso cabrales--------------------------queso cabrales

queso manchego la pastora---------------queso manchego la pastora

raclette courdavault--------------------raclette courdavault

ravioli angelo--------------------------ravioli angelo

rhönbräu klosterbier--------------------rhönbräu klosterbier

röd kaviar------------------------------röd kaviar

rogede sild-----------------------------rogede sild

rössle sauerkraut-----------------------rössle sauerkraut

sasquatch ale---------------------------sasquatch ale

schoggi schokolade----------------------schoggi schokolade

scottish longbreads---------------------scottish longbreads

singaporean hokkien fried mee-----------singaporean hokkien fried mee

sir rodney's marmalade------------------sir rodney's marmalade

sir rodney's scones---------------------sir rodney's scones

sirop d'érable--------------------------sirop d'érable

spegesild-------------------------------spegesild

steeleye stout--------------------------steeleye stout

tarte au sucre--------------------------tarte au sucre

teatime chocolate biscuits--------------teatime chocolate biscuits

thüringer rostbratwurst-----------------thüringer rostbratwurst

tofu------------------------------------tofu

tourtière-------------------------------tourtière

tunnbröd--------------------------------tunnbröd

uncle bob's organic dried pears---------uncle bob's organic dried
pears

valkoinen suklaa------------------------valkoinen suklaa

vegie-spread----------------------------vegie-spread

wimmers gute semmelknödel---------------wimmers gute semmelknödel

zaanse koeken---------------------------zaanse koeken

Rahul- Hide quoted text -

- Show quoted text -

Are you writing this to a text file or a web page?
If you copy paste all the data you posted in the post above to
textpad, you'll see that it is correctly formatted.
But the same data does not look ok on a web page like this one,
because the fonts are not monospaced ( i think).
If you are writing to a web page then follow Eliyahu's advice and bind
it to some table.
 
R

Rahul

Are you writing this to a text file or a web page?
If you copy paste all the data you posted in the post above to
textpad, you'll see that it is correctly formatted.
But the same data does not look ok on a web page like this one,
because the fonts are not monospaced ( i think).
If you are writing to a web page then follow Eliyahu's advice and bind
it to some table.- Hide quoted text -

- Show quoted text -

Hi ,

I am writing in a simple text file.
here i use '-' character on the place of a single space.

Rahul
 
G

Guest

Hi ,

I am writing in a simple text file.
here i use '-' character on the place of a single space.

Rahul- Hide quoted text -

- Show quoted text -

If you don't really need a text to be right aligned, you can try to
use a tab

e.g.

String.Format("{0}" & vbTab & "{1}" & vbTab & "{2}", ...
 
R

Rahul

I think it's because of the size of the strings.

When you have

String.Format("{0,10}....

it does mean the length of the Code-field cannot be more than 10
characters, in other case it would shift the whole line.

Try to change

String.Format("{0,10}{1, 40} {2,-10}",
Left(DS.Table(0).Row(i)("Code").ToString(), 10),
Left(DS.Table(0).Row(i)("Description").ToString(), 40),
DS.Table(0).Row(i)("Amount").ToString())

I think it should help

Hi,

I have try it, but not working, and output is same

String.Format("{0,10}{1, 40} {2,-10}",
Left(DS.Table(0).Row(i)("Code").ToString(), 10),
Left(DS.Table(0).Row(i)("Description").ToString(), 40),
DS.Table(0).Row(i)("Amount").ToString())
 
G

Guest

Hi,

I have try it, but not working, and output is same

String.Format("{0,10}{1, 40} {2,-10}",
Left(DS.Table(0).Row(i)("Code").ToString(), 10),
Left(DS.Table(0).Row(i)("Description").ToString(), 40),
DS.Table(0).Row(i)("Amount").ToString())- Hide quoted text -

- Show quoted text -

Where do you open the file?

The text output must be viewed (or printed) with a fixed-width font in
order to see the correct result. If you use Notepad, click Format -
Font, and select a fixed-width font, for example, Courier.

The code to check

<%@ Import Namespace="System.IO" %>
<script language="vb" runat="server">
sub Page_Load(sender as Object, e as EventArgs)

Dim sw As System.IO.StreamWriter = New
System.IO.StreamWriter(Server.MapPath("test.txt"))
sw.WriteLine(String.Format("{0,10}{1, 40} {2,-10}", _
Left("123", 10), _
Left("123", 40), _
"123"))
sw.WriteLine(String.Format("{0,10}{1, 40} {2,-10}", _
Left("1234567890123456789", 10), _
Left("123", 40), _
"123"))
sw.WriteLine(String.Format("{0,10}{1, 40} {2,-10}", _
Left("12345", 10), _
Left("12345", 40), _
"12345"))
sw.Close()

end sub
</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top