Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Invalid Column Name weird--- 1

Status
Not open for further replies.

tsp1lrk72

IS-IT--Management
Feb 25, 2008
100
US
Hello,

I have some code:

Code:
  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not Page.IsPostBack Then
        End If
        'Define connection to read the data 
        Dim myConn As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
        Dim MyCmd As New SqlCommand("SELECT EmployeeID, Password From tblVendorLogin Where EmployeeID =  " & Request.Cookies("SKUPM")("EmpID"), myConn)
        MyCmd.CommandType = CommandType.Text
        MyCmd.Parameters.Add(New SqlParameter("@EmployeeID", SqlDbType.VarChar, 7)).Value = Convert.ToString(EmployeeID)
        myConn.Open()
        Dim Result As SqlDataReader = MyCmd.ExecuteReader(CommandBehavior.CloseConnection)
        Result.Read()
        If Not Result.IsDBNull(0) Then
            EmployeeID.Text = Result("EmployeeID").ToString()
        End If
        If Not Result.IsDBNull(0) Then
            txtCurrent.Text = Result("Password").ToString()
        End If

    End Sub

I keep getting: Invalid column name 'lisa'.

lisa is one of the Employee ID's... the minute I delete that from my table and deal only with numbers, for instance, 1111111 as the Employee ID, my code is fine-- It's a SQL thing I think, (I posted there too) but I'm thinking it's the way I'm calling the Employee ID- is Char okay to use? Why is it as soon as I have alphanumeric characters I get that message????

Thanks!
 
You are concatenating strings to build your CommandText, but then you are using a Named Parameter with the Command. So, to fix your string, make it say something like:

"SELECT EmployeeID, Password From tblVendorLogin Where EmployeeID = @EmployeeID
 
In your SQL, a text are char value needs to be enclosed in single quotes. This is not the case for numeric values.

So

Dim MyCmd As New SqlCommand("SELECT EmployeeID, Password From tblVendorLogin Where EmployeeID = " & Request.Cookies("SKUPM")("EmpID"), myConn)

will work fine for numeric values but you will need

Dim MyCmd As New SqlCommand("SELECT EmployeeID, Password From tblVendorLogin Where EmployeeID = '" & Request.Cookies("SKUPM")("EmpID") & "'", myConn)

when using text. NOTE the single quotes addition.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Hi mstrmage1768! That was it... WOW--- little quotes will get ya everytime! Thanks so much... that did the trick!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top