Long ago, I needed to connect my database in VB and well, the best way is to use app.config and a ConnectionString in it.
In app.config:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <connectionStrings> <add name="ConnectionString" connectionString="Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;" providerName="System.Data.SqlClient"/> </connectionStrings> </configuration>
Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click Dim myCommand As OleDbCommand Dim intUserCount As Integer Dim strSQL As String strSQL = "SELECT COUNT(*) FROM users " _ & "WHERE usernameID='" & Replace(txtUsername.Text, "'", "''") & "' " _ & "AND passwordID='" & Replace(txtPassword.Text, "'", "''") & "';" '-> here is the problem myConnection.ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"] Dim conString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString myCommand = New OleDbCommand(strSQL, conString)
conString.Open() intUserCount = myCommand.ExecuteScalar() myConnection.Close() If intUserCount > 0 Then lblInvalid.Text = "" FormsAuthentication.SetAuthCookie(txtUsername.Text, True) Response.Redirect("default.aspx") Else lblInvalid.Text = "<p class='message'>Sorry... try again...</p>" End If End Sub