How to write a module for the string manipulation?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JenHu

    How to write a module for the string manipulation?

    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.Sql Client
    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;passwo rd=123")
    GPConnection.Op en()
    GPCommand = New SqlCommand("SEL ECT
    EMPLOYID,LASTNA ME,FRSTNAME FROM UPR00102", GPConnection)
    dr = GPCommand.Execu teReader
    Dim InFile As String
    InFile += GenerateARecord (EmpID, LName, FName)
    Catch
    End Try
    dr.Close()
    GPConnection.Cl ose()
    End Sub

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


    End Function
    End Module

    *-----------------------*
    Posted at:

    *-----------------------*
  • Lucas Tam

    #2
    Re: How to write a module for the string manipulation?

    jenniferhu46@ho tmail-dot-com.no-spam.invalid (JenHu) wrote in
    news:41ae2ff1$1 _5@Usenet.com:
    [color=blue]
    > 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.[/color]

    How many characters are each column?

    [color=blue]
    > 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[/color]

    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.

    --
    Lucas Tam (REMOVEnntp@rog ers.com)
    Please delete "REMOVE" from the e-mail address when replying.

    Comment

    • Cor Ligthert

      #3
      Re: How to write a module for the string manipulation?

      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



      "JenHu" <jenniferhu46@h otmail-dot-com.no-spam.invalid>[color=blue]
      > 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.Sql Client
      > 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;passwo rd=123")
      > GPConnection.Op en()
      > GPCommand = New SqlCommand("SEL ECT
      > EMPLOYID,LASTNA ME,FRSTNAME FROM UPR00102", GPConnection)
      > dr = GPCommand.Execu teReader
      > Dim InFile As String
      > InFile += GenerateARecord (EmpID, LName, FName)
      > Catch
      > End Try
      > dr.Close()
      > GPConnection.Cl ose()
      > 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
      > *-----------------------*[/color]


      Comment

      • JenHu

        #4
        Re: How to write a module for the string manipulation?

        > 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
        [color=blue]
        > To output, go dr("Field1") & Space(5) & dr("field2") &[/color]
        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("E mpID") & Space(10) & dr("LName")
        & Space(30) & dr("FName") & Space(30)
        dr.Close()
        [color=blue]
        >
        > With reading those, you get a table from which you can use the[/color]
        fields in the[color=blue]
        > datarows as
        >
        > datarow(0)
        > datarow(1)
        > datarow(2)[/color]
        Can you explain more about how to use dataset to generate a text
        delimited string line? Thanks.

        *-----------------------*
        Posted at:

        *-----------------------*

        Comment

        • Cor Ligthert

          #5
          Re: How to write a module for the string manipulation?

          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.Sql Client
          Module Module1
          Private GPCDataset as new Dataset
          Sub Main()
          Dim GPConnection as New SqlConnection(" data
          source=AMKSQL2; initial log=EPAY;passwo rd=123")
          Dim GPCDataAdapter as New SQLDataAdapter( "SELECT
          employid,lastna me,frstname FROM UPR00102", GPConnection)

          Try
          GPConnection.Op en()
          GPCDataAdapter. fill(GPCDataset )
          Catch ex as exception
          messagebox.show (ex.tostring)
          Finally
          GPCConnection.d ispose
          End Try
          End Sub
          Function GenerateARecord (ByVal RowNumber as integer) as string
          dim sb as new stringbuilder
          dim dr as datarow = GPCDataset.tabl es(0).rows(RowN umber)
          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

          "JenHu" <jenniferhu46@h otmail-dot-com.no-spam.invalid>
          [color=blue][color=green]
          >> How many characters are each column?[/color]
          >
          > 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
          >[color=green]
          >> To output, go dr("Field1") & Space(5) & dr("field2") &[/color]
          > 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("E mpID") & Space(10) & dr("LName")
          > & Space(30) & dr("FName") & Space(30)
          > dr.Close()
          >[color=green]
          >>
          >> With reading those, you get a table from which you can use the[/color]
          > fields in the[color=green]
          >> datarows as
          >>
          >> datarow(0)
          >> datarow(1)
          >> datarow(2)[/color]
          > Can you explain more about how to use dataset to generate a text
          > delimited string line? Thanks.
          >
          > *-----------------------*
          > Posted at:
          > www.GroupSrv.com
          > *-----------------------*[/color]


          Comment

          Working...