0

i am runnig this code inside a reader loop:

While reader.read
For x = 0 To reader.FieldCount - 1
    MsgBox(reader.GetValue(x))
    Dim find_text As String = "<<" & reader.GetName(x) & ">>"
    Dim replacet_text As String = reader.GetString(x)

    oDoc.Content.Find.Execute(FindText:=find_text, ReplaceWith:=replacet_text, Replace:=word.WdReplace.wdReplaceAll)
Next
End While

so its showing every column as my SQL query says SELECT * from table1

but the issue is, depending on the type of column in the database its showing errors like cannot be converted to type 'String'.

how can i do ALL columns no matter what type? i just want to read all into a string

1 Answer 1

1

Try Dim replacet_text As String = reader.GetValue(x).ToString and if it failed it means the type is not convertible to string so you can place this line of code in a try catch block and place error message when data is not convertible to string:

While reader.read
For x = 0 To reader.FieldCount - 1
    MsgBox(reader.GetValue(x))
    Dim find_text As String = "<<" & reader.GetName(x) & ">>"
    Dim replacet_text As String
    Try
        replacet_text = reader.GetValue(x).ToString()
    Catch ex As Exception
        replacet_text = "-- Not Available! --"
    End Try
    oDoc.Content.Find.Execute(FindText:=find_text, ReplaceWith:=replacet_text, Replace:=word.WdReplace.wdReplaceAll)
Next
End While
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way I can check the type of column in the loop ?
Yes you can use reader.GetSchemaTable() function to access information about requested table type in another DataTable which is called Schema Table. The first column(index 0) in schema table should be your field name and second column wil be the type. And number of rows in schema table are equal to number of table defined fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.