By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,646 Members | 1,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,646 IT Pros & Developers. It's quick & easy.

I got "System.__ComObject", Where is my SQL record value?

P: 2
I can't assign a MS SQL Server table record value to a simple VB variable, should be an easy thing. Sample SQL Server table has the data in the record as a char(30) string, the column for that record is named "Try". The VB script function accessing the record value is running on a web hosting server with ASP.Net 2.0 on a web page. Result always is it "can't convert from a Field to type 'String'". Printing out the SQL variable results in "System.__ComObject". I know this is a wrapper, but how do I get in and access my value?

I have tried CAST and CONVERT SQL commands. Also, if I create a Gridview and tie the data source to the SQL database, it displays the table values just fine, but I need to apply the values to a formula and can't use the raw values from the SQL table so this won't work for me. Going the opposite way, setting the SQL table value to any VB value always works good. Is ASP.Net 2.0 overtaking the Com wrapper?

Anyone have ideas on how to make this go away?


Expand|Select|Wrap|Line Numbers
  1. <script runat="server">
  2.   Dim SampleString as String 'here's where we want the string data from the SQL table
  3.  
  4.   Public Sub SaveSignals()
  5.     Dim Connection1 As Object
  6.     Dim MyTable As Object
  7.     Dim Query As String
  8.     Dim ConnectionString As String
  9.  
  10.     ConnectionString = "Provider=SQLOLEDB;" & _
  11.           "Data Source=server;" & _
  12.           "Initial Catalog=tablename;" & _
  13.           "User ID=userid;" & _
  14.           "Password=pw1234"
  15.     Connection1 = CreateObject("ADODB.Connection") 'make a connection
  16.     Connection1.open(ConnectionString)
  17.  
  18.     MyTable = CreateObject("ADODB.Recordset") 'make a table object
  19.     Query = "SELECT * FROM tablename" 'SQL command to open up the named table, get 1st record
  20.     MyTable.CursorType = 2 'adOpenDynamic
  21.     MyTable.LockType = 2 'adLockOptimistic
  22.     MyTable.open(Query, Connection1) 'opens up the table using the SQL connection
  23.  
  24.     MyTable.MoveFirst() 'goes to the first record, everything works great up to here
  25.  
  26.     'assign a SQL table value to my VB string variable    
  27.     SampleString=MyTable("Try") '<------ Results in error at web server:
  28.       'Exception Details: System.InvalidCastException: 
  29.       'Conversion from type 'Field' to type 'String' is not valid.
  30.  
  31.     'just for kicks, print out the value to the web page.    
  32.     Response.Write(MyTable("Try")) '<------ so if we comment out the above line so the server is ok,
  33.       'the server prints out: "System.__ComObject" on the web page, instead of my string value
  34.  
  35.     'when we get here, attempt to put any string value back into the SQL table    
  36.     MyTable("Try") = "TestMe" 'any data, from any string source,
  37.       ' going back to the database works great, no problems at all.
  38.  
  39.     MyTable.update()  ' Save changes, always successful
  40.  
  41.     MyTable.Close()   ' Close table.
  42.     Connection1.Close()   ' Close table. Success always.
  43.   End Sub
  44. </script>
  45.  
Dec 4 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.