Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET

 
Hi guys
 
I want to make a SQL Connector form for my application
 
this is the code for the form:
 
Imports System.Data.SqlClient
 
Public Class frmServer
    Public connectionstring As String
 
    Private Sub frmServer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
    End Sub
 
    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        clsMSSQL.Database = txtDatabase.Text
        clsMSSQL.Server = txtServer.Text
        clsMSSQL.con.Open()
        Me.Close()
        frmMain.Show()
    End Sub
 
    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Close()
    End Sub
 
    Private Sub cmdTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTest.Click
        Try
            If txtServer.Text = "" OrElse txtDatabase.Text = "" Then
                MessageBox.Show("Fill all the required fields", "SQL Connector", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Else
                clsMSSQL.Database = txtDatabase.Text
                clsMSSQL.Server = txtServer.Text
                clsMSSQL.con.Open()
                clsMSSQL.con.Close()
                MessageBox.Show("Test Connection Successfully!")
            End If
        Catch
            MessageBox.Show("Cannot Established Connection", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub
and this is the class:
 
Imports System.Data.SqlClient
 
Public Class clsMSSQL
    Public Shared Server As String = ""
    Public Shared Database As String = ""
    Public Shared constring As String = "Data Source=" & Server & ";Initial Catalog=" & Database & " ;Integrated Security=True;"
    Public Shared con As New SqlConnection(constring)
    Public Shared Connector As New frmServer()
End Class
 
Where is the wrong in this code?
I can't connect with database it gives connection error.
Posted 25-Jan-13 2:22am
jomachi1.6K
Edited 25-Jan-13 2:42am
v2
Comments
OriginalGriff at 25-Jan-13 7:34am
   
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
What happens that you don't expect, or doesn't happen that you do?
Use the "Improve question" widget to edit your question and provide better information.
Richard MacCutchan at 25-Jan-13 7:49am
   
You should show the actual connection string in your try block, and the full exception message in the catch block.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The Server and Database are both blank so constring will not be correct - it's "Data Source=;Initial Catalog= ;Integrated Security=True;"
 
Your class clsMSSQL is badly constructed. Try the following instead
 
Imports System.Data.SqlClient
Public Class clsMSSQL
	Private _Server As String
	Private _Database As String
	Public Property Server() As String
		Get
			Return _Server
		End Get
		Set(ByVal value As String)
			_Server = value
		End Set
	End Property
	Public Property Database() As String
		Get
			Return _Database
		End Get
		Set(ByVal value As String)
			_Database = value
		End Set
	End Property
	Public ReadOnly Property constring() As String
		Get
			Return "Data Source=" & _Server & ";Initial Catalog=" & _Database & " ;Integrated Security=True;"
		End Get
	End Property
End Class
 
Etc ... I haven't included the other bits you will need to change in a similar way
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,127
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 25 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100