AllExperts > Experts 
Search      

VB.NET

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More VB.NET Answers
Question Library

Ask a question about VB.NET
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Chris
Expertise
I can answer pretty much any question relating to VB.NET and its use in a Windows environment. I specialize in ASP.NET web development and MSSQL database access.

Experience
I have over 5 years of industry experience using VB.NET and other .NET technologies for web and database development.

Education/Credentials
I have some college education, but does it really matter in this field of work?

 
   

You are here:  Experts > Computing/Technology > Basic > VB.NET > connection and split function

Topic: VB.NET



Expert: Chris
Date: 8/22/2007
Subject: connection and split function

Question
Hi
I am new to vb.net but i am a php developer
I want to make a little application for my shop so I need help regarding this
1-I need a class for connection of vb.net with sql server 2000 because I don’t want to make connections every time for all my forms and example how to use that class
2-i have two textbox one for user enter and second is read only first textbox consists of numeric value and a slash sign (/)so when user enter first value / second value then second value shows in 2nd read only textbox
For this purpose I use this code
Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
       If TextBox1.Text <> "" Then
           Dim strToSplit As String = TextBox1.Text
           Dim arTemp() As String = Split(strToSplit, "/")
           TextBox2.Text = arTemp(1)
       End If
   End Sub
But user only enter first value then it gives error how could I solve this problem and also how can I prevent for user to enter alphabetic and slash sign (/) twice
I am using visual studio.net 2003
I am very thankful to you
Regards,
Bilal


Answer
As far as connections to SQL server go, you are supposed to open your connection just before you use it to execute a query, and close it immediately afterwards, to keep the server's connection pool from filling up.  You can create a small class to encapsulate this functionality, as well as connection settings, and use it for all of your queries.

'--[ dbaccess.vb ]--

Imports System.Data.SqlClient

Public Class DBAccess
 Protected m_strConnString As String = ""

 Public ReadOnly Property ConnectionString() As String
   Get
     Return m_strConnString
   End Get
 End Property

 Public Sub New(ByVal strConnString As String)
   m_strConnString = strConnString
 End Sub

 ' Prevent a new instance from being made without specifying a connection string
 Private Sub New()
 End Sub

 Public Function MakeConnection() As SqlConnection
   Return New SqlConnection(m_strConnString)
 End Function

 Public Function MakeCommand(ByVal strQuery As String) As SqlCommand
   Return New SqlCommand(strQuery, MakeConnection())
 End Function
End Class

'--[ end dbaccess.vb ]--


And to use it:

   Dim objDBA As New DBAccess("initial catalog=DATABASE;data source=SERVER;user id=USER;password=PASS")
   Dim objCMD As SqlCommand = objDBA.MakeCommand("SELECT * FROM Users")

   objCMD.Connection.Open()
   Dim objDR As SqlDataReader = objCMD.ExecuteReader()
   '...
   objCMD.Connection.Close()

This is a very simple implementation, it might be perfect for what you need, or it might not.  You can also take a look at this project I made and have been using for all of my data access needs, I call it Table Assimilator:
http://www.beyourown.net/areas/Developers/TableAssimilator.aspx

It's a free download, but you'll have to register first.

As for question #2... you're assuming that you'll have two entries in the array returned from the .Split() function.  This won't be true if there's not yet a "/" character followed by some other text.  You might also want to use the TextChanged event handler, to have TextBox2's value update as text is entered in TextBox1:

 Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
   ' it's faster to check the length of the string
   If (TextBox1.Text.Length > 0) Then
     ' the c after "/" indicates its a character to split by, faster performance
     Dim arTemp As String() = TextBox1.Text.Split("/"c)

     ' make sure there's two entries in the array we got from Split()
     If (arTemp.Length > 1) Then
       TextBox2.Text = arTemp(1)
     End If
   End If
 End Sub

Hope this helps!

Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.