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

Posted on 2005-05-05
Last Modified: 2011-10-03
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 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


Question by:fruhj
    LVL 21

    Assisted Solution

    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?

    LVL 14

    Accepted Solution

    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.
    LVL 13

    Assisted Solution

    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;
    LVL 12

    Author Comment

    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

    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!
    LVL 12

    Author Comment

    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)

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now