string array

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

    string array

    How to get a list of names from database into string array?

    variable name should be string array...

    This is the code I use...

    public string GetFunctionName (Guid UserID)

    {


    sSelect = "";

    string name="";

    if (db!=null)

    {


    if (nGroupID.Lengt h !=0)

    {

    for ( int iIndex = 0; iIndex < nGroupID.Length ; iIndex++ )

    {

    if ( iIndex != 0 )

    sSelect += " OR ";

    sSelect += "GroupID ='" + nGroupID[ iIndex ].ToString () + "'";

    }



    DataRow[] FGrows = db.dataSetUsers .FunctionsGroup s.Select(sSelec t);


    foreach (DataSetUsers.F unctionsGroupsR ow row in FGrows)

    {

    DataSetUsers.Fu nctionsRow function =

    db.dataSetUsers .Functions.Find ByFunctionID( row.FunctionID );

    name = function.Name ;

    }

    }

    }

    return name;

    }


  • John Timney \(ASP.NET MVP\)

    #2
    Re: string array

    Once you concatonate the names in a string, you could just use string split
    method and it would create an array for you, or add an iterator to your
    foreach and add them directly to a string array.

    --
    Regards

    John Timney
    ASP.NET MVP
    Microsoft Regional Director

    "Hrvoje Voda" <hrvoje.voda@lu atech.com> wrote in message
    news:d8u0n9$1kf $1@ss405.t-com.hr...[color=blue]
    > How to get a list of names from database into string array?
    >
    > variable name should be string array...
    >
    > This is the code I use...
    >
    > public string GetFunctionName (Guid UserID)
    >
    > {
    >
    >
    > sSelect = "";
    >
    > string name="";
    >
    > if (db!=null)
    >
    > {
    >
    >
    > if (nGroupID.Lengt h !=0)
    >
    > {
    >
    > for ( int iIndex = 0; iIndex < nGroupID.Length ; iIndex++ )
    >
    > {
    >
    > if ( iIndex != 0 )
    >
    > sSelect += " OR ";
    >
    > sSelect += "GroupID ='" + nGroupID[ iIndex ].ToString () + "'";
    >
    > }
    >
    >
    >
    > DataRow[] FGrows = db.dataSetUsers .FunctionsGroup s.Select(sSelec t);
    >
    >
    > foreach (DataSetUsers.F unctionsGroupsR ow row in FGrows)
    >
    > {
    >
    > DataSetUsers.Fu nctionsRow function =
    >
    > db.dataSetUsers .Functions.Find ByFunctionID( row.FunctionID );
    >
    > name = function.Name ;
    >
    > }
    >
    > }
    >
    > }
    >
    > return name;
    >
    > }
    >
    >[/color]


    Comment

    • Hrvoje Voda

      #3
      Re: string array

      I'm not that good in c#, can you give me some example?




      "John Timney (ASP.NET MVP)" <timneyj@despam med.com> wrote in message
      news:uPDt2dycFH A.132@TK2MSFTNG P10.phx.gbl...[color=blue]
      > Once you concatonate the names in a string, you could just use string
      > split method and it would create an array for you, or add an iterator to
      > your foreach and add them directly to a string array.
      >
      > --
      > Regards
      >
      > John Timney
      > ASP.NET MVP
      > Microsoft Regional Director
      >
      > "Hrvoje Voda" <hrvoje.voda@lu atech.com> wrote in message
      > news:d8u0n9$1kf $1@ss405.t-com.hr...[color=green]
      >> How to get a list of names from database into string array?
      >>
      >> variable name should be string array...
      >>
      >> This is the code I use...
      >>
      >> public string GetFunctionName (Guid UserID)
      >>
      >> {
      >>
      >>
      >> sSelect = "";
      >>
      >> string name="";
      >>
      >> if (db!=null)
      >>
      >> {
      >>
      >>
      >> if (nGroupID.Lengt h !=0)
      >>
      >> {
      >>
      >> for ( int iIndex = 0; iIndex < nGroupID.Length ; iIndex++ )
      >>
      >> {
      >>
      >> if ( iIndex != 0 )
      >>
      >> sSelect += " OR ";
      >>
      >> sSelect += "GroupID ='" + nGroupID[ iIndex ].ToString () + "'";
      >>
      >> }
      >>
      >>
      >>
      >> DataRow[] FGrows = db.dataSetUsers .FunctionsGroup s.Select(sSelec t);
      >>
      >>
      >> foreach (DataSetUsers.F unctionsGroupsR ow row in FGrows)
      >>
      >> {
      >>
      >> DataSetUsers.Fu nctionsRow function =
      >>
      >> db.dataSetUsers .Functions.Find ByFunctionID( row.FunctionID );
      >>
      >> name = function.Name ;
      >>
      >> }
      >>
      >> }
      >>
      >> }
      >>
      >> return name;
      >>
      >> }
      >>
      >>[/color]
      >
      >[/color]


      Comment

      • Nathan Kovac

        #4
        Re: string array

        I am not totally clear what your doing in your code and what you want to do
        so I am going to show you a fresh example of how I would do what the
        previous guy mentioned.

        Keep in mind I didn't understand your example and you may already have a
        loop set up to go through the records and the last 2 lines is probably what
        you need. Also note I haven't tested this code, but it should work with a
        little debugging.

        private string[] GetFirstColFrom Sql(cmdText);

        {

        System.Data.Sql Client.SqlConne ction conn=this.GetOp enConnection();

        System.Data.Sql Client.SqlComma nd sqlCommand=new
        System.Data.Sql Client.SqlComma nd(cmdText, conn);

        System.Data.Sql Client.SqlDataR eader
        r=sqlCommand.Ex ecuteReader(Sys tem.Data.Comman dBehavior.Defau lt);

        String tmpString="";

        while (r.Read())

        {

        tmpString+="|"+ Convert.ToStrin g(r.Value(0));

        }

        string[] resultArray=tmp String.Split('| ');

        r.Close();

        conn.Close();

        return resultArray;

        }

        "Hrvoje Voda" <hrvoje.voda@lu atech.com> wrote in message
        news:d8ubim$nke $1@ss405.t-com.hr...[color=blue]
        > I'm not that good in c#, can you give me some example?
        >
        >
        >
        >
        > "John Timney (ASP.NET MVP)" <timneyj@despam med.com> wrote in message
        > news:uPDt2dycFH A.132@TK2MSFTNG P10.phx.gbl...[color=green]
        >> Once you concatonate the names in a string, you could just use string
        >> split method and it would create an array for you, or add an iterator to
        >> your foreach and add them directly to a string array.
        >>
        >> --
        >> Regards
        >>
        >> John Timney
        >> ASP.NET MVP
        >> Microsoft Regional Director
        >>
        >> "Hrvoje Voda" <hrvoje.voda@lu atech.com> wrote in message
        >> news:d8u0n9$1kf $1@ss405.t-com.hr...[color=darkred]
        >>> How to get a list of names from database into string array?
        >>>
        >>> variable name should be string array...
        >>>
        >>> This is the code I use...
        >>>
        >>> public string GetFunctionName (Guid UserID)
        >>>
        >>> {
        >>>
        >>>
        >>> sSelect = "";
        >>>
        >>> string name="";
        >>>
        >>> if (db!=null)
        >>>
        >>> {
        >>>
        >>>
        >>> if (nGroupID.Lengt h !=0)
        >>>
        >>> {
        >>>
        >>> for ( int iIndex = 0; iIndex < nGroupID.Length ; iIndex++ )
        >>>
        >>> {
        >>>
        >>> if ( iIndex != 0 )
        >>>
        >>> sSelect += " OR ";
        >>>
        >>> sSelect += "GroupID ='" + nGroupID[ iIndex ].ToString () + "'";
        >>>
        >>> }
        >>>
        >>>
        >>>
        >>> DataRow[] FGrows = db.dataSetUsers .FunctionsGroup s.Select(sSelec t);
        >>>
        >>>
        >>> foreach (DataSetUsers.F unctionsGroupsR ow row in FGrows)
        >>>
        >>> {
        >>>
        >>> DataSetUsers.Fu nctionsRow function =
        >>>
        >>> db.dataSetUsers .Functions.Find ByFunctionID( row.FunctionID );
        >>>
        >>> name = function.Name ;
        >>>
        >>> }
        >>>
        >>> }
        >>>
        >>> }
        >>>
        >>> return name;
        >>>
        >>> }
        >>>
        >>>[/color]
        >>
        >>[/color]
        >
        >[/color]


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: string array

          Nathan Kovac <nate@tctelco.n et> wrote:[color=blue]
          > I am not totally clear what your doing in your code and what you want to do
          > so I am going to show you a fresh example of how I would do what the
          > previous guy mentioned.
          >
          > Keep in mind I didn't understand your example and you may already have a
          > loop set up to go through the records and the last 2 lines is probably what
          > you need. Also note I haven't tested this code, but it should work with a
          > little debugging.[/color]

          <snip>
          [color=blue]
          > while (r.Read())
          >
          > {
          > tmpString+="|"+ Convert.ToStrin g(r.Value(0));
          > }
          > string[] resultArray=tmp String.Split('| ');
          > r.Close();
          > conn.Close();
          > return resultArray;[/color]

          There's no point in putting everything into a string and then splitting
          it up again.

          Just use an ArrayList, and entries to that in the while loop. If an
          array is required afterwards, you can then call ToArray(typeof( string))
          on the ArrayList and cast to string[].

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          Working...