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

What's a System.__ComObject? Where's my SQL Server value?

P: 2
I can't assign a MS SQL Server table record value to a simple VB variable, should be a really 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 "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
  4.   Public Sub SaveSignals()
  5.     Dim Connection1 As Object
  6.     Dim MyTable As Object
  7.     Dim Query As String
  8.     Dim ConnectionString As String
  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
  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., Connection1) 'opens up the table using the SQL connection
  23.     MyTable.MoveFirst() 'goes to the first record, everything works great up to here
  26. 'problem here
  28.     'assign a SQL table value to my VB string variable    
  29.     SampleString=MyTable("Try") '<------ Results in error at web server:
  30.       'Exception Details: System.InvalidCastException: 
  31.       'Conversion from type 'Field' to type 'String' is not valid.
  33.     'just for kicks, print out the value to the web page.    
  34.     Response.Write(MyTable("Try")) '<------ so if we comment out the above line so the server is ok,
  35.       'the server prints out: "System.__ComObject" on the web page, instead of my string value
  39.     'when we get here, attempt to put any string value back into the SQL table    
  40.     MyTable("Try") = "TestMe" 'any data, from any string source,
  41.       ' going back to the database works great, no problems at all.
  43.     MyTable.update()  ' Save changes, always successful
  45.     MyTable.Close()   ' Close table.
  46.     Connection1.Close()   ' Close table. Success always.
  47.   End Sub
  48. </script>
Dec 4 '06 #1
Share this Question
Share on Google+
1 Reply

P: 1
Hi, I ran into the same problem and was able to fix it by changing this:


to this:

SampleString=MyTable.Fields.Item("Try") .Value

I can't figure out why the syntax needs to be different, but whatever the reasons this seems to work just fine.
May 22 '08 #2

Post your reply

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