Wednesday, March 21, 2012

Multiple DropDowns error

Hi:

I have two drop downs bound to the same data source.. These dropdowns are automatically populated from a database. When I click the button I get some sort of strange query error.

Not sure what I'm doing wrong here.

<%@.ImportNamespace="System.Data" %>

<%@.ImportNamespace="System.Data.SQLClient" %>

<scriptlanguage="VB"runat="server">

Dim sOrderbyasString

Dim sDirectionasString

Dim MySQLAsString

Dim MySQL1AsString

Dim sSubjectAsString

Dim sCategoryAsString

Sub Page_Load(ByVal SourceAsObject,ByVal EAs EventArgs)

IfNot Page.IsPostBackThen

Dim strConnAsString ="server=GAALP-DT-UHABB2\CFW;uid=sa;pwd=removed;database=NetG"

Dim MySQLAsString ="Select DISTINCT [Subject] from dbo_v_netG_courses"

Dim MySQL1AsString ="Select DISTINCT [Category] from dbo_v_netG_courses"

Dim MyConnAsNew SqlConnection(strConn)

Dim objDRAs SqlDataReader

Dim CmdAsNew SqlCommand(MySQL, MyConn)

Dim Cmd1AsNew SqlCommand(MySQL1, MyConn)

MyConn.Open()

objDR = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

ddl.DataSource = objDR

ddl.DataValueField ="Subject"

ddl.DataTextField ="Subject"

ddl.DataBind()

MyConn.Close()

MyConn.Open()

ddlDir.DataSource = Cmd1.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

ddlDir.DataValueField ="Category"

ddlDir.DataTextField ="Category"

ddlDir.DataBind()

MyConn.Close()

ddl.Items.Insert(0,"-- Choose --")

ddlDir.Items.Insert(0,"-- Choose --")

EndIf

'ddl.Items.Insert(0, "-- Choose --")

EndSub

' Sub Page_Change(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)

' MyDataGrid.CurrentPageIndex = e.NewPageIndex

' BindData()

'Sub GridOne(ByVal Source As Object, ByVal E As EventArgs)

' MyDataGrid.CurrentPageIndex = 0

'End Sub

'Sub GetData(ByVal Source As Object, ByVal E As EventArgs)

' BindData()

' End Sub

Sub BindData(ByVal SourceAsObject,ByVal EAs EventArgs)

sSubject = ddlDir.SelectedItem.Text

sCategory = ddlDir.SelectedItem.Value

Dim strConnAsString ="server=GAALP-DT-UHABB2\CFW;uid=sa;pwd=removed;database=NetG"

If sSubject =""And sCategory =""Then

MySQL ="Select * from dbo_v_netG_courses"

Else ( THIS LINE IS GIVING ME THE ERROR)

MySQL ="Select * from dbo_v_netG_courses where [Subject] = & sSubject"

EndIf

Dim MyConnAsNew SqlConnection(strConn)

Dim dsAs DataSet =New DataSet()

Dim CmdAsNew SqlDataAdapter(MySQL, MyConn)

Cmd.Fill(ds,"dbo_v_netG_courses")

MyDataGrid.DataSource = ds.Tables("dbo_v_netG_courses").DefaultView

MyDataGrid.DataBind()

EndSub

</script>

<html>

<head>

<metaname="GENERATOR"Content="ASP Express 3.0">

<title>Ad Hoc Sorting with a DataGrid</title>

</head>

<body>

<Formid="form1"runat="server">

<table>

<tr>

<tdalign="Left"valign="Top"><b><i>View Employee Data</i></b></td>

<tdalign="right"valign="Top">

Subject:<asp:dropdownlistid="ddl"runat="server">

</asp:dropdownlist>

Category:<asp:dropdownlistid="ddlDir"runat="server">

</asp:dropdownlist><br/>

<br/>

<asp:Buttonid="btn1"Text="View Records"onclick="BindData"runat="server"/><br/>

</td>

</tr>

<tr>

<tdalign="Left"valign="Top"Colspan="2">

<asp:Datagridrunat="server"

Id="MyDataGrid"

GridLines="Both"

cellpadding="0"

cellspacing="0"

Headerstyle-BackColor="#8080C0"

Headerstyle-Font-Bold="True"

Headerstyle-Font-Size="12"

BackColor="#8080FF"

Font-Size="10"

AlternatingItemStyle-BackColor="#EFEFEF"

AlternatingItemStyle-Font-Size="10"

BorderColor="Black">

</asp:DataGrid><br> </td>

</tr>

</table>

</form>

</body>

</html>

This code works now.. One question though.. How can I return all values by default when the page loads..

Thanks

Working code in VB for multiple dropdown selections and then button click to submit values

<%@.ImportNamespace="System.Data" %>

<%@.ImportNamespace="System.Data.SQLClient" %>

<scriptlanguage="VB"runat="server">

Dim sOrderbyasString

Dim sDirectionasString

Dim MySQLAsString

Dim MySQL1AsString

Dim sSubjectAsString

Dim sCategoryAsString

Sub Page_Load(ByVal SourceAsObject,ByVal EAs EventArgs)

IfNot Page.IsPostBackThen

Dim strConnAsString ="server=LAPTOP;uid=sa;pwd=sa;database=NetG"

Dim MySQLAsString ="Select DISTINCT [Subject] from dbo_v_netG_courses"

