How to write a module for the string manipulation?

J

JenHu

Hi all,

I want to retrieve data from database and generate a string line
with fix length characters, so I can put into a file. But this is
the first time I am writing module besides ASP.NET. I need help from
you.

I wrote the code like this, and I don't how to put into a string
line........can someone show me how to do it.
The output should like this:
738487827 Baston Mary

Thanks.

Imports System.Data
Imports System.Data.SqlClient
Module Module1

Dim GPConnection As SqlConnection
Dim GPCommand As SqlCommand
Dim dr As SqlDataReader

Sub Main()
Dim EmpID(10) As Char
Dim LName(30) As Char
Dim FName(30) As Char
Try
GPConnection = New SqlConnection("data
source=AMKSQL2;initial log=EPAY;password=123")
GPConnection.Open()
GPCommand = New SqlCommand("SELECT
EMPLOYID,LASTNAME,FRSTNAME FROM UPR00102", GPConnection)
dr = GPCommand.ExecuteReader
Dim InFile As String
InFile += GenerateARecord(EmpID, LName, FName)
Catch
End Try
dr.Close()
GPConnection.Close()
End Sub

Function GenerateARecord(ByVal strEmpID As String, ByVal strLName
As String, ByVal strFName As String)


End Function
End Module

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
L

Lucas Tam

(e-mail address removed)-spam.invalid (JenHu) wrote in
Hi all,

I want to retrieve data from database and generate a string line
with fix length characters, so I can put into a file. But this is
the first time I am writing module besides ASP.NET. I need help from
you.

How many characters are each column?

I wrote the code like this, and I don't how to put into a string
line........can someone show me how to do it.
The output should like this:
738487827 Baston Mary

You have a data reader right?

To output, go dr("Field1") & Space(5) & dr("field2") & space(10) & etc

A couple of useful functions:

Space - allows you to output x number of spaces
Len - Length of the string
String.PadLeft/.PadRight - pads the string with X number of characters

Make sure you close the datareader when you're done with it.
 
C

Cor Ligthert

Mary,

In addition to Lucas,

In my opinon would you have a look at the datasets and in that the
datatables.

With reading those, you get a table from which you can use the fields in the
datarows as

datarow(0)
datarow(1)
datarow(2)

When you want more information, message that back?

Cor
 
J

JenHu

How many characters are each column?

Dim EmpID(10) As Char 'EmpID is position 1~10
Dim LName(30) As Char 'LName is position 11~40
Dim FName(30) As Char 'FName is position 40~70
To output, go dr("Field1") & Space(5) & dr("field2") &
space(10) & etc

Do you mean I should write like this?
dr.Read()
strEmpID = dr("EMPLOYID")
strLName = dr("LastName")
strFName = dr("FRSTName")
Dim strResult As String
strResult=dr("EmpID") & Space(10) & dr("LName")
& Space(30) & dr("FName") & Space(30)
dr.Close()
With reading those, you get a table from which you can use the fields in the
datarows as

datarow(0)
datarow(1)
datarow(2)
Can you explain more about how to use dataset to generate a text
delimited string line? Thanks.

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
C

Cor Ligthert

Jen Yu

Using your first sent code something as (everything typed in this message so
watch by instance typos).

\\\
Imports System.text
Imports System.Data
Imports System.Data.SqlClient
Module Module1
Private GPCDataset as new Dataset
Sub Main()
Dim GPConnection as New SqlConnection("data
source=AMKSQL2;initial log=EPAY;password=123")
Dim GPCDataAdapter as New SQLDataAdapter("SELECT
employid,lastname,frstname FROM UPR00102", GPConnection)

Try
GPConnection.Open()
GPCDataAdapter.fill(GPCDataset)
Catch ex as exception
messagebox.show(ex.tostring)
Finally
GPCConnection.dispose
End Try
End Sub
Function GenerateARecord(ByVal RowNumber as integer) as string
dim sb as new stringbuilder
dim dr as datarow = GPCDataset.tables(0).rows(RowNumber)
sb.append(dr(0))
sb.append(" ")
sb.append(dr(1))
sb.append(" ")
sb.append("dr(2))
return sb.tostring
End Function
End Module
////

I hope this helps?

Cor
 

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

Top