Results 1 to 10 of 10

Thread: [RESOLVED] Changing SQLConnection question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Location
    Southeast Michigan
    Posts
    155

    Resolved [RESOLVED] Changing SQLConnection question

    I was trying to set up a simple test to be able to switch between a remote SQL server and a local SQL server. When I run this it gets data from the remote server and if I change the value of useLocalDB to true and run the stored procedure again I still get the data from the remote.

    I'm probably just not understanding the connection process?

    Code:
    Imports System.Data.SqlClient
    Public Class Form1
        Dim tblSteps As DataTable = New DataTable
        Dim StepsBindingSource As New BindingSource
        Dim useLocalDB As Boolean = False
        Dim cnPMSQL_Remote = New SqlConnection(My.Settings.SQLConnectionStringRemote)
        Dim cnPMSQL_Local = New SqlConnection(My.Settings.SQLConnectionStringLocal)
    
        Private Sub ExitProgram_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitProgram.Click
            Me.Close()
        End Sub
    
        Private Sub RunSproc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RunSproc.Click
            Dim spSteps As SqlCommand
            If useLocalDB Then
                spSteps = New SqlCommand("sproc_shop_order_get_recipe1", cnPMSQL_Local)
            Else
                spSteps = New SqlCommand("sproc_shop_order_get_recipe1", cnPMSQL_Remote)
            End If
            spSteps.Parameters.AddWithValue("@int_shop_order", "152837000001")
            spSteps.Parameters.AddWithValue("@int_prev_so", "459761000001")
            spSteps.CommandType = CommandType.StoredProcedure
            spSteps.CommandTimeout = 15
            tblSteps.Clear()
    
            Try
                Dim StepsAdapter As SqlDataAdapter = New SqlDataAdapter(spSteps)
                StepsAdapter.Fill(tblSteps)
                StepsBindingSource.DataSource = tblSteps
                dgSteps.DataSource = StepsBindingSource
            Catch ex As Exception
                Trace.WriteLine(Date.Now.ToLongTimeString & "*: Database not available" & ex.Message)
                MessageBox.Show("Database not available" & Environment.NewLine & ex.Message & Environment.NewLine & ex.StackTrace)
            End Try
    
        End Sub
    
        Private Sub RemoteDB_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoteDB.CheckedChanged
            If RemoteDB.Checked Then
                useLocalDB = False
            Else
                useLocalDB = True
            End If
        End Sub
    End Class
    Dave

    Helpful information I've found here so far : The Definitive "Passing Data Between Forms" : Restrict TextBox to only certain characters, numeric or symbolic :
    .NET Regex Syntax (scripting) : .NET Regex Language Element : .NET Regex Class : Regular-Expressions.info
    Stuff I've learned here so far : Bing and Google are your friend. Trying to help others solve their problems is a great learning experience

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Changing SQLConnection question

    Before you fill your datatable... check the connection... make sure the connection is correct on the Adaptor and the command objects.

    Also... *how* do you *know* your'e getting data from the server and not from the local one?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Location
    Southeast Michigan
    Posts
    155

    Re: Changing SQLConnection question

    I changed the stored procedure on the local database to return data in a different order for my test so I could be sure. I ran the code reversing the logic so that the first time run it connects to the local data to verify it. Which ever connection is run first is how it stays.

    I checked the connection string on the SqlCommand by adding this right before the Fill statement :

    Code:
    MessageBox.Show(spSteps.Connection.ConnectionString)
    It changes as expected but I don't know how to verify the connection on the adapter?
    Dave

    Helpful information I've found here so far : The Definitive "Passing Data Between Forms" : Restrict TextBox to only certain characters, numeric or symbolic :
    .NET Regex Syntax (scripting) : .NET Regex Language Element : .NET Regex Class : Regular-Expressions.info
    Stuff I've learned here so far : Bing and Google are your friend. Trying to help others solve their problems is a great learning experience

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Changing SQLConnection question

    Just for grins-- Do you close the connection any where when you are done with it? What about the command? Yes I understand that when it goes out of scope that should happen automaticly but I do it myself any way.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Changing SQLConnection question

    Does the BindingSource.ListChanged event fire when the DataSource is changed? If not you need to call the BindingSource.ResetBindings method to instruct bound controls to refresh and display the new data.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Location
    Southeast Michigan
    Posts
    155

    Re: Changing SQLConnection question

    I don't think it has to do with the BindingSource. Stepping through the program and looking at the data in the tlbSteps table, in the Immediate Window, right after the StepsAdapter.Fill(tblSteps) statement, the data isn't changing as it should. If I run it to the local database first then any subsequent try at changing the connection always shows the data as if it comes from the local database. Running it first time through using the remote database has the same effect. Always reads from the remote.
    Dave

    Helpful information I've found here so far : The Definitive "Passing Data Between Forms" : Restrict TextBox to only certain characters, numeric or symbolic :
    .NET Regex Syntax (scripting) : .NET Regex Language Element : .NET Regex Class : Regular-Expressions.info
    Stuff I've learned here so far : Bing and Google are your friend. Trying to help others solve their problems is a great learning experience

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Location
    Southeast Michigan
    Posts
    155

    Re: Changing SQLConnection question

    By the way, I did add some close and dispose statements as shown below trying to clear everything. Not sure I've got them in the right places.
    Code:
        Private Sub RunSproc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RunSproc.Click
            Dim spSteps As SqlCommand
    
            If useLocalDB Then
                spSteps = New SqlCommand("sproc_shop_order_get_recipe1", cnPMSQL_Local)
            Else
                spSteps = New SqlCommand("sproc_shop_order_get_recipe1", cnPMSQL_Remote)
            End If
    
            spSteps.Parameters.AddWithValue("@int_shop_order", "152837000001")
            spSteps.Parameters.AddWithValue("@int_prev_so", "459761000001")
            spSteps.CommandType = CommandType.StoredProcedure
            spSteps.CommandTimeout = 15
            tblSteps.Clear()
    
            Try
                Dim StepsAdapter As SqlDataAdapter = New SqlDataAdapter(spSteps)
                MessageBox.Show(spSteps.Connection.ConnectionString)
                StepsAdapter.Fill(tblSteps)
                StepsBindingSource.DataSource = tblSteps
                dgSteps.DataSource = StepsBindingSource
                StepsAdapter.Dispose()
            Catch ex As Exception
                Trace.WriteLine(Date.Now.ToLongTimeString & "*: Database not available" & ex.Message)
                MessageBox.Show("Database not available" & Environment.NewLine & ex.Message & Environment.NewLine & ex.StackTrace)
            End Try
            cnPMSQL_Local.close()
            cnPMSQL_Remote.close()
            spSteps.Dispose()
        End Sub
    Dave

    Helpful information I've found here so far : The Definitive "Passing Data Between Forms" : Restrict TextBox to only certain characters, numeric or symbolic :
    .NET Regex Syntax (scripting) : .NET Regex Language Element : .NET Regex Class : Regular-Expressions.info
    Stuff I've learned here so far : Bing and Google are your friend. Trying to help others solve their problems is a great learning experience

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Location
    Southeast Michigan
    Posts
    155

    Re: Changing SQLConnection question

    Ok, turns out everything is working fine. To test if it was working on the local or remote database I was changing the order that data was being sent back from the stored procedure. Turns out my column order stays the same after the first time through in the datagridview, or table, regardless of how the data is sent in subsequent tries.

    I went back into one of the databases and changed the data in the table and it is in fact retrieving from whichever database I have selected.

    Thanks for the responses, that was driving me crazy!
    Dave

    Helpful information I've found here so far : The Definitive "Passing Data Between Forms" : Restrict TextBox to only certain characters, numeric or symbolic :
    .NET Regex Syntax (scripting) : .NET Regex Language Element : .NET Regex Class : Regular-Expressions.info
    Stuff I've learned here so far : Bing and Google are your friend. Trying to help others solve their problems is a great learning experience

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Changing SQLConnection question

    Cool! If you could mark the thread as resolved (under the Thread Tools at the top just above Post#1). Thanks!

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Location
    Southeast Michigan
    Posts
    155

    Re: [RESOLVED] Changing SQLConnection question

    Thanks again. Just had to add tblSteps.Columns.Clear and all is well.
    Dave

    Helpful information I've found here so far : The Definitive "Passing Data Between Forms" : Restrict TextBox to only certain characters, numeric or symbolic :
    .NET Regex Syntax (scripting) : .NET Regex Language Element : .NET Regex Class : Regular-Expressions.info
    Stuff I've learned here so far : Bing and Google are your friend. Trying to help others solve their problems is a great learning experience

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