Wrox Programmer Forums
BOOK: Beginning ASP.NET 1.0
This is the forum to discuss the Wrox book Beginning ASP.NET 1.0 with C# by Chris Goode, John Kauffman, Christopher L. Miller, Neil Raybould, S. Srinivasa Sivakumar, Dave Sussman, Ollie Cornes, Rob Birdwell, Matt Butler, Gary Johnson, Ajoy Krishnamoorthy, Juan T. Llibre, Chris Ullman; ISBN: 9780764543708
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 1.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old January 26th, 2005, 07:12 AM
Registered User
Join Date: Jun 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sql Parameterized query, @ or ?


I am trying to bind to a datagrid using a function to get the data from an SQL database, using a parameterized query. I think I need to use "?" in the query rather than "@Theme_id", but my code still doesn't work. Can anyone help please? The code is:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DataGrid1.DataSource = GetTableData()
    End Sub

    Function GetTableData() As DataTable
        Dim ds As New DataSet()
        Dim strSql As String
        Dim id As Integer
        Dim objCommand As New SqlCommand()
        objCommand.CommandText = strSql
        strSql = "SELECT DISTINCT WHMetaRecord.meta_id, WHThemes.Theme_id, WHMetaRecord.Title, WHMetaRecord.URL, WHThemes.Description, WHMetaRecord.CreationDate, WHMetaRecord.Format,WHMetaRecord.Size,WHMetaRecord .detailsurl FROM WHMetaRecord INNER JOIN WHMetaThemes ON WHMetaRecord.meta_id = WHMetaThemes.meta_id INNER JOIN WHThemes ON WHMetaThemes.Theme_id = WHThemes.Theme_id WHERE (WHThemes.Theme_id = ?) ORDER BY WHMetaRecord.Title,WHMetaRecord.CreationDate DESC"
        Dim da As New SqlDataAdapter(strSql, SqlConnection1)
        Dim myparameters As SqlParameterCollection()
        da.SelectCommand.Parameters.Add("@Theme_id", SqlDbType.Int)
        da.SelectCommand.Parameters(0).Value = id
        da.Fill(ds, "tblSearchResults")
        lbl1.Text = (ds.Tables(0).Rows(0).Item(4))
        lbl2.Text() = (ds.Tables(0).Rows(0).Item(4))
        Return ds.Tables("tblSearchResults")
    End Function

Thanks in anticipation

Old January 26th, 2005, 10:28 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts


Don't use ? for sql, use a parameter @Theme_id in the query.

Dim da As New SqlDataAdapter(strSql, SqlConnection1)
da.SelectCommand.Parameters.Add("@Theme_id", SqlDbType.Int, 10).Value = id

Dim ds as new DataSet
da.Fill(ds, "tblSearchResults")

Get rid of the command object; you aren't using it. Don't create a new dataset at the beginning, wait until later where I put it.

Old January 26th, 2005, 11:33 AM
Registered User
Join Date: Jun 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts

Brian, you are an absolute star - many thanks!

Similar Threads
Thread Thread Starter Forum Replies Last Post
Output Query to txt file from SQL Query everest SQL Server 2005 4 November 22nd, 2007 01:49 AM
DateTime Typed Paramater on Parameterized Query Mutia VB.NET 2002/2003 Basics 1 July 25th, 2007 08:57 AM
Calling Parameterized Update Query from C# fcortes C# 6 July 11th, 2007 08:47 AM
Parameterized Query asters VB.NET 2002/2003 Basics 5 July 9th, 2007 12:08 PM
Parameterized SQLDataAdapter boyshey VB.NET 2002/2003 Basics 1 April 14th, 2006 07:08 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.