easy points - best way to modify select statement contained in the #region Web Form Designer generated code

Hey Experts-

   I'm working in asp.neet using c#

   my form has a select statement in the section:

   #region Web Form Designer generated code
   ...
   this.odbcSelectCommand1.CommandText = "SELECT LIST.* FROM LIST WHERE userID = 2";

   I need to alter the where statement programatically.

  Should I just mess with that string and build what I need, or is there another way of doing it that overrides the web form generated code -

  in otherwords, I know I can do something like
    this.odbcSelectCommand1.CommandText = "SELECT LIST.* FROM LIST WHERE userID = " + varUserId ;

  but what I need to know is - is this the best or "right" way to do it?
  for that matter I'm pretty new to ASP.net and C# - can I just specify the whole thing in my code? which would take precidence?

  I'm working on this right now -so if someone can give a good answer - you'll likely get the points today.

 - Jack


   

  Basically
LVL 12
fruhjAsked:
Who is Participating?
 
existenz2Commented:
I prefer to use:
this.odbcSelectCommand1.CommandText = string.Format("SELECT * FROM LIST WHERE userID = {0}",varUserId);

The string.Format is just what you like most for using. I prefer this.

But note the SELECT statement, instead of using LIST.* or * I would recommend you to use the actual columnnames from the table, even if you need them all. This ensures you the query will keep working, when the table gets an extra column.
0
 
YurichCommented:
it's an absolutely fine way to do it. The only other way is to use parameters but I wouldn't bother since there is absolutely nothing wrong with your code and it's simpler.

what do you mean by "can I just specify the whole thing in my code? which would take precidence?" - what's a "whole thing" and what should take a precidence of what?

regards,
mik
0
 
dunglaCommented:
So you are assign CommandText in design time right? I think you do something like that:
this.odbcSelectCommand1.CommandText = "SELECT * FROM LIST WHERE UserID = ?";
odbcSelectCommand1.Parameters.Add("@UserID", SqlDbType.Int);
odbcSelectCommand1.Parameters["@UserID"].Value = varUserID;
0
 
fruhjAuthor Commented:
Thanks Guys (or Gals)

You guys gave me some alternate ways of putting the parameters in the querty - which is great stuff to know.

What I was really getting at (and probably was unclear about) is - should the Web Form Designer Code ever be modified? By default in VS2003, it's hidden and I have to click the '-' symbol to expand the section and see the code -that made me leary of modifying it - for fear that it could cause issues with the development IDE being able to recognize it's own code.

What I found was that I seem to be able to set the value twice - once in the web generated code using
this.odbcSelectCommand1....

and again in my if(!ispostback) block -
  just using
odbcSelectCommand1... (without the this)

I assume that the order of code execution is such that the if(!postback) loop occurs after the web form designer code

Thanks again for your responses!
0
 
fruhjAuthor Commented:
PS the system made me choose a single answer as the accepted one - however I am equally greatful to all 3 of you for your comments - I've upped the question value from 250 to 450 so a 3 way split works out to 150 each (600 each with an A weighting)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.