Results 1 to 3 of 3

Thread: [2008] What is wrong with this code? (SQL Command)

  1. #1

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    [2008] What is wrong with this code? (SQL Command)

    vb.net Code:
    1. Dim sqlCon As New SqlConnection("Data Source=ICYCULYR\SQLEXPRESS;Initial Catalog=test_kcDB;Integrated Security=True;Pooling=False")
    2.         sqlCon.Open()
    3.         Dim sqlCommand As New SqlCommand()
    4.         sqlCommand.Connection = sqlCon
    5.         Dim returnValue As Object = "Nothing"
    6.         Select Case whichPreference
    7.             Case PreferencesEnum.dblClickMenu
    8.                 sqlCommand.CommandText = ("SELECT preferences_value FROM dataTable")
    9.                 Dim sqlReader As SqlDataReader = sqlCommand.ExecuteReader()
    10.                 Dim tmpInt As Integer = 0
    11.                 While sqlReader.Read()
    12.                     If tmpInt = whichPreference Then
    13.                         returnValue = sqlReader.GetValue(tmpInt)
    14.                         Exit While
    15.                     End If
    16.                     tmpInt += 1
    17.                 End While
    18.         End Select
    19.         sqlCon.Close()
    the problem line is 'returnValue = sqlReader.GetValue(tmpInt)',
    my value is DBNULL, but should return the first value of my column, which would be 'true'.

    I assume sqlReader is reading the rows from my column 'preferences_value', so I don't see why it isn't working?
    sqlReader.GetValue(tmpInt) (tmpInt being 0, as whichPreference is 0)
    it should return the first row.

    If anyone could help me with this?

    Thanks

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2008] What is wrong with this code? (SQL Command)

    I am unsure what you are trying to achieve with the tmpInt variable.
    The parameter for SqlDataReader.GetValue is the zero-based column index (as you can learn from MSDN). This will always be zero in your example since you're only selecting one column.

  3. #3

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2008] What is wrong with this code? (SQL Command)

    I see, thanks, I have several preferences stored in one column, and tmpInt tells me which one.

    Thanks

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