' Dim MySQL1 As String = "Select DISTINCT [Category] from dbo_v_netG_courses"

Dim MyConnAsNew SqlConnection(strConn)

Dim objDRAs SqlDataReader

Dim CmdAsNew SqlCommand(MySQL, MyConn)

' Dim Cmd1 As New SqlCommand(MySQL1, MyConn)

MyConn.Open()

objDR = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

ddl.DataSource = objDR

ddl.DataValueField ="Subject"

ddl.DataTextField ="Subject"

ddl.DataBind()

MyConn.Close()

' MyConn.Open()

' ddlDir.DataSource = Cmd1.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

'ddlDir.DataValueField = "Category"

' ddlDir.DataTextField = "Category"

' ddlDir.DataBind()

'MyConn.Close()

ddl.Items.Insert(0,"-- Choose --")

'ddlDir.Items.Insert(0, "-- Choose --")

EndIf

'ddl.Items.Insert(0, "-- Choose --")

EndSub

Sub fillModel(ByVal SourceAsObject,ByVal EAs EventArgs)

Dim strConnAsString ="server=LAPTOP;uid=sa;pwd=sa;database=NetG"

If ddl.SelectedItem.Text <>"-- Choose --"Then

sSubject = ddl.SelectedItem.Value

Dim MySQL1AsString ="Select DISTINCT Category from dbo_v_netG_courses where [Subject]=" &"'" & sSubject &"'"

Dim MyConnAsNew SqlConnection(strConn)

'Dim objDR As SqlDataReader

Dim Cmd1AsNew SqlCommand(MySQL1, MyConn)

MyConn.Open()

ddlDir.DataSource = Cmd1.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

ddlDir.DataValueField ="Category"

ddlDir.DataTextField ="Category"

ddlDir.DataBind()

' ddl2.selectedindex=0

' tdmodel.visible = "true"

'lit1.text = "<b>Vehicle Type</b>: " & ddl1.selectedItem.text

' ddl2.items.insert(0, "-- Choose --")

' tdStyle.visible = "false"

MyConn.Close()

EndIf

ddlDir.Items.Insert(0,"-- Choose --")

EndSub

' Sub Page_Change(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)

' MyDataGrid.CurrentPageIndex = e.NewPageIndex

' BindData()

'Sub GridOne(ByVal Source As Object, ByVal E As EventArgs)

' MyDataGrid.CurrentPageIndex = 0

'End Sub

'Sub GetData(ByVal Source As Object, ByVal E As EventArgs)

' BindData()

' End Sub

Sub BindData(ByVal SourceAsObject,ByVal EAs EventArgs)

sSubject = ddl.SelectedItem.Value

sCategory = ddlDir.SelectedItem.Value

Dim strConnAsString ="server=LAPTOP;uid=sa;pwd=sa;database=NetG"

If sSubject =""And sCategory =""Then

MySQL ="Select * from dbo_v_netG_courses"

ElseIf sSubject ="-- Choose --"And sCategory =""Then

MySQL ="Select * from dbo_v_netG_courses"

'Page.IsPostBack = True

Else

MySQL ="Select * from dbo_v_netG_courses where [Subject]=" &"'" & sSubject &"'" &" and [Category] =" &"'" & sCategory &"'"

Dim MyConnAsNew SqlConnection(strConn)

Dim dsAs DataSet =New DataSet()

Dim CmdAsNew SqlDataAdapter(MySQL, MyConn)

Cmd.Fill(ds,"dbo_v_netG_courses")

MyDataGrid.DataSource = ds.Tables("dbo_v_netG_courses").DefaultView

MyDataGrid.DataBind()

EndIf

EndSub

</script>

<html>

<head>

<metaname="GENERATOR"Content="ASP Express 3.0">

<title>Ad Hoc Sorting with a DataGrid</title>

</head>

<body>

<Formid="form1"runat="server">

<table>

<tr>

<tdalign="Left"valign="Top"><b><i>View Employee Data</i></b></td>

<tdalign="right"valign="Top">

Subject:<asp:dropdownlistid="ddl"

runat="server"

onselectedindexchanged="fillModel"AutoPostBack="True"

>

</asp:dropdownlist>

Category:<asp:dropdownlistid="ddlDir"runat="server"DataTextField="Model">

</asp:dropdownlist><br/>

<br/>

<asp:Buttonid="btn1"Text="View Records"onclick="BindData"runat="server"/><br/>

</td>

</tr>

<tr>

<tdalign="Left"valign="Top"Colspan="2">

<asp:Datagridrunat="server"

Id="MyDataGrid"

cellpadding="0"

Headerstyle-BackColor="#8080C0"

Headerstyle-Font-Bold="True"

Headerstyle-Font-Size="12"

BackColor="#8080FF"

Font-Size="10pt"

AlternatingItemStyle-BackColor="#EFEFEF"

AlternatingItemStyle-Font-Size="10"

BorderColor="Black"AllowSorting="True">

<AlternatingItemStyleBackColor="#EFEFEF"Font-Size="10pt"/>

<HeaderStyleBackColor="#8080C0"Font-Bold="True"Font-Size="12pt"/>

</asp:DataGrid><br> </td>

</tr>

</table>

</form>

</body>

</html>

|||I assume this is an .NET 1.1 application?

No comments:

Post a Comment