SQL Server Bugaboo

Hi Gang!

Here's the skinny:

I am working on a simple db app that reads and updates a db on SQL Server 7.  When I ONLY have the connection string in the code, I can connect just fine.  When I add code to manipulate data, I lose the connection to the db.

How can this be

The code that causes the problem is below.

Thanks for you help.

Dim conn As New SqlClient.SqlConnection
        ' TODO: Modify the connection string and include any
        ' additional required properties for your database.
        conn.ConnectionString = "integrated security=SSPI;data source=PROGRAMMING;" & _
        "persist security info=False;initial catalog=SShopDB"
        Dim sql As String = "SELECT * FROM SShopDB"
        Dim cmd As New SqlClient.SqlCommand(sql, conn)

        Try
            conn.Open()
            

            Dim reader As System.Xml.XmlReader = cmd.ExecuteXmlReader()
            Do While reader.Read()
                txtOut.AppendText(reader.Value & ControlChars.CrLf)
            Loop



        Catch ex As Exception
            MessageBox.Show("Failed to connect to data source")
        Finally
            conn.Close()
        End Try

    End Sub

    


Answer this question

SQL Server Bugaboo

  • Ben Docume

    The error is caught when the button that activates the code is clicked.  The message is "Failed to Connect to Database," as specified in the Catch code.

    I am using vb.net, and am just really lost.  If there was a simple list of instructions somewhere that listed what I needed to do, life would be easier.  LOL

    Anyway, thanks for your help.  I am new to database apps in vb.net.  I have done it with MySQL and PHP.  This is new to me, but I need to know it.

    Thanks again!
    Gary

  • TJHinUSA

    if (cmd.Connection.State != ConnectionState.Open)
         cmd.Connection.Open();

    //Update data here

    cmd.Connection.Close();

  • jrbeachy

    When is the exception thrown   What type of exception is it and what is its text
  • kiln

    On closer inspection you have no user name or password defined in your connection string.  That can cause problems.  Also check the Exceptions inner exception.  Quite often the inner or inner-inner exception will give a more accurate description of the problem.
  • SQL Server Bugaboo