what is wrong with this code -- sql query thru command object

Posted on 2006-04-14
Last Modified: 2010-04-23
this returns the error "Referenced object has a value of nothing" on the line rdr1 = cm1.executereader

I'm guessing it has to do with how I do parameters?

            c1 = New SqlConnection
            c1.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings("Connection")

            cm1 = New SqlCommand
            cm1.CommandType = CommandType.Text
            cm1.Connection = c1
            cm1.CommandText = "SELECT desc [d] FROM table1_sql WHERE item = ? ORDER BY fn"
            cm1.Parameters.Add("@Desc", SqlDbType.Char, 15, txtItem.Text)
            rdr1 = cm1.ExecuteReader
            While rdr1.Read
                lstFtr.Items.Add(CType(rdr1("d"), String))
            End While


        Catch ex As Exception

        End Try
Question by:g_johnson
    LVL 4

    Author Comment

    actually, the more i investigate the more i think it's a syntax error.

    obviously i am doing something wrong when adding parameters, but i don't know what it is
    LVL 69

    Expert Comment

    by:Éric Moreau
    Hi g_johnson,

    Is "desc" your field name? I think the problem lies there because "desc" is a reserved word (of the ORBER BY clause). Try putting it in square brakets like you did for the alias.

    LVL 69

    Expert Comment

    by:Éric Moreau

    and what happen if you don't use a parameter?  
    LVL 4

    Author Comment

    if i don't use a parameter, i'm fine
    LVL 4

    Author Comment

    this is the actual line of code:
    cm1.CommandText = "SELECT description_1 [d] FROM table1_sql WHERE item = ? ORDER BY fn"
    and ex.message returns
    "Line 1: Incorrect syntax near '?'."
    LVL 69

    Accepted Solution

    Does fn is a valid fieldname?

    Try this:
    cm1.CommandText = "SELECT desc [d] FROM table1_sql WHERE item = @Desc ORDER BY fn"
    LVL 4

    Author Comment

    got it -- just before I read your answer.  Now I understand!   :-)


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    759 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

    9 Experts available now in Live!

    Get 1:1 Help Now