Jun 2nd, 2011, 10:11 AM
#1
Thread Starter
Addicted Member
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:
Imports System.Data.SqlClient
Imports System.IO
Imports System.Configuration
Imports System.Data.Common.DbConnection
Public Class csvUpdate
'connection string
Public WithEvents con As New SqlConnection _
("Data Source=" + TxtServer.Text + ";Initial Catalog=" + TxtCat.Text + ";Integrated Security=True;Asynchronous Processing=True")
Public WithEvents var As New SqlConnection _
("Data Source=" + TxtServer.Text + ";Initial Catalog=" + TxtCat.Text + ";User Id =" + TxtUser.Text + ";Password=" + TxtPass.Text + ";Asynchronous Processing=True")
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Height = 172
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCSV.Click
If TxtTable.Text <> "" Then
OpenFileDialog1.Title = "Open a Text File"
OpenFileDialog1.Filter = "Comma Delimited(*.csv)|*.csv"
OpenFileDialog1.DefaultExt = ".csv"
OpenFileDialog1.InitialDirectory = "C:\"
OpenFileDialog1.ShowDialog()
If ChkAD.Checked = True Then
'readcsvAD()
Else
'readcsvUP()
End If
Else
TxtTable.Focus()
End If
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkAD.CheckedChanged
If Me.Height < 242 Then
Me.Height = 243
Else
Me.Width = 144
Me.Height = 172
End If
End Sub
End Class
Thanks!
Attached Files
Jun 2nd, 2011, 12:36 PM
#2
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
Jun 2nd, 2011, 07:11 PM
#3
Re: Using Textbox.Text in Connection String VB.NET 2008
As tg suggests, only create a connection when you need one:
vb.net Code:
Using connection As New SqlConnection(GetConnectionString())
'...
End Using
Your GetConnectionString method does as it says: gets the connection string.
Jun 3rd, 2011, 12:36 PM
#4
Thread Starter
Addicted Member
Re: Using Textbox.Text in Connection String VB.NET 2008
Originally Posted by
jmcilhinney
As tg suggests, only create a connection when you need one:
vb.net Code:
Using connection As New SqlConnection(GetConnectionString())
'...
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
Forum Rules
Click Here to Expand Forum to Full Width