System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0

Last post 07-06-2009 10:53 PM by Tonie. 1 replies.

Sort Posts:

  • System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0

    07-06-2009, 6:49 PM
    • Member
      22 point Member
    • jcook331
    • Member since 07-06-2009, 10:43 PM
    • Posts 130

    I am new to .net and sql server and I'm having some problems with basic stuff. I've created a stored procedure that inserts a new record into a table but I have been unable to call it from the .net environment. I'm using VB to call the SP. I keep getting the error message:

    System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0

    It seems to be referring to the line:

    Dim conn As New SqlConnection(connstr)

    I'm trying to follow a code example I found on the internet, but I may have it all wrong. This is my code:

    Partial Class _Default
    Inherits System.Web.UI.Page

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

    Dim connstr As String = "ConnectionStrings:DatabaseConnectionString1()"
    Dim conn As New SqlConnection(connstr)
    Dim cmd As New SqlCommand("insert_goal", conn)

    cmd.CommandType = Data.CommandType.StoredProcedure

    cmd.Parameters.Add(New SqlParameter("@year", Data.SqlDbType.NChar, 10))
    cmd.Parameters("@year").Value = "2009-2010"
    cmd.Parameters.Add(New SqlParameter("@unit", Data.SqlDbType.VarChar, 50))
    cmd.Parameters("@unit").Value = "welding"
    cmd.Parameters.Add(New SqlParameter("@ignum", Data.SqlDbType.TinyInt, 2))
    cmd.Parameters("@ignum").Value = 1

    conn.Open()
    cmd.ExecuteNonQuery()
    conn.Close()

    End Sub
    End Class

  • Re: System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0

    07-06-2009, 10:53 PM
    Answer
    • Member
      13 point Member
    • Tonie
    • Member since 06-11-2009, 4:55 AM
    • Posts 3

    Try this:


    1. Open your web.config file and in the <ConnectionStrings> section add the following:

    <connectionStrings>
            <clear/>
            <remove name="LocalSQLServer"/>
            <add name="YOURPREFERREDCONNECTIONSTRINGNAME" connectionString="Data Source=YOURDATASOURCE;Initial Catalog=DATABASENAME;User ID=USERNAME;Password=PASSWORD" providerName="System.Data.SqlClient"/>
        </connectionStrings>


    2.  Open your code behind file and import the follwing

    Imports System.Data.SqlClient
    Imports System.Configuration


    3. Modify your code to the following

                Dim conn As SqlConnection = New SqlConnection()
                Dim cmd As SqlCommand = New SqlCommand()

                Try
                    conn.ConnectionString = ConfigurationManager.ConnectionStrings("YOURPREFERREDCONNECTIONSTRINGNAME").ConnectionString
                    cmd.Connection = conn
                    cmd.CommandText = "NAMEOFYOURSTOREDPROCEDURE"
                    cmd.CommandType = CommandType.StoredProcedure
                    
                    'Pass parameters required by the stored procedure
                    With cmd
                        .Parameters.Add(New SqlParameter("@year", Data.SqlDbType.NChar, 10))
                        .Parameters("@year").Value = "2009-2010"
                        .Parameters.Add(New SqlParameter("@unit", Data.SqlDbType.VarChar, 50))
                        .Parameters("@unit").Value = "welding"
                        .Parameters.Add(New SqlParameter("@ignum", Data.SqlDbType.TinyInt, 2))
                        .Parameters("@ignum").Value = 1
                    End With
                    conn.Open()
                    cmd.ExecuteNonQuery()
                    conn.Close()
                Catch ex As Exception
                    Response.Write(ex)
                End Try

Page 1 of 1 (2 items)