String Connection in Web.Config using Asp.Net VB Language with SQL Server 2005

Posted: Thursday,March 12, 2009 in ASP.NET VB
Tags: , , , , ,

In web.config after <configuration> statement. add this code :

<add key=”ConnectionString” value=”server=nameofserver;uid=nameofuserid;pwd=nameofpassword;database=nameofdatabase” />

After setting web.config which the key is “ConnectionString”. Use this string as configuration manager application setting which the connection will be build with sql connection statement. The sample is like this :

Imports System.Data.SqlClient
Imports System.Data
Imports System.Data.OleDb

Partial Class _Default
    Inherits System.Web.UI.Page
    Dim strConString As String
    Dim conMyData As SqlConnection
    Dim cmd As SqlCommand
    Dim parm As SqlParameter
    Dim returnValue As Integer

    Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        If txtUser.Text = “” Or txtPwd.Text = “” Then
            lblMessage.Text = “Please, Fill All The Field”
            txtUser.Text = “”
            txtPwd.Text = “”
        ElseIf txtUser.Text <> “” And txtPwd.Text <> “” Then
            ‘ Create SQL command to execute spr_validasi_login
            strConString = System.Configuration.ConfigurationManager.AppSettings(“ConnectionString“)
            conMyData = New SqlConnection(strConString)
            cmd = New SqlCommand(“spr_validasi_login”, conMyData)
            cmd.CommandType = CommandType.StoredProcedure

            ‘ Create a billion passing parameters
            cmd.Parameters.Add(New SqlParameter(“@user”, OleDbType.VarChar)).Value = txtUser.Text
            cmd.Parameters.Add(New SqlParameter(“@pwd”, OleDbType.VarChar)).Value = txtPwd.Text

            ‘ Create a return parameter
            parm = cmd.Parameters.Add(“@return”, SqlDbType.Int)
            parm.Direction = ParameterDirection.Output


            returnValue = cmd.Parameters(“@return”).Value

            If returnValue = 1 Then
                Session(“UserActive”) = txtUser.Text
                txtUser.Text = “”
                txtPwd.Text = “”
                lblMessage.Text = “Username or Password Is Not Match”
                txtUser.Text = “”
                txtPwd.Text = “”
            End If

            ‘Catch Ex As Exception
            ‘    lblMessage.Text = “Username or Password Is Invalid”
            ‘    txtUser.Text = “”
            ‘    txtPwd.Text = “”
            ‘End Try
        End If
    End Sub

Good Luck, Have A Nice Try… ^^


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s