Retrieve Data from SQL Server

Tjoppie

New member
Joined
Sep 1, 2005
Messages
2
Location
South Africa
Programming Experience
5-10
Hi all,

I want to create a class that retrieves data from sql server that will always stay static (For the current user logged in)

First I would like to have a value that I can always refer inside developing my application, to the current user logged on on SQL Server. Now I know that the sql server command for that is
VB.NET:
 SUSER_SNAME()
, but how do I construct that into a class so I can just refer to that everytime I want to refer to that value?

This is how I started out:

VB.NET:
 [size=2][color=#0000ff]

Imports[/color][/size][size=2] System.Data.SqlClient

[/size][size=2][color=#0000ff]Imports[/color][/size][size=2] System.Drawing

[/size][size=2][color=#0000ff]Public[/color][/size][size=2] [/size][size=2][color=#0000ff]Module[/color][/size][size=2] JMSGlobals

[/size][size=2][color=#0000ff]Public[/color][/size][size=2] JMSColor = Color.FromArgb(149, 178, 198)

[/size][size=2][color=#0000ff]Public[/color][/size][size=2] JMSConnection [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]New[/color][/size][size=2] SqlConnection

[/size][size=2][color=#0000ff]Public[/color][/size][size=2] JMSColorLblsBack = Color.FromArgb(57, 106, 105)

[/size][size=2][color=#0000ff]Public[/color][/size][size=2] [/size][size=2][color=#0000ff]Function[/color][/size][size=2] CurrentUserName()

[/size][size=2][color=#0000ff]Try

[/color][/size][size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] myDataReader [/size][size=2][color=#0000ff]As[/color][/size][size=2] SqlDataReader

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] RetrieveSQLUser [/size][size=2][color=#0000ff]As[/color][/size][size=2] SqlCommand

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] CurrentSQLUser [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String

[/color][/size][size=2]JMSConnection = JMSClasses.JMSConnection

JMSConnection.ConnectionString = JMSClasses.JMSConnection.ConnectionString

RetrieveSQLUser = [/size][size=2][color=#0000ff]New[/color][/size][size=2] SqlCommand("select SUSER_SNAME() AS SQLLogin", JMSConnection)

JMSConnection.Open()

myDataReader = RetrieveSQLUser.

[/size][size=2][color=#008000]'CurrentSQLUser = myDataReader.GetValue(0)

[/color][/size][size=2][/size][size=2][color=#0000ff]Catch[/color][/size][size=2] ex [/size][size=2][color=#0000ff]As[/color][/size][size=2] Exception

MsgBox(ex.ToString)

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Try

[/color][/size][size=2][/size][size=2][color=#008000]'JMSConnection.Close()

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Function

End[/color][/size][size=2] [/size][size=2][color=#0000ff]Module

[/color][/size]

When I tell a form to insert that value into a label, nothing happens..

This is what I did:

VB.NET:
Private Sub FrmMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Me.lblWelcomeName.Text = JMSClasses.CurrentUserName

What am I doing wrong, and could anyone give me an example please?

Thanks

Rudi
 
Back
Top