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 :

<appSettings>
<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
            ‘Try
            ‘ 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

            conMyData.Open()
            cmd.ExecuteNonQuery()

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

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

            conMyData.Close()
            ‘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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s