Click here to Skip to main content
15,879,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:


Hi guys

I want to make a SQL Connector form for my application

this is the code for the form:

VB
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
Updated 25-Jan-13 1:42am
v2
Comments
OriginalGriff 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 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

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
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900