[RESOLVED] Connection Object Error
Hi all,,
When I open a form where I refer to a public connection string, Visual studio tells me: The Variable 'JMSConnection' is either undeclared or was never asigned.
The line which gives this is:
VB Code:
Me.SQLCon1.ConnectionString = JMSGlobals.JMSConnection.ConnectionString
The connection string in the globals module is:
VB Code:
Module JMSGlobals
Public JMSColor = Color.FromArgb(149, 178, 198)
Public JMSConnection As New SqlConnection
End Module
The connection string gets built in the login form and looks like:
VB Code:
Private Sub cmdLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogin.Click
If (RdoSQLAuth.Checked = True) Then
'JMSGlobals.JMSConnection.ConnectionString = "Server=" & Me.txtJMSServer.Text & ";Database=MSTRIBOLOGY;User ID=" & Me.txtUsername.Text & ";Password=" & Me.txtPassword.Text & ";Trusted_Connection=False;workstation id=" & Environment.MachineName & ";"
JMSGlobals.JMSConnection.ConnectionString = "Data Source=" & Me.txtJMSServer.Text & ";Initial Catalog=MSTRIBOLOGY;User Id=" & Me.txtUsername.Text & ";Password=" & Me.txtPassword.Text & ";Application Name=" & Application.ProductName & " " & Application.ProductVersion & ";"
Me.DialogResult = DialogResult.OK
'Me.Close()
ElseIf (RdoNTAuth.Checked = True) Then
JMSGlobals.JMSConnection.ConnectionString = "Data Source=" & Me.txtJMSServer.Text & ";Initial Catalog=MSTRIBOLOGY;Integrated Security=SSPI;workstation id=" & Environment.MachineName & ";Application Name=" & Application.ProductName & " " & Application.ProductVersion & ";"
Me.DialogResult = DialogResult.OK
'Me.Close()
End If
End Sub
Anyone know where my problem might be?
Thanks
Rudi
Re: Connection Object Error
Maybe RdoNTAuth and RdoSQLAuth are uncheched....
Regards
Jorge
Re: Connection Object Error
Does it work, because I get the same notifications with my global variables I declare in a global module but the project still works. I have just ignored them.
Re: Connection Object Error
Hmm.. Yeah it works, I figured the error is sql server related and not an error in my application. I think I'll just ignore them aswell
Thanks
Rudi
Re: [RESOLVED] Connection Object Error
My suggestion is not to use Global Modules. Modules are a carry-over from VB6 - not very object-oriented at all.
If you must use global variables for the connection string, create a class that is marked MustInherit so that you don't try to instantiate it. From within this class, create Public Shared objects. I don't recommend puting complex objects here because I think it muddies up the code, but if you need to, place a connection string within this class.
As another alternative, I would create your own class that encapsulates all of your db connections and commands for your project. Instantiate this class when needed and pass references to the class object as needed.
Finally, another interesting way to keep app data around within an app is to create a DataSet object with schema organized to store information such as db settings, applciation settings, user preferences. I liek doing this in some situations because if I need to dump these settings into a easily-read format, I can use the DataSet.WriteXML() function to easily dump the current applicaiton state to a file. Elegant and easy - plus you can then share these application settings between programs. If you're concerned with storing passwords in the xml files, then you could encrypt the output stream of WriteXML with the RijndaelManaged class before it's written to disk. After writing the encrypted file to disk, the user can carry aorund their settings file on a USB keychain in case they move between PCs.
Re: [RESOLVED] Connection Object Error
Yeah I get them when I use a sqlselect statement with a global variable like
Select * from table where Name = ' & globalvar & "'"
Dunno how to fix it but the program works.
Re: [RESOLVED] Connection Object Error
mikeismike
Would you have an app example of this concept? I guess I am a legacy VB6 programmer because I have been using globals forever and always up to learning the newer, correcter way.
Re: [RESOLVED] Connection Object Error
Yeah - I'll post one shortly - what concepts would you like to see in the sample app?
Re: [RESOLVED] Connection Object Error
connection strings and extracting info like current sql user... etc... stuff about my server ;P :D
Re: [RESOLVED] Connection Object Error
How you use what I call global variables. A variable that can be used and changed by different forms and then used in the sql selects. I would also be interested in the use of the connection strings. Obviously can be fake, I don't need yours but I would like to use the proper methodology for .net if globals are wrong.
1 Attachment(s)
Re: [RESOLVED] Connection Object Error
Here's an example app with a few forms - I've forgone the encryption, but there's three sections: the Utility shared class I described, the dbconnection class that you could use, then the XML file application data form wher eit autoloads xml files to remember credentials when it opens.
Re: [RESOLVED] Connection Object Error