Invalid Column Name, System.Data.SqlClient.SqlException, VB.Net

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I can get my Where clause to work when using the interger divisionID, but not the string division. Any ideas what I need to do? The problem is WHERE Players.division = ddlDivisionSelection.SelectedValue. The ddlDivisionSelection.SelectedValue
shows the right string but I can not get the Players.division to work right????
strSelectCmd "SELECT playerID, Players.division, teamID, playerName, jerseyNumber, height, weight, playerTitle,playerAddress, playerCity, playerState, playerZip, playerContactPhone, playerEmail, position FROM Players WHERE Players.division
= AdultALeague And teamID = 2" String
Thanks! Mike
<pre class="prettyprint lang-vb Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
The Page is accessed for the first time.
If Not IsPostBack Then
Enable the GridView paging option and
specify the page size.
gvPlayers.AllowPaging = True
gvPlayers.PageSize = 15

Enable the GridView sorting option.
gvPlayers.AllowSorting = True

Initialize the sorting expression.
ViewState("SortExpression") = "playerID ASC"
Dim conn As SqlConnection
Dim divisionComm As SqlCommand
Dim teamComm As SqlCommand
Dim reader As SqlDataReader
Dim connectionString As String = _
ConfigurationManager.ConnectionStrings( _
"hysdb").ConnectionString
conn = New SqlConnection(connectionString)
divisionComm = New SqlCommand( _
"SELECT divisionID, division FROM Division ORDER BY division", conn)
teamComm = New SqlCommand( _
"SELECT teamID, teamName FROM TeamInfo ORDER BY teamName", conn)
Try
conn.Open()
reader = divisionComm.ExecuteReader()
ddlDivisionSelection.DataSource = reader
ddlDivisionSelection.DataValueField = "division"
ddlDivisionSelection.DataTextField = "division"
ddlDivisionSelection.DataBind()
reader.Close()
reader = teamComm.ExecuteReader()
ddlTeamSelection.DataSource = reader
ddlTeamSelection.DataValueField = "teamID"
ddlTeamSelection.DataTextField = "teamName"
ddlTeamSelection.DataBind()
reader.Close()
Finally
conn.Close()
End Try
End If
End Sub
Private Sub BindGridView()

Get the connection string from Web.config with a
Using statement to explicitly dispose the object in the code
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("hysdb").ToString())
Create a DataSet object.
Dim dsPlayers As New DataSet()
Dim strSelectCmd As String = String.Empty
Create a SELECT query.

strSelectCmd = "SELECT playerID, Players.division, teamID, playerName, jerseyNumber, height, weight, playerTitle," & _
"playerAddress, playerCity, playerState, " & _
"playerZip, playerContactPhone, playerEmail, position " & _
"FROM Players " & _
"WHERE Players.division = " & ddlDivisionSelection.SelectedValue & " And teamID = " & _
ddlTeamSelection.SelectedValue

Open the connection
conn.Open()

Dim da As New SqlDataAdapter(strSelectCmd, conn)

Fill a DataTable with a reference to the DataSet and the name of the DataSet table, Players
da.Fill(dsPlayers, "Players")

Get the DataView from Players DataTable.
Dim dvplayers As DataView = dsPlayers.Tables("Players").DefaultView

Set the sort column and sort order.
dvplayers.Sort = ViewState("SortExpression").ToString()

Bind the GridView control.
gvPlayers.DataSource = dvplayers
gvPlayers.DataBind()
End Using
End Sub[/code]
<br/>
<span style="font-size:small <span style="font-size:small <br/>

View the full article
 
Back
Top