TwelvestoneBack End

VB.NET / SQL question


Sign in

  • Waiting for Godot ( 720 k posts )
    Just conversation.
  • Thunder Dome ( 23 k posts )
    Photoshop Tennis and Collabs.
  • Photography ( 4.8 k posts )
    For all you shutterbugs, sh...
  • Flash ( 18 k posts )
    ActionScripting to tweens, ...
  • Front End ( 5.8 k posts )
    general front end design an...
  • Back End ( 9.6 k posts )
    serverside scripting, progr...
  • Projects and Theory ( 12 k posts )
    This forum is for discussio...
  • FAQ ( 269 posts )
    All those nagging questions...
  • Design ( 17 k posts )
    graphics & all aspects of g...
  • Purgatory ( 3.6 k posts )
    12stone Jail, feel free to ...
JimmyTheGent
 
2010-06-16

I am creating a very basic application but its not working. I am pretty much it must be something stupid and I am looking at this too closely. So I am seeking a second option..

Here is my query:

Private callsQuery As String = "SELECT CL.CALL_NUMBER,UA.FIRST_NAME, UA.SURNAME, LOC.NAME as LOCATION, CL.SHORT_PROBLEM " _ & "FROM CL_CALL_LOGGING AS CL " _ & "INNER JOIN AR_User_Attributes AS UA " _ & "ON UA.Ref = CL.User_Ref " _ & "INNER JOIN AR_LOCATIONS as LOC " _ & "ON LOC.LOCATION_REF = UA.LOCATION_REF " _ & "WHERE CL.Resolve_Time IS NULL " _ & "AND CL.CURRENT_GROUP ='" & selectedGroup & "'"

selectedGroup is a public property pointing to a variable. before the SQL is run the variable is passed a value from the query string.

The problem is ti isnt working, when debugging, If I view the string it looks like this:

Private callsQuery As String = "SELECT CL.CALL_NUMBER,UA.FIRST_NAME, UA.SURNAME, LOC.NAME as LOCATION, CL.SHORT_PROBLEM " _ & "FROM CL_CALL_LOGGING AS CL " _ & "INNER JOIN AR_User_Attributes AS UA " _ & "ON UA.Ref = CL.User_Ref " _ & "INNER JOIN AR_LOCATIONS as LOC " _ & "ON LOC.LOCATION_REF = UA.LOCATION_REF " _ & "WHERE CL.Resolve_Time IS NULL " _ & "AND CL.CURRENT_GROUP = ''"

So it is not receiving the variable for some reason. I have also tried this with a viewstate variable with identical results. It shouldn't be an issue applying a variable in the string, maybe I have just been looking at this too long.

Hopefully a few suggestions from you guys and fresh eyes tomorrow can make this go away k

Cheers folks

digitalsteven
 
2010-06-17

Add a quickwatch or watch to your selectedGroup variable. You'll likely find that it's an empty string, which is where you need to make your fix.

Also, look up the StringBuilder class. Not only is it the most efficient way of building strings, it also make your code so much more readable. k

JimmyTheGent
 
2010-06-17

When debugging if I view the selectedGroup variable I can see the group number in there. I know about stringbuilder but I was under the impression it uses slightly more resources. This app is mainly going to be accessed via a mobile device so I am trying to keep it as resource starved as possible.

JimmyTheGent
 
2010-06-17

Here is the main body of code. The only bit not shown is a little validation method making sure the selectedGroup property isnt passing back any crap

I have discounted that as a problem by bypassing it on a few of the tests. So the query or code is to blame I suppose

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'Assign the selected group to the property/variable
    selectedGroup = Request.QueryString("Group")
    'If INVALID input 
    If selectedGroup = "00" Then
        'create error label
        Dim err As New Label
        err.Text = "Invalid group selected, Please go back and select your group from the dropdown list. <br /><br />"
        err.ForeColor = Drawing.Color.Red

        'create back button
        Dim lnkErr As New HyperLink
        lnkErr.Text = "Back"
        lnkErr.NavigateUrl = "~/index.aspx"

        pnMain.Controls.Add(err)
        pnMain.Controls.Add(lnkErr)
    Else
        'Create DB connection
        Dim dbAccess As New MobileInfra.DataAccess
        'Assign query
        dbAccess.CommandText = callsQuery
        Try
            Dim dsCalls As New Data.DataSet
            'assign datasource
            dsCalls = dbAccess.ExecuteDataSet

            'Create gridview to display
            Dim gvCalls As New GridView
            gvCalls.DataSource = dsCalls
            gvCalls.ID = "gvCalls"
            gvCalls.RowHeaderColumn = False
            gvCalls.AutoGenerateColumns = False
            gvCalls.GridLines = GridLines.Horizontal

            'Create Columns
            Dim CALL_NUMBER As New BoundField
            CALL_NUMBER.DataField = "CALL_NUMBER"
            Dim col1 As DataControlField = CALL_NUMBER

            Dim FIRST_NAME As New BoundField
            FIRST_NAME.DataField = "FIRST_NAME"
            Dim col2 As DataControlField = FIRST_NAME

            Dim SURNAME As New BoundField
            SURNAME.DataField = "SURNAME"
            Dim col3 As DataControlField = SURNAME

            Dim LOCATION As New BoundField
            LOCATION.DataField = "LOCATION"
            Dim col4 As DataControlField = LOCATION

            gvCalls.Columns.Add(CALL_NUMBER)
            gvCalls.Columns.Add(FIRST_NAME)
            gvCalls.Columns.Add(SURNAME)
            gvCalls.Columns.Add(LOCATION)

            'databind
            gvCalls.DataBind()

            pnMain.Controls.Add(gvCalls)
        Catch ex As Exception

        End Try


    End If

End Sub
digitalsteven
 
2010-06-17

Where is the selectedGroup declared? Does it fall out of scope at any point?

digitalsteven
 
2010-06-17

Just created a page with the function below and accessed: Default.aspx?Group=17

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    'Assign the selected group to the property/variable
    Dim selectedGroup As String = Request.QueryString("Group")

    'If INVALID input 
    If selectedGroup = "00" Then

        'create error label
        Response.Write("ERROR")

    Else

        'Create DB connection

        Dim callsQuery As String = "SELECT CL.CALL_NUMBER,UA.FIRST_NAME, UA.SURNAME, LOC.NAME as LOCATION, CL.SHORT_PROBLEM " _
        & "FROM CL_CALL_LOGGING AS CL " _
        & "INNER JOIN AR_User_Attributes AS UA " _
        & "ON UA.Ref = CL.User_Ref " _
        & "INNER JOIN AR_LOCATIONS as LOC " _
        & "ON LOC.LOCATION_REF = UA.LOCATION_REF " _
        & "WHERE CL.Resolve_Time IS NULL " _
    & "AND CL.CURRENT_GROUP ='" & selectedGroup & "'"

        Try

            Response.Write(callsQuery.ToString())

        Catch ex As Exception

        End Try

    End If

End Sub

And the output was: SELECT CL.CALL_NUMBER,UA.FIRST_NAME, UA.SURNAME, LOC.NAME as LOCATION, CL.SHORT_PROBLEM FROM CL_CALL_LOGGING AS CL INNER JOIN AR_User_Attributes AS UA ON UA.Ref = CL.User_Ref INNER JOIN AR_LOCATIONS as LOC ON LOC.LOCATION_REF = UA.LOCATION_REF WHERE CL.Resolve_Time IS NULL AND CL.CURRENT_GROUP ='17'

..as expected.

You haven't declared the selectedGroup variable within the function so at best guess the variable isn't in scope. Is it declared elsewhere with the wrong access modifier?

digitalsteven
 
2010-06-17

I'm heading out for the afternoon but I can perhaps assist more this evening. Just strip it down to bare bones and build it back up again.

For a quick test, change the variable name to queryGroup and limit the scope to that PageLoad method only. That will help identify/elimate scope issues.

Hope you get it sorted, mate. k

JimmyTheGent
 
2010-06-17

I originally had

private _group as string Private Property selectedGroup() as string etc...

Which as you rightly pointed out is out of scope. I have moved the declaration within the page_load and it is working. I knew it would be soemthing stupid!

Thanks very much indeed for having a look. I only do this kind of work on and off so I get rusty and little things can trip me up.

I owe you one. :beer:

digitalsteven
 
2010-06-17

No worries, mate.

I rarely develop nowadays too and it's often scope that scuppers me. If you're using a variable debugging generally gives you and "instance not set" error which usually identifies scope as the issue.

Glad it's sorted.

:beer:

jamiec
 
2010-06-18

Just a little pointer. Use parameterised queries instead of string mashing to build your SQL... your code is probabily wide open to SQL Injection attack.

JimmyTheGent
 
2010-06-18

I do validation on all input before it gets anywhere near the SQL. I am actually going to make them into stored procs though, to make use of the DB cache facility.

Sorry, you must be a member to post to a conversation. Either log in or sign up to get involved.
TwelvestoneBack End

VB.NET / SQL question