VB - SQL dropdown selection

andreas

New member
Joined
Jan 8, 2011
Messages
2
Programming Experience
Beginner
Hello guys,

I have two tables on a databsee, one contains University course name, the other one contains module names of each course.

I wanna use a dropdown list to view the courses available but i want them to be in a gridview. i'm struggling to find out how to link those 2 and i want the gridview to show the users a choise from the dropdown box.

Thanks for the help.

<body>
<form id="form1" runat="server">
<div>

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [coursenames]"></asp:SqlDataSource>
<br />
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"
SelectCommand="SELECT * FROM [ModuleNames]"></asp:SqlDataSource>
<br />
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="Course Name"
DataValueField="Course ID">
</asp:DropDownList>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="Module ID" DataSourceID="SqlDataSource2"
EmptyDataText="Choose a course">
<Columns>
<asp:BoundField DataField="Course ID" HeaderText="Course ID"
SortExpression="Course ID" />
<asp:BoundField DataField="Module ID" HeaderText="Module ID" ReadOnly="True"
SortExpression="Module ID" />
<asp:BoundField DataField="Module Name" HeaderText="Module Name"
SortExpression="Module Name" />
<asp:BoundField DataField="Core or Option" HeaderText="Core or Option"
SortExpression="Core or Option" />
<asp:BoundField DataField="Level" HeaderText="Level" SortExpression="Level" />
</Columns>
</asp:GridView>
<br />

</div>
</form>
</body>
</html>
 
That didn't seem to work:
This is my code, I've been trying loads of tutorials of different ways but none want to work...

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource2" DataTextField="Course_Name"
DataValueField="Course_ID" Height="16px">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:db2k5_dh2gccConnectionString %>"
SelectCommand="SELECT * FROM [Modules] WHERE [Course ID]=@Course ID ">
</asp:SqlDataSource>
<br />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Module_ID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="Course_ID" HeaderText="Course_ID"
SortExpression="Course_ID" />
<asp:BoundField DataField="Module_ID" HeaderText="Module_ID" ReadOnly="True"
SortExpression="Module_ID" />
<asp:BoundField DataField="Module_Name" HeaderText="Module_Name"
SortExpression="Module_Name" />
<asp:BoundField DataField="Core_or_Option" HeaderText="Core_or_Option"
SortExpression="Core_or_Option" />
<asp:BoundField DataField="Level" HeaderText="Level" SortExpression="Level" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:db2k5_dh2gccConnectionString %>"
SelectCommand="SELECT [Course ID] AS Course_ID, [Module ID] AS Module_ID, [Module Name] AS Module_Name, [Core or Option] AS Core_or_Option, [Level] FROM [Modules]">
</asp:SqlDataSource>

</div>
</form>
</body>
</html>


The error I got was:


Must declare the scalar variable "@Course".



Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Must declare the scalar variable "@Course".

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[SqlException (0x80131904): Must declare the scalar variable "@Course".]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +1950890
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +4846875
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +194
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2392
System.Data.SqlClient.SqlDataReader.ConsumeMetaData() +33
System.Data.SqlClient.SqlDataReader.get_MetaData() +83
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +297
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +954
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +141
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +12
System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) +10
System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +130
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +287
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable) +92
System.Web.UI.WebControls.SqlDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +1297
System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +95
System.Web.UI.WebControls.ListControl.PerformSelect() +34
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgs e) +22
System.Web.UI.WebControls.ListControl.OnPreRender(EventArgs e) +18
System.Web.UI.Control.PreRenderRecursiveInternal() +80
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842




--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.3603; ASP.NET Version:2.0.50727.3614
 
I am afraid you have not understood my suggestion.

I was talking about the GridView's SqlDataSource.

VB.NET:
<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
    ConnectionString="<%$ ConnectionStrings:db2k5_dh2gccConnectionString %>" 
    SelectCommand="SELECT [Course ID] AS Course_ID, [Module ID] AS Module_ID, [Module Name] AS Module_Name, [Core or Option] AS Core_or_Option, [Level] FROM [Modules] WHERE [Course ID]=@Course">
    <SelectParameters>
        <asp:ControlParameter ControlID="DropDownList1" PropertyName="SelectedValue" Name="@Course" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

And of course restore the SqlDataSource1 :)
 
Back
Top