Results 1 to 4 of 4

Thread: error msg : value type string cannot be converted......

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    24

    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,

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    24

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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:
    1. Dim sqlCom As SqlCommand = New SqlCommand(query2, strConnectionString)
    2. Dim sqlReader As SqlDataReader = sqlCom.ExecuteReader()
    should be this:
    VB Code:
    1. Dim sqlCon As New SqlConnection(strConnectionString)
    2. Dim sqlCom As SqlCommand = New SqlCommand(query2, sqlCon)
    3.  
    4. sqlCon.Open()
    5.  
    6. Dim sqlReader As SqlDataReader = sqlCom.ExecuteReader()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width