PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Using Textbox.Text in Connection String VB.NET 2008-VBForums
Results 1 to 4 of 4

Thread: Using Textbox.Text in Connection String VB.NET 2008

  1. #1

    Thread Starter
    Addicted Member condonethis's Avatar
    Join Date
    Apr 2010
    Location
    TX
    Posts
    133

    Question Using Textbox.Text in Connection String VB.NET 2008

    Using the following code and trying to allow the end user to specify the values for the connection string for an app that inports .csv to a specified location. App fails to run due to the fact that it tries to initialize connection string on program run resulting in NULL values. Is there any way I can prevent this and make this work?

    vb.net Code:
    1. Imports System.Data.SqlClient
    2. Imports System.IO
    3. Imports System.Configuration
    4. Imports System.Data.Common.DbConnection
    5.  
    6. Public Class csvUpdate
    7.  
    8.     'connection string
    9.     Public WithEvents con As New SqlConnection _
    10.  ("Data Source=" + TxtServer.Text + ";Initial Catalog=" + TxtCat.Text + ";Integrated Security=True;Asynchronous Processing=True")
    11.  
    12.     Public WithEvents var As New SqlConnection _
    13.    ("Data Source=" + TxtServer.Text + ";Initial Catalog=" + TxtCat.Text + ";User Id =" + TxtUser.Text + ";Password=" + TxtPass.Text + ";Asynchronous Processing=True")
    14.  
    15.  
    16.  
    17.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    18.         Me.Height = 172
    19.     End Sub
    20.  
    21.    
    22.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCSV.Click
    23.         If TxtTable.Text <> "" Then
    24.             OpenFileDialog1.Title = "Open a Text File"
    25.             OpenFileDialog1.Filter = "Comma Delimited(*.csv)|*.csv"
    26.             OpenFileDialog1.DefaultExt = ".csv"
    27.             OpenFileDialog1.InitialDirectory = "C:\"
    28.             OpenFileDialog1.ShowDialog()
    29.             If ChkAD.Checked = True Then
    30.                 'readcsvAD()
    31.             Else
    32.                 'readcsvUP()
    33.             End If
    34.         Else
    35.             TxtTable.Focus()
    36.         End If
    37.     End Sub
    38.  
    39.     Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkAD.CheckedChanged
    40.  
    41.         If Me.Height < 242 Then
    42.             Me.Height = 243
    43.         Else
    44.             Me.Width = 144
    45.             Me.Height = 172
    46.         End If
    47.  
    48.     End Sub
    49. End Class


    Thanks!
    Attached Files Attached Files

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,677

    Re: Using Textbox.Text in Connection String VB.NET 2008

    1) what's the reason for declaring your connections withevents?
    2) Don't instantiate them until you actually use them. Because you are declaring them with the New keyword, they are being created when the class is created. Take the New out, as well as the connection string, and only instantiate them when you need to.

    -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
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,879

    Re: Using Textbox.Text in Connection String VB.NET 2008

    As tg suggests, only create a connection when you need one:
    vb.net Code:
    1. Using connection As New SqlConnection(GetConnectionString())
    2.    '...
    3. End Using
    Your GetConnectionString method does as it says: gets the connection string.

  4. #4

    Thread Starter
    Addicted Member condonethis's Avatar
    Join Date
    Apr 2010
    Location
    TX
    Posts
    133

    Re: Using Textbox.Text in Connection String VB.NET 2008

    Quote Originally Posted by jmcilhinney View Post
    As tg suggests, only create a connection when you need one:
    vb.net Code:
    1. Using connection As New SqlConnection(GetConnectionString())
    2.    '...
    3. End Using
    Your GetConnectionString method does as it says: gets the connection string.
    Thanks for the help, I was able to complete the project and it is in the Code Bank:

    http://www.vbforums.com/showthread.php?t=651729

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width