code error

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

    #1

    code error

    what is wrong with this code ?


    listUsers.Displ ayMember = "UserName";

    string UserIDSelect = "UserID not in "+ "(Select UserID "+

    "From Users where Users.UserID ='" +
    nUserID + "'" + ")";


    DataRow[] Urows = db.dataSetAccou nts.Users.Selec t(UserIDSelect) ;

    foreach ( DataSetAccounts .UsersRow row in Urows )

    {

    DataSetAccounts .UsersRow users =

    db.dataSetAccou nts.Users.FindB yUserID ( row.UserID );

    if ( users != null )

    {

    listUsers.Items .Add( users );

    }

    }



    Hrcko


  • Yunus Emre ALPÖZEN [MCAD.NET]

    #2
    Re: code error

    Opps what are u trying to do??
    [color=blue]
    > string UserIDSelect = "UserID not in "+ "(Select UserID "+
    >
    > "From Users where Users.UserID ='"
    > + nUserID + "'" + ")";[/color]

    Simply use
    db.dataSetAccou nts.Users.Defau ltView=String.F ormat("UserID <>'{0}'
    ",nUserID);

    And what is this ????[color=blue]
    > DataRow[] Urows = db.dataSetAccou nts.Users.Selec t(UserIDSelect) ;
    >
    > foreach ( DataSetAccounts .UsersRow row in Urows )
    > {
    >
    > DataSetAccounts .UsersRow users =
    >
    > db.dataSetAccou nts.Users.FindB yUserID ( row.UserID );
    >
    > if ( users != null )
    >
    > {
    >
    > listUsers.Items .Add( users );
    >
    > }[/color]

    Select method iterates whole datatable once and then in the loop foreach row
    you call FindByUserID which iterates whole table again and again. (n*n) What
    about algorithm complexity ?

    Clear whole code and simply iterate datatable one time

    foreach ( DataSetAccounts .UsersRow row in Urows )
    {
    if (row.UserID==nU serID) continue;
    listUser.Items. Add(row);
    }

    I think it is quite simple and faster than yours.....

    --

    Thanks,
    Yunus Emre ALPÖZEN
    BSc, MCAD.NET

    "Hrcko" <hrvoje.voda2@z g.htnet.hr> wrote in message
    news:d5gc0q$j5c $1@ss405.t-com.hr...[color=blue]
    > what is wrong with this code ?
    >
    >
    > listUsers.Displ ayMember = "UserName";
    >
    > string UserIDSelect = "UserID not in "+ "(Select UserID "+
    >
    > "From Users where Users.UserID ='"
    > + nUserID + "'" + ")";
    >
    >
    > DataRow[] Urows = db.dataSetAccou nts.Users.Selec t(UserIDSelect) ;
    >
    > foreach ( DataSetAccounts .UsersRow row in Urows )
    >
    > {
    >
    > DataSetAccounts .UsersRow users =
    >
    > db.dataSetAccou nts.Users.FindB yUserID ( row.UserID );
    >
    > if ( users != null )
    >
    > {
    >
    > listUsers.Items .Add( users );
    >
    > }
    >
    > }
    >
    >
    >
    > Hrcko
    >
    >[/color]


    Comment

    • Ignacio Machin \( .NET/ C# MVP \)

      #3
      Re: code error

      Hi,

      Well first of all A little explanation of what is what would be helpful

      I assume that db.dataSetAccou nts.Users is a datatable , if so the
      query you are using is not correct, a DatTable and/or dataset is not a DB
      engine, meaning that you cannot use a subquery to get data from it.

      In this case I think that you want to get all the users except nUserID

      if this is the case use a DataView


      DataView dv = new DataView ( db.dataSetAccou nts.Users, "UserID <> " +
      nUserID, DataViewCurrent RowStatus.Curre ntRows);

      Cheers,

      --
      Ignacio Machin,
      ignacio.machin AT dot.state.fl.us
      Florida Department Of Transportation


      "Hrcko" <hrvoje.voda2@z g.htnet.hr> wrote in message
      news:d5gc0q$j5c $1@ss405.t-com.hr...[color=blue]
      > what is wrong with this code ?
      >
      >
      > listUsers.Displ ayMember = "UserName";
      >
      > string UserIDSelect = "UserID not in "+ "(Select UserID "+
      >
      > "From Users where Users.UserID ='"
      > + nUserID + "'" + ")";
      >
      >
      > DataRow[] Urows = db.dataSetAccou nts.Users.Selec t(UserIDSelect) ;
      >
      > foreach ( DataSetAccounts .UsersRow row in Urows )
      >
      > {
      >
      > DataSetAccounts .UsersRow users =
      >
      > db.dataSetAccou nts.Users.FindB yUserID ( row.UserID );
      >
      > if ( users != null )
      >
      > {
      >
      > listUsers.Items .Add( users );
      >
      > }
      >
      > }
      >
      >
      >
      > Hrcko
      >
      >[/color]


      Comment

      • Hrcko

        #4
        Re: code error

        I get an error on DataViewCurrent RowStatus.Curre ntRows.

        Hrcko


        "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
        in message news:%23YqgpumU FHA.3188@TK2MSF TNGP09.phx.gbl. ..[color=blue]
        > Hi,
        >
        > Well first of all A little explanation of what is what would be helpful
        >
        > I assume that db.dataSetAccou nts.Users is a datatable , if so
        > the query you are using is not correct, a DatTable and/or dataset is not
        > a DB engine, meaning that you cannot use a subquery to get data from it.
        >
        > In this case I think that you want to get all the users except nUserID
        >
        > if this is the case use a DataView
        >
        >
        > DataView dv = new DataView ( db.dataSetAccou nts.Users, "UserID <> " +
        > nUserID, DataViewCurrent RowStatus.Curre ntRows);
        >
        > Cheers,
        >
        > --
        > Ignacio Machin,
        > ignacio.machin AT dot.state.fl.us
        > Florida Department Of Transportation
        >
        >
        > "Hrcko" <hrvoje.voda2@z g.htnet.hr> wrote in message
        > news:d5gc0q$j5c $1@ss405.t-com.hr...[color=green]
        >> what is wrong with this code ?
        >>
        >>
        >> listUsers.Displ ayMember = "UserName";
        >>
        >> string UserIDSelect = "UserID not in "+ "(Select UserID "+
        >>
        >> "From Users where Users.UserID ='"
        >> + nUserID + "'" + ")";
        >>
        >>
        >> DataRow[] Urows = db.dataSetAccou nts.Users.Selec t(UserIDSelect) ;
        >>
        >> foreach ( DataSetAccounts .UsersRow row in Urows )
        >>
        >> {
        >>
        >> DataSetAccounts .UsersRow users =
        >>
        >> db.dataSetAccou nts.Users.FindB yUserID ( row.UserID );
        >>
        >> if ( users != null )
        >>
        >> {
        >>
        >> listUsers.Items .Add( users );
        >>
        >> }
        >>
        >> }
        >>
        >>
        >>
        >> Hrcko
        >>
        >>[/color]
        >
        >[/color]


        Comment

        Working...