|
-
Jan 19th, 2007, 02:41 AM
#1
Thread Starter
Junior Member
error msg : value type string cannot be converted......
Dim oConfig As System.Configuration.Configuration
Dim strConnectionString As String
oConfig =Configuration.WebConfigurationManager.OpenWebConfiguration("/myob")
If (oConfig.ConnectionStrings.ConnectionStrings.Count > 0) Then
strConnectionString = oConfig.ConnectionStrings.ConnectionStrings("myAccountConnectionString2").ConnectionString
Else
Throw New ApplicationException("Could not load the database")
End If
Dim query2 As String = "SELECT f008jumlah FROM t008AsetSemasa "
Dim sqlCom As SqlCommand = New SqlCommand(query2, strConnectionString)
Dim sqlReader As SqlDataReader = sqlCom.ExecuteReader()
sqlReader = sqlCom.ExecuteReader()
Dim jum As Decimal
Dim index As Integer
jum = 0
While sqlReader.Read()
jum = jum + sqlReader("f008jumlah") & ControlChars.NewLine 'This will add whatever is in that column to a new line in the textbox
jumAset.Text = jum
index += 1
End While
the code that i bold have a error. the error message say " Value of type ' String' cannot be converted to system.Data.sqlClient.SqlConnection.
but when i used the same code for connection in INSERT statement, the code can be running,
-
Jan 19th, 2007, 02:51 AM
#2
Re: error msg : value type string cannot be converted......
There is no SqlCommand constructor that has two String arguments. You are confusing it with the SqlDataAdapter. In order to do what you're trying to do you have to first create an SqlConnection and then pass it to the SqlCommand constructor. The reason for that is that you must explicitly open and close the connection when executing the command, whereas the SqlDataAdapter will implicitly open and close the connection for you.
-
Jan 19th, 2007, 04:52 AM
#3
Thread Starter
Junior Member
Re: error msg : value type string cannot be converted......
but the connection to database i did in web configuration, i just call that page to make connection,
the connection to database is not in the same file which i wrote select statement in it
-
Jan 19th, 2007, 05:32 AM
#4
Re: error msg : value type string cannot be converted......
You didn't do the connection in the config file, you did the connection string. Your config file simply contains a string that describes how to connect to your database. This:
VB Code:
Dim sqlCom As SqlCommand = New SqlCommand(query2, strConnectionString)
Dim sqlReader As SqlDataReader = sqlCom.ExecuteReader()
should be this:
VB Code:
Dim sqlCon As New SqlConnection(strConnectionString)
Dim sqlCom As SqlCommand = New SqlCommand(query2, sqlCon)
sqlCon.Open()
Dim sqlReader As SqlDataReader = sqlCom.ExecuteReader()
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|