Results 1 to 11 of 11

Thread: Help with sql code

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2007
    Posts
    105

    Help with sql code

    I am using the following code...

    vb Code:
    1. Public Function getdatafi(ByVal querystring As String)
    2.         'MsgBox(querystring)
    3.         Using connection As New SqlConnection(GetConnectionStringfi)
    4.             Dim command As New SqlCommand(querystring, connection)
    5.             connection.Open()
    6.  
    7.             Dim reader As SqlDataReader = command.ExecuteReader
    8.  
    9.             While reader.Read
    10.                 Return reader.GetValue(0)
    11.             End While
    12.  
    13.             reader.Close()
    14.  
    15.         End Using
    16.  
    17.     End Function

    vb Code:
    1. tempdimid = getdatafi("SELECT dim_id from dim_info where inspection_id = " & inspectionid & _
    2.             " and dim_name = '" & dimname(tempnumber) & "'")

    I put a break in and viewed the sql code. It is the correct code but it returns 0. If I run the exact sql code that the program creates in the SQL query anylizer, it returns the correct number. I am using the getdatafi function in other places and it works as it should. Does anyone know why this is not working?

  2. #2
    Lively Member
    Join Date
    Jul 2008
    Posts
    89

    Re: Help with sql code

    try rdr.Item(0) instead and see if that works?

  3. #3
    Lively Member
    Join Date
    Jul 2008
    Posts
    89

    Re: Help with sql code

    actually, you said returns the correct value, meaning the query returns only one row? if so then, try:

    Code:
    reader = command.ExecuteReader(CommandBehavior.SingleRow)
    If reader.Read Then
    return reader.item(0)
    endif

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2007
    Posts
    105

    Re: Help with sql code

    Thanks. I will not be able to try it until tomorrow. I just don't understand why it won't work. I use this function in several other of my programs and I never had any problems with it. I even moved it to the form_load sub and it still did not work. Any idea why? The only thing I can think of is that the function is not being called but I don't know why it would be doing that and there are no errors either.

  5. #5
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Help with sql code

    A couple of things that I see that should be cleaned up. They might not fix the issue, but they should be looked at
    1.) Your function does not have a return type. Turn on Option Strict, it will benefit you in the long run.
    2.) If your function returns a row, reader.Close() does not get called.
    3.) You shouldn't build SQL queries by appending strings together. Look into using parameterized queries. This will help protect you from issues with single quotes in your queries among other things.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Nov 2007
    Posts
    105

    Re: Help with sql code

    I guess I just got lazy....I will turn on option strict and do some research on the parameterized queries. I have not worked on the program for a couple of months, so I will look into it tomorrow.

    I do not understand what you mean in 2. I thought that after it was done reading, it closed the connetion. Does it not finish the function if it returns a row?

  7. #7
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Help with sql code

    After you do a return, it exits the function, so your .close does not get called.

    An example like this, shows what I mean:

    Code:
       Private Function RunMe() As String
            Return "Hello"
            MessageBox.Show("After hello")
        End Function

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Nov 2007
    Posts
    105

    Re: Help with sql code

    So how should I fix it? I don't want to have 100 connections open, but I assume they will time out and close. Do I have to call reader.close from the sub that calls the function?

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Nov 2007
    Posts
    105

    Re: Help with sql code

    Quote Originally Posted by asnider
    actually, you said returns the correct value, meaning the query returns only one row? if so then, try:

    Code:
    reader = command.ExecuteReader(CommandBehavior.SingleRow)
    If reader.Read Then
    return reader.item(0)
    endif
    This did not help...It still returns 0

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Nov 2007
    Posts
    105

    Re: Help with sql code

    I turned on option strict and fixed all the errors. The query now returns the correct result!

    But I have run into another problem...

    vb.net Code:
    1. Private Sub dimdata_UserAddedRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles dimdata.UserAddedRow
    2.         Dim ThisDataTableRow As Data.DataRow
    3.  
    4.         ThisDataTableRow = CType(e.Row.DataBoundItem, DataRow)
    5.  
    6.         'ThisDataTableRow.Item("ID") = recordcount
    7.         [COLOR="Red"]ThisDataTableRow.Item("DIM_ID") = tempdimid[/COLOR]
    8.  
    9.     End Sub

    I am using this code to add extra data to the datagridview but the code in red gives the errror:
    Object reference not set to an instance of an object. The thisdatatablerow is nothing. Any ideas?
    Last edited by dave18; Jul 18th, 2008 at 10:01 AM.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Nov 2007
    Posts
    105

    Re: Help with sql code

    I am still having problems with this....does anyone have any ideas?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width