none
VS 2010 app problem with connection RRS feed

  • Question

  • Hi, I have the following little app running that allows me to run Stored procedures.  It works fine as long as I use the hardcoded string to the database, but when I want to use the mySettings option to read it from the app.config file, I get an error:

    value of type string cannot be converted to system.data.sqlclient.sqlconnection

     

    This is my code:

    My Form:

    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Configuration





    Public Class SPEXEC

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Dim sqlcomm As New SqlCommand

            Dim SqlConn As SqlConnection

            'SqlConn = New SqlConnection("server = jt05rpmaps10\stats;database=webstats;User ID=sa;Password=jt05rpmaps10 ;connection timeout=6000")
            'Dim s As String = "ESSConnectionString"

            SqlConn = My.Settings("ESSConnectionString")

            sqlcomm.Connection = sqlconn
            sqlcomm.CommandTimeout = 240

            sqlcomm.CommandType = CommandType.StoredProcedure
            sqlcomm.CommandText = TextBox1.Text
            sqlconn.Open()

            MessageBox.Show(sqlcomm.ExecuteNonQuery)




        End Sub


        Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

        End Sub
    End Class

     

    My App.config file:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <configSections>
        </configSections>
        <connectionStrings>
            <add name="SPEXEC.My.MySettings.ESSConnectionString" connectionString="Provider=SQLOLEDB;Data Source=win7;Persist Security Info=True;Password=ripley;User ID=sa;Initial Catalog=Webstats"
                providerName="System.Data.OleDb" />
        </connectionStrings>
        <system.diagnostics>
            <sources>
                <!-- This section defines the logging configuration for My.Application.Log -->
                <source name="DefaultSource" switchName="DefaultSwitch">
                    <listeners>
                        <add name="FileLog"/>
                        <!-- Uncomment the below section to write to the Application Event Log -->
                        <!--<add name="EventLog"/>-->
                    </listeners>
                </source>
            </sources>
            <switches>
                <add name="DefaultSwitch" value="Information" />
            </switches>
            <sharedListeners>
                <add name="FileLog"
                     type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
                     initializeData="FileLogWriter"/>
                <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
                <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
            </sharedListeners>
        </system.diagnostics>
    </configuration>

     

    I appreciate any assistance

     

    Using Visual Studio 2010

    • Moved by Bob Beauchemin Tuesday, April 19, 2011 2:09 PM Moved to the most appropriate forum (From:.NET Framework inside SQL Server)
    Tuesday, April 19, 2011 1:22 PM

Answers

  •         Dim SqlConn As SqlConnection

            'SqlConn = New SqlConnection("server = jt05rpmaps10\stats;database=webstats;User ID=sa;Password=jt05rpmaps10 ;connection timeout=6000")
            'Dim s As String = "ESSConnectionString"

            SqlConn = My.Settings("ESSConnectionString")
       


    Hello Louis,

    The setting is a string, not an instance of a SqlConnection classs; so you can't set it this way, you have to instanziiate a new SqlConnection with that string; try it this way:

    SqlConn = New SqlConnection(My.Settings("ESSConnectionString"))
    

    Olaf Helper
    * cogito ergo sum * errare humanum est * quote erat demonstrandum *
    Wenn ich denke, ist das ein Fehler und das beweise ich täglich
    Blog Xing
    Tuesday, April 19, 2011 1:42 PM