Click here to Skip to main content
15,881,027 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
I'm sure I must be missing something pretty basic or simple here, but I just can't see it !!!

I have a SQL-Server Table called Locations, with 2 Columns, LocationKey & LocationPath.

I have 1 record in that Table, the LocationKey = LocationDespatchSchedules & the LocationPath = D:\

My access to that table is via the following code :

VB
Public Sub Get_Location(myFile As String, myType As String)

    Dim mySqlConn As SqlConnection = New SqlConnection(myDB)
    Dim mySqlString As String = "SELECT LocationPath FROM Locations WHERE LocationKey = 'Location" & myFile.Replace(" ", "") & "'"
    Dim mySqlCmd As SqlCommand
    Dim myObject As Object

    Try
        mySqlConn.Open()
        mySqlCmd = New SqlCommand(mySqlString, mySqlConn)
        myObject = mySqlCmd.ExecuteScalar()
        If myObject IsNot Nothing Then
            myFileLocation = myObject.ToString
        Else
            MessageBox.Show("Error - No Record Found")
        End If
    Catch ex As Exception
        myAbortCode = "2"
        MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        MessageBox.Show("Program Aborted") 'Store error details in file for copying
        Exit Sub
    Finally
        mySqlConn.Close()
    End Try

End Sub


The value passed into myFile in this run is ... Despatch Schedules

The value of mySqlCommand in the execution line is ...
SQL
SELECT LocationPath FROM Locations WHERE LocationKey = 'LocationDespatchSchedules'


As far as I can see, that is a match to the Key and it should therefore drop into the myFileLocation = myObject.ToString line, but it doesn't, it returns Nothing and always falls through to the Error message.

What have I done wrong ?!?
Posted
Updated 12-Feb-14 9:31am
v2
Comments
Vedat Ozan Oner 12-Feb-14 16:14pm    
to do:
1- ensure your query string what you want as 'Console.Writeline(mySqlString)'
2- copy this string and run it directly from SSMS and see what result is(do you expect that result?)
mgoad99 12-Feb-14 16:17pm    
try running the value of the mySqlCommand directly in SQL Server Management Studio.

Does that return a record? that can help to start narrow down what the problem is.

1 solution

The problem was caused because the Locations Table hadn't been copied into the DataSet !
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900