Dynamic Sql not working for DB2 (from book: Asp.net Data Web Controls - Listing 6.2)

S

Samson

A code sample in Listing 6.2 of Scott Mitchell's book Asp.net Data Web
Controls shows how to generate a dynamic sql statement with a WHERE
clause based on a button that the user clicks. It is based on the Sql
Server's Pubs database and everything works fine, but when I changed
it to DB2 and modified the associated code (connecting now via ODBC so
all the Sql* became Odbc*), the dynamic sql doesn't work anymore
(gives no error message; it displays table headers but gives no data)
although my static sql statement (when filterValue = String.Empty)
still works. Below is my code. I am wondering if the @ symbol in the
dynamic WHERE clause works for Sql servers only. Does anyone have any
idea? Please help!

*****************************************************

<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Odbc" %>
<script runat="server" language="VB">

Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack then
BindData(String.Empty)
End If
End Sub

Sub BindData(filterValue as String)
DisplayViewingTypeMessage(filterValue)

'1. Create a connection
Const strConnString as String = "DSN=myDsn;UID=myId;PWD=myPwd;"
Dim objConn as New OdbcConnection(strConnString)

'Create an appropriate SQL command string
Dim strSQL as String
If filterValue = String.Empty then
strSQL = "SELECT lname, fname FROM books WHERE publisher = 'sams'"
Else
'SQL needs WHERE clause
strSQL = "SELECT lname, fname, FROM books WHERE publisher = 'sams'
AND '@category'"
End If

'2. Create a command object for the query
Dim objCmd as New OdbcCommand(strSQL, objConn)

'Add parameter for WHERE clause if needed
If strSQL <> String.Empty then
Dim filterParam as New OdbcParameter("@category", OdbcType.Char,
255)
filterParam.Value = filterValue
objCmd.Parameters.Add(filterParam)
End If

objConn.Open()

'Finally, specify the DataSource and call DataBind()
dgTitles.DataSource =
objCmd.ExecuteReader(CommandBehavior.CloseConnection)
dgTitles.DataBind()

objConn.Close()
End Sub

Sub FilterData(sender as Object, e as CommandEventArgs)
BindData(e.CommandArgument)
End Sub

Sub DisplayViewingTypeMessage(filterValue as String)
If filterValue = String.Empty then
lblViewingMsg.Text = "You are viewing all categories published by
Sams"
Else
lblViewingMsg.Text = "You are viewing only this category - " &
filterValue
End If
End Sub

</script>

<form runat="server">
<asp:Button Text="View ALL category" runat="server"
CommandArgument="" OnCommand="FilterData" />
<asp:Button Text="View Computer" runat="server"
CommandArgument="Computer" OnCommand="FilterData" />
<asp:Button Text="View Fiction" runat="server"
CommandArgument="Fiction" OnCommand="FilterData" />
<asp:Button Text="View Non-Fiction" runat="server"
CommandArgument="NonFiction" OnCommand="FilterData" />

<p><asp:label id="lblViewingMsg" runat="server" /></p>

<asp:DataGrid runat="server" id="dgTitles"
autoGenerateColumns="false">
<HeaderStyle BackColor="#ddddff" HorizontalAlign="Center" />
<AlternatingItemStyle BackColor="#eeeee6" />
<Columns>
<asp:BoundColumn DataField="lname" HeaderText="Last Name" />
<asp:BoundColumn DataField="fname" HeaderText="First Name" />
<asp:BoundColumn DataField="category" HeaderText="Category" />
</Columns>
</asp:datagrid>
</form>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top