Hi Roland,
I don't see why the different data types would make a difference. I would trace the values of the data and see if you can find the reason why.
>Hi Cathi
>
>I tried the Northwind and it works. I tried subsituted my table and it did not work.
>
>The only difference I noticed in the Northwind Table, Field REGION and My Field APN is that REGION is a nvarchar and mine APN is defined as varchar. Would that make a difference?
>
>Thanks
>Roland
>
>===========================================================================
>
>
>>Hi Roland,
>>
>>Your syntax should be okay from what I can tell. Here is a sample program that tests your syntax against the SQL Server Northwind database. This doesn't generate any errors for me. Try this code. If no errors then the problem is somewhere else in your data.
>>
>>
>>Dim oConnStr As String = "data source=localhost;initial catalog=Northwind;user id=sa;password="
>>Dim oConn As New SqlConnection()
>>oConn.ConnectionString = oConnStr
>>oConn.Open()
>>
>>' Create a Command object with select statement
>>Dim strSelect As [String] = "SELECT * FROM CUSTOMERS where customerid like 'C%'"
>>Dim oSelectCommand As New SqlCommand(strSelect, oConn)
>>
>>' Create a DataAdapter
>>Dim da As New SqlDataAdapter()
>>da.SelectCommand = oSelectCommand
>>
>>' Create a DataSet
>>Dim ds As New DataSet()
>>
>>' Fill the DataSet with table information
>>da.Fill(ds, "CUSTOMERS")
>>
>>Dim dr As DataRow
>>For Each dr In ds.Tables("CUSTOMERS").Rows()
>> MessageBox.Show(IIf(IsDBNull(dr("Region")), "Value is Null", dr("Region")))
>>Next
>>
>>oConn.Close()
>>
>>
>>>Hi,
>>>
>>>I encountered the following error message with the code below the error message.
>>>
>>>An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll
>>>Additional information: Cast from type 'DBNull' to type 'String' is not valid.
>>>
>>>MyEmailBody = myDataRow("AccountId") & " " & _
>>>myDataRow("OrderNumber") & " " & _
>>>IIf(IsDBNull(myDataRow("APN")), "", Trim(myDataRow("APN"))) & " " & _
>>>IIf(IsDBNull(myDataRow("StreetNumber")), "", Trim(myDataRow("StreetNumber")))
>>>
>>>What is the proper code to cancanate the string?
>>>
>>>Any help would be greatly appreciated.
>>>
>>>
>>>Thanks
>>>Roland
-----------------------------------------
Cathi Gero, CPA
Prenia Software & Consulting Services
Microsoft C# / .NET MVP
Mere Mortals for .NET MVP
cgero@prenia.comwww.prenia.comWeblog: blogs.prenia.com/cathi