Read Comma Delimited File

  • Thread starter Luis Esteban Valencia
  • Start date
L

Luis Esteban Valencia

Hello. I have a method that receives a customer number, and it must return
the customer credit which is in a file. like this.

So if the parameter is 1, it must give me 450000.


1,450000
2,60000
3,34000
4,780000
5,99000
10,45000
11,340000
12,45000
13,56000
14,56000
15,20000
16,90000
17,99999
18,23090
19,34434
20,34534


Thanks
 
M

Mark Rae

Hello. I have a method that receives a customer number, and it must
return
the customer credit which is in a file. like this.

So if the parameter is 1, it must give me 450000.

Several ways:

1) Open the file into a StreamReader object, read each line in turn and
Split() it into an array, looping till you find the line you're looking for

2) Use ADO.NET, OleDb and the Jet provider to treat the file like a database
table and then run a simple SELECT statement against it
 
L

Luis Esteban Valencia

I already did but I got an error.

[WebMethod]

public int traerCredito(int cliente)

{

StreamReader sr;

string linea;

try

{

sr=File.OpenText(archivo);

while (sr.Peek() != -1)

{

linea= sr.ReadLine();

char[] splitter = {','};

string[] otr= linea.Split(splitter, 2);

if (otr[0].ToString() == cliente.ToString())

{

return Convert.ToInt32(otr[1]);

}

else

{

return -1;

}

}

}

catch(Exception ex)

{

//throw ex;

return -1;

}


}



The error is:

:\inetpub\wwwroot\WSCreditCheck\CreditCheck.asmx.cs(61):
'WSCreditCheck.CreditCheck.traerCredito(int)': no todas las rutas de código
devuelven un valor




Not all the paths return a value.??

Why??
 
M

Mark Rae

Not all the paths return a value.??

Try this:

public int traerCredito(int cliente)
{
StreamReader sr;
string linea;
int intReturn = 0;
/*
0 = found
0 = not found
-1 = error
*/

try
{
sr=File.OpenText(archivo);
while (sr.Peek() != -1)
{
linea= sr.ReadLine();
char[] splitter = {','};
string[] otr= linea.Split(splitter, 2);
if (otr[0].ToString() == cliente.ToString())
{
intReturn = Convert.ToInt32(otr[1]);
break;
}
}
return intReturn;
}
catch(Exception ex)
{
//throw ex;
return -1;
}
}
 
P

Paul Clement

¤ Hello. I have a method that receives a customer number, and it must return
¤ the customer credit which is in a file. like this.
¤
¤ So if the parameter is 1, it must give me 450000.
¤
¤
¤ 1,450000
¤ 2,60000
¤ 3,34000
¤ 4,780000
¤ 5,99000
¤ 10,45000
¤ 11,340000
¤ 12,45000
¤ 13,56000
¤ 14,56000
¤ 15,20000
¤ 16,90000
¤ 17,99999
¤ 18,23090
¤ 19,34434
¤ 20,34534
¤

The ADO.NET method (you could also use a DataReader):

Dim ConnectionString As String

ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & "E:\My Documents\TextFiles" & ";" & _
"Extended Properties=""Text;HDR=No"""

Dim TextConnection As New System.Data.OleDb.OleDbConnection(ConnectionString)
TextConnection.Open()

Dim da As New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM FileName#csv WHERE F1='1'",
TextConnection)

Dim ds As New DataSet("TextFiles")
da.Fill(ds, "CSV")
Dim dt As DataTable
dt = ds.Tables("CSV")

Dim drCurrent As DataRow
For Each drCurrent In dt.Rows
Console.WriteLine(drCurrent.Item(0).ToString)
Console.WriteLine(drCurrent.Item(1).ToString)
Next


Paul
~~~~
Microsoft MVP (Visual Basic)
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top