VB.NET SQL Command

Last post 06-07-2009 5:48 PM by Naom. 8 replies.

Sort Posts:

  • VB.NET SQL Command

    06-06-2009, 2:32 AM
    • Member
      19 point Member
    • cypher_jy
    • Member since 05-06-2009, 2:08 AM
    • Posts 41

    cmdSelect = New SqlCommand("SELECT most, least FROM Test WHERE QuestNo ='" + q1Lbl.Text + "'", conMyData)

    The "most" and "least" are variables. I would like to know whether can i use the variables in the sql command as above??

  • Re: VB.NET SQL Command

    06-06-2009, 4:06 AM
    • All-Star
      59,913 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,551
    • TrustedFriends-MVPs

    Above is a wrong way of passing data to sql query use parameters

    http://aspsnippets.com/post/Parameterized-Queries-ADONet.aspx

    Download the source and use it

  • Re: VB.NET SQL Command

    06-06-2009, 4:06 AM
    • Contributor
      2,142 point Contributor
    • om_prakashp
    • Member since 03-09-2009, 6:50 AM
    • Posts 312

    If most, least are column names in "Test" table then you can use the query as you mentioned:

    cmdSelect = New SqlCommand("SELECT most, least FROM Test WHERE QuestNo ='" + q1Lbl.Text + "'", conMyData)

    Om Prakash
  • Re: VB.NET SQL Command

    06-06-2009, 4:11 AM
    • Contributor
      2,367 point Contributor
    • akhhttar
    • Member since 02-14-2007, 8:17 AM
    • Pakistan - Lahore
    • Posts 352

    Hi,

    No, you can't use C# variable in SQL Command like this. Can you please let me know that why you want to use these variables in SQL Command? So that i might be able to suggest you some alternative.

     Thank You

    Muhammad Akhtar Shiekh

    Lets resolve the problem together.

    Please remember to mark the appropriate replies as answer after your question is solved, thanks

    My Blog
  • Re: VB.NET SQL Command

    06-06-2009, 7:25 AM
    • Member
      19 point Member
    • cypher_jy
    • Member since 05-06-2009, 2:08 AM
    • Posts 41

    Im getting a selected value from a radio button list(RBL) and storing that value into a variable. The value that ill be getting from the RBL are M1 or M2 or M3 or M4, which are also column names in the Test table. Kindly advice me on other alternatives.

  • Re: VB.NET SQL Command

    06-06-2009, 7:35 AM
    Answer
    • Contributor
      2,367 point Contributor
    • akhhttar
    • Member since 02-14-2007, 8:17 AM
    • Pakistan - Lahore
    • Posts 352

     Okey, Go it.

    All you need is to contact the values of these variable in SQL Query , like following

    SqlCommand("SELECT " +most + ", " +least + " FROM Test WHERE QuestNo ='" + q1Lbl.Text + "'", conMyData )

    Thanks

    Muhammad Akhtar Shiekh

    Lets resolve the problem together.

    Please remember to mark the appropriate replies as answer after your question is solved, thanks

    My Blog
  • Re: VB.NET SQL Command

    06-06-2009, 7:55 AM
    • All-Star
      59,913 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,551
    • TrustedFriends-MVPs

    cypher_jy:

    Im getting a selected value from a radio button list(RBL) and storing that value into a variable. The value that ill be getting from the RBL are M1 or M2 or M3 or M4, which are also column names in the Test table. Kindly advice me on other alternatives.

    Did you refer the article lin I gave you ?? Tell me what problem you are facing?

  • Re: VB.NET SQL Command

    06-06-2009, 8:49 PM
    • Contributor
      3,588 point Contributor
    • Niikola
    • Member since 05-26-2009, 8:06 AM
    • Barcelona, Spain
    • Posts 701

     May I ask what is the point of this query?

    The "most" and "least" are variables.

    That's fine, but are they numeric values or column names?

     


    Smile...tomorrow will be worse
  • Re: VB.NET SQL Command

    06-07-2009, 5:48 PM
    • All-Star
      31,047 point All-Star
    • Naom
    • Member since 12-31-2007, 7:08 PM
    • Wisconsin
    • Posts 6,872

    Looks like they were column names.

    Looking for a job opportunity.

    Beware of bugs in the above code; I have only proved it correct, not tried it.
    (Donald Knuth)

    Visit my blog

    PluralSight Learning Library
Page 1 of 1 (9 items)