Ask a Question related to ASP.NET Data Grid Control, Design and Development.
-
Samson #1
Dynamic Sql not working for DB2 (from book: Asp.net Data Web Controls - Listing 6.2)
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.CloseConnecti on)
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>
Samson Guest
-
Looking for a book on writing ASP.NET Controls with VB.NET
I am an ASP.NET developer who would like to start learning about writing ASP.NET custom controls using VB.NET. I already have a reasonable amount of... -
dreamweaver 2004 dynamic app book
Hi, I'm new in this of dynamic applications, just started learning with macromedia book. I followed the tutorials for the creation of the newland... -
Good book on Building Controls
Hey, We're looking to purchase some books for the developers. I'm wondering if anyone can recomend some good reads for building controls. Good... -
MsPress book ASP.NET Server Controls and Components DatePicker?
Has anyone had any trouble getting the datepicker in the MS Press book ASP.NET Server Controls and Components working under .NET Framework 1.1? I... -
Can This be done? Dynamic jump menus listing folder contents!
Hello all, We have: MS Advanced server2000 ASP MS SQL latest stable: PHP Active Perl



Reply With Quote

