Mhm. This is the VB.net code behind a webpage I'm working with, I've also provided the ASP.net for the frontpage as well. This maybe really simple and I've missed it but everytime I run this code (below) I keep on getting the same error "Invalid column name, Skyrail" where 'Skyrail' is the data in one of the field names (the user alias field). So here's my VB.net code:

'Import the required things
Imports System.Data, System.Data.SqlClient
Class _Default
    Inherits System.Web.UI.Page
    'Set the variables
    Dim SQLConnectionString As String
    Dim SQLConnectionStatus As String = "Closed"
    Dim SQLConnection As New SqlConnection()
    Dim SQLCommand As New SqlCommand()
    Dim SQLData As SqlDataReader
    'Function used to open up the SQL connection
    Function sqlDBConnection() As Object
        'Create the Connection String
        SQLConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Owner\My Documents\Visual Studio 2008\WebSites\Overland\App_Data\Overland.mdf;Integrated Security=True;User Instance=True"
        'Set the connection string into the SQL connection
        SQLConnection.ConnectionString = SQLConnectionString
        'Open up the connection
        SQLConnection.Open()
        'Check to see if it was succesful
        If SQLConnection.State = ConnectionState.Open Then
            'If so tell them so
            ValidationWarning.Text = "Success!"
            'Set the connection status to open
            SQLConnectionStatus = "Open"
        End If
        'Return the connection status
        Return SQLConnectionStatus
    End Function
    'Function used to close the SQL connection
    Function sqlDBConnectionClose() As Object
        'Close the connection
        SQLConnection.Close()
        'Check to see if it's still open
        If SQLConnection.State = ConnectionState.Open Then
            'If it is tell them that the database closing failed
            ValidationWarning.Text = "Failed closing database!"
            'Keep the connection status as open
            SQLConnectionStatus = "Open"
        Else
            'Else the connection isn't open so tell them
            ValidationWarning.Text = "Connection Closed"
            'Set the connections status as closed
            SQLConnectionStatus = "Closed"
        End If
        'Return the connection status
        Return SQLConnectionStatus
    End Function

    'The sub procedure that validates the boxes
    Protected Sub ValidateInput_Command(ByVal sender As Object, ByVal e As System.EventArgs) Handles ValidateInput.Command
        'Check to see if any of the fields are empty
        If UserAlias.Text.Length = 0 Or UserName.Text.Length = 0 Or UserPass.Text.Length = 0 Then
            'Tell them that they've left a field or two empty
            ValidationWarning.Text = "You left a field blank!"
            'Ensure that the two passwords are equal
        ElseIf UserPass.Text <> UserPassCheck.Text Then
            'If they don't tell them that
            ValidationWarning.Text = "You're passwords didn't match"
            'Check to see if the password length is above 6
        ElseIf UserPass.Text.Length < 6 Then
            'If not tell them that it's too short
            ValidationWarning.Text = "You're password is too short!"
        Else
            'We're going to try and open a database connection
            Try
                'Try and open it
                sqlDBConnection()
                'Catch any exceptions
            Catch exception As System.Exception
                'Tell them that it failed and for my use why
                ValidationWarning.Text = "Could not connect to the database" & exception.Message
                'MsgBox(exception.Message, MsgBoxStyle.Critical, "Exception Error")
            End Try
            'If the connection was succesful
            If SQLConnectionStatus = "Open" Then
                'Then we're going to check if the user already exists - doesn't actually work...at all
                Dim SQLCheckForUser As String = "SELECT * FROM Users WHERE UserAlias = " & UserAlias.Text
                SQLCommand.Connection = SQLConnection
                SQLCommand.CommandText = SQLCheckForUser
                SQLCommand.ExecuteNonQuery()
                SQLData = SQLCommand.ExecuteReader
                If SQLData.Read() Then
                    ValidationWarning.Text = "Found the user already?"
                End If
            End If
        End If
    End Sub
    'The sub procedure that closes the connection
    Protected Sub CloseConnection_Command(ByVal sender As Object, ByVal e As System.EventArgs) Handles CloseConnection.Command
        'The function that closes the connection
        sqlDBConnectionClose()
    End Sub
End Class

And here is my ASP.net page (.aspx, minus the CSS_

<%@ 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>Overland</title>
    <style type="text/css">
        ...
    </style>
</head>
<body>
    <form runat="server" id="HomePage">
    
        <div class="Surround">
        
            <div class="Navigation">
            
            </div>
            
            <div class="Content">
            
                <h1>Overland</h1>
                <fieldset>
                    <label for="UserAlias">User Alias:</label>
                    <p><asp:TextBox MaxLength="20" ID="UserAlias" runat="server" class="textbox" /></p>
                    <label for="UserName">Real Name:</label>
                    <p><asp:TextBox MaxLength="30" ID="UserName" runat="server" class="textbox"  /></p>
                    <label for="UserPass">Password:</label>
                    <p><asp:TextBox MaxLength="50" TextMode="Password" ID="UserPass" runat="server" class="textbox" /></p>
                    <label for="UserPassCheck">Password (again):</label>
                    <p><asp:TextBox MaxLength="50" TextMode="Password" ID="UserPassCheck" runat="server" class="textbox" /></p>
                    <asp:Button Text="Validate" ID="ValidateInput" runat="server"/>
                    <asp:Button Text="Close Connection" ID="CloseConnection" runat="server" />
                    <p><asp:Label ID="ValidationWarning" runat="server" Text="" /></p>
                </fieldset>
            
            </div>
            
        </div>
    </form>
</body>
</html>

It's a simple page and currently I'm just messing around/testing. What I'm trying to do is a mockup registration page for a simple user system, it's breaking in the VB.net where I am trying to check whether the user exists already by using a SELECT query: Dim SQLCheckForUser As String = "SELECT * FROM Users WHERE UserAlias = " & UserAlias.Text .

Another problem with this code is that once I've fixed the SELECT query to work how can I check if it actually found anybody?

Thanks for your time.

Recommended Answers

All 2 Replies

Line 78, your query:

Dim SQLCheckForUser As String = "SELECT * FROM Users WHERE UserAlias = " & UserAlias.Text

THe problem with this is that you are not putting it within quotes. ANd because you're not, it's recognizing it as a column, which doesn't exist. Two ways, which I recommend the first very strongly:

1. Put it into a parameter and attach it.
2. Put quotes around your existing portion.

1.

Dim SQLCheckForUser As String = "SELECT * FROM Users WHERE UserAlias = @Alias"
SQLCommand.Parameters.AddWithValue("@Alias", Trim(UserAlias.Text))
2.

Dim SQLCheckForUser As String = "SELECT * FROM Users WHERE UserAlias = '" & Trim(UserAlias.Text) & "'"

Thank you very much for your answer and I am ashamed at my such late reply (I have been in Germany for the past week and haven't been able to access a computer properly).

Your solution worked and so now I can continue to learn more about VB.net and ASP.net, thanks once again :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.