0

I'd like to get some values from nvarchar column (SQL Server database). My code looks like below:

Dim value1 As String

Dim con2 As New System.Data.SqlClient.SqlConnection
con2.ConnectionString = "data source=AAA;initial catalog=DWH;integrated security=SSPI;"

con2.Open()

Dim retrieveGSFdata As New System.Data.SqlClient.SqlCommand
retrieveGSFdata.Connection = con2
retrieveGSFdata.CommandText = "SELECT * FROM table1"

Dim myGSFdataReader as System.Data.SqlClient.SqlDataReader
myGSFdataReader = retrieveGSFdata.ExecuteReader()

Dim iteratorGSF As Integer
iteratorGSF = 0

while(myGSFdataReader.read())

    Select Case iteratorGSF
      case 0:
        if myGSFdataReader.IsDBNull(4)
           value1 = 0
        else
           value1 = myGSFdataReader.GetString(4)
        end if
    End Select  

    iteratorGSF = iteratorGSF + 1

End while

myGSFdataReader.Close()
con2.Close()

It works fine only when there are numeric values in the column. But in this column are stored variable characters values and this types of values are not being displayed on the page. I don't know what is the reason.

I've tried to use getValue.ToString instead of getString but it doesn't work either.

I'd be grateful for your help.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Konrad Z.
  • 1,592
  • 4
  • 20
  • 34
  • 1
    You say *SQL* (Structured Query Language), but you really mean **SQL Server** (the MIcrosoft database product) - right? – marc_s Jun 11 '12 at 11:54
  • 2
    Also: can you tell us what columns that `table1` has (name and their datatype) and show us how you're trying to read out a `nvarchar` column? Also: you say *it did not work* - did you get an error? If so: **WHAT** exact error did you get ? – marc_s Jun 11 '12 at 11:56
  • 1
    @marc_s I think that is safe to assume since he's using the SqlClient namespace, but good point. – Steven Doggart Jun 11 '12 at 11:56
  • In what way does it not work? Do you get exceptions, or null values, or wrong values, or what? What happened when you called ToString instead of GetString? – Steven Doggart Jun 11 '12 at 11:58
  • can you give some sample data and your table structure – vpv Jun 11 '12 at 12:01
  • @Konza The reason we are all asking you for the additional information is because there is nothing wrong syntactically with when you are doing. Calling DataReader.GetString and storing the result in a string variable is precisely how you should be reading an nvarchar column. So, if it's not working, it due to something else. Since we don't have your database to connect to, we can't reproduce the problem, so if you want us to help you, you are going to have to provide us with more details. – Steven Doggart Jun 11 '12 at 12:11
  • Yes I mean of course SQL Server. Column is named "displayedString" and has datatype nvarchar(150). In this column are stored descriptions of some operations (it is simple text containing detailed information as data, city etc.) I'm trying to display this values in aspx page using I don't get any error if in the column there is text - the text is just not being displayed. When I insert to the column numeric values - numbers appears on aspx page. – Konrad Z. Jun 11 '12 at 12:19
  • @Konza Have you traced through it to verify that the value1 variable is really empty after executing the `value1 = myGSFdataReader.GetString(4)` line? If it's not displaying properly, it could be a problem with that code rather the part where you are reading it. – Steven Doggart Jun 11 '12 at 12:23
  • Yes you're right - the problem is with reading this values. Probably I should change script type in aspx code. – Konrad Z. Jun 11 '12 at 12:38
  • From ASPX code I removed this part of code: , so i left only: <%= value1 %> and now it works :) Thank you guys so much for your help! – Konrad Z. Jun 11 '12 at 12:49
  • @Konza Then please post that as an answer and accept it so others know this has been resolved. – Bridge Jun 11 '12 at 15:17

1 Answers1

0

Try

value1 = myGSFdataReader.GetValue(4)

Or

value1 = CStr(myGSFdataReader.GetValue(4))

Amy
  • 132
  • 1
  • 5
  • It gives the same result as before. When I insert to the column value "555aaa" on the aspx page the value doesn't appear, and when I insert "555" - the number is being displayed. – Konrad Z. Jun 11 '12 at 12:21
  • when you debug, what is assigned to value1. You need to see if value1 is being assigned correctly before passed to the aspx page – Amy Jun 11 '12 at 12:56