Navigation

Search

Categories

On this page

LINQ to SQL Samples In VB.NET
How to generate a random password
Updating or Deleting a List of Records
Calling a Function in VB.NET
Removing Duplicate Values from a List
How to generate a random number
Clearing Form Fields
Removing Duplicate Values from a String

Archive

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 240
This Year: 46
This Month: 3
This Week: 0
Comments: 0

Sign In
Pick a theme:

# Monday, June 14, 2010
Monday, June 14, 2010 9:27:21 PM (GMT Daylight Time, UTC+01:00) ( LINQ | VB.NET )


I’m trying to learn LINQ and came across this page of useful examples in VB

http://msdn.microsoft.com/en-us/vbasic/bb688085.aspx

Comments [0] | | # 
# Tuesday, June 01, 2010
Tuesday, June 01, 2010 8:52:04 PM (GMT Daylight Time, UTC+01:00) ( VB.NET )


Public Function GeneratePassword(ByVal PwdLength As Integer) As String
    Dim _allowedChars As String = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789"
    Dim rndNum As New Random()
    Dim chars(PwdLength - 1) As Char
    Dim strLength As Integer = _allowedChars.Length
    For i As Integer = 0 To PwdLength - 1
        chars(i) = _allowedChars.Chars(CInt(Fix((_allowedChars.Length) * rndNum.NextDouble())))
    Next i
    Return New String(chars)
End Function

 
Comments [0] | | # 
# Wednesday, March 31, 2010
Wednesday, March 31, 2010 3:51:30 PM (GMT Daylight Time, UTC+01:00) ( ASP.NET | SQL | VB.NET )


This is a nice example of how to pass a delimitted list of values to SQL Server and perform some kind of dbase function with those whether that be an UPDATE, DELETE or whatever.

First here is the sample stored procedure we are going to call updating each company sent to it by the PK value, id_company

USE [my_dbase]
GO
CREATE PROCEDURE [dbo].[p_UpdateCompany] 
    @id_assign int = NULL
AS
BEGIN

    SET NOCOUNT ON;

    UPDATE Companies 
    SET markedfordeletion = 1 --or whatever could be a delete statement as well    

END

Next, here is the VB.NET code used to call the sproc

Protected Sub btnCompanySubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim strConnection As SqlConnection
        Dim strSql As String
        Dim cmd As SqlCommand
        Dim CompanyValues As String = txtid_company.Text
        
        Dim CompanyList As New ArrayList
        CompanyList.AddRange(Split(CompanyValues, ","))
        
        strConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("conString_Repository").ConnectionString)
        strSql = "p_UpdateCompany"
        cmd = New SqlCommand(strSql)
        cmd.CommandType = CommandType.StoredProcedure
        
        For i As Integer = 0 To CompanyList.Count - 1
            Dim CompanyVal As Integer = CompanyList(i)
            
            cmd.Parameters.Add("@id_Company", SqlDbType.Int, CompanyVal)
            Try
                strConnection.Open()
                cmd.Connection = strConnection
                cmd.ExecuteNonQuery()
            Finally
                strConnection.Close()
            End Try
        Next
    End Sub

<form id="form1" runat="server">
    <div>
        <table id="Companies">
            <tr>
                <td>Enter id_company values:<br />
                    (separated by commas)</td>
                <td><asp:TextBox ID="txtid_company" runat="server" TextMode="MultiLine" Rows="5" Columns="25" /></td>
            </tr>           
            <tr>
                <td colspan="2">
                    <asp:Button ID="btnCompanySubmit" runat="server" onclick="btnCompanySubmit_Click" Text="Submit" />
                </td>
            </tr>           
        </table>
    </div>
    
    </form>
Comments [0] | | # 
# Wednesday, March 17, 2010
Wednesday, March 17, 2010 6:06:50 PM (GMT Standard Time, UTC+00:00) ( ASP.NET | VB.NET )


lblCompanyName.Text = GetCompanyName(intid_company).ToString()


Public Shared Function GetCompanyName(ByVal id_company As Integer) As String
        Dim result As String = String.Empty
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("myConnString").ConnectionString)

        Dim cmd As New SqlCommand("getCompanyName", con)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@id_company", id_company)
        Using con
            con.Open()
            Dim reader As SqlDataReader = cmd.ExecuteReader()
            If reader.Read() Then
                result = CType(reader("CompanyName"), String)
            End If
        End Using
        Return result
End Function
Comments [0] | | # 
# Wednesday, January 06, 2010
Wednesday, January 06, 2010 6:39:30 PM (GMT Standard Time, UTC+00:00) ( ASP.NET | VB.NET )


This is an example of how to remove duplicate values from a list or string separated by commas. You will also need to import the System.IO namespace to use the StringBuilder method: <%@ Import Namespace="System.IO" %>

'lblIDList contains a list of values separated by commas
'12,58,102,12,99,87,87

Dim values As String
values = lblIDList.Text
                
'Remove duplicates
values = RemoveDuplicates(values)                
                
'Reassign the lblIDList value without the duplicates
lblIDList.Text = values

Public Function RemoveDuplicates(ByVal items As String) As String
            Dim Result As StringBuilder = New StringBuilder()
            Dim newArray As Array
    
            newArray = Split(items, ",")
            For i As Integer = 0 To newArray.Length - 1
                If Result.ToString.IndexOf(newArray(i).ToString()) = -1 Then
                    Result.Append(newArray(i).ToString() & ",")
                End If
            Next
            Return Result.ToString.Substring(0, Result.ToString.LastIndexOf(","))
End Function
Comments [0] | | # 
Wednesday, January 06, 2010 6:32:18 PM (GMT Standard Time, UTC+00:00) ( ASP.NET | VB.NET )


An example of how to generate a random number in VB.NET

intLowerBound = 0
intUpperBound = 1000      
'Get the random number and display it in lblRandomNumber
lblRandomNumber.Text = GetRandomNumberInRange(intLowerBound, intUpperBound)

Function GetRandomNumberInRange(intLowerBound As Integer, intUpperBound As Integer)
                
    Dim RandomGenerator As Random
    Dim intRandomNumber As Integer

    ' Create and init the randon number generator
    RandomGenerator = New Random()

    ' Get the next random number
        intRandomNumber = RandomGenerator.Next(intLowerBound, intUpperBound + 1)
                
    ' Return the random # as the functions return value
    GetRandomNumberInRange = intRandomNumber
                        
End Function
Comments [0] | | # 
# Friday, December 04, 2009
Friday, December 04, 2009 3:26:50 AM (GMT Standard Time, UTC+00:00) ( ASP.NET | VB.NET )

 

Here is a server-side approach to clearing all form fields on a form when a button is clicked.

    Protected Sub btnClearFields_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        'First clear all form fields of any existing values
        EmptyTextBoxValues(Me)
    End Sub

    Private Sub EmptyTextBoxValues(ByVal parent As Control)
        For Each c As Control In parent.Controls
            If (c.Controls.Count > 0) Then
                EmptyTextBoxValues(c)
            Else
                If TypeOf c Is TextBox Then
                    CType(c, TextBox).Text = ""
                End If
            
                If TypeOf c Is Dropdownlist Then
                    CType(c, Dropdownlist).SelectedValue = ""
                End If
                
                If TypeOf c Is ListBox Then
                    CType(c, ListBox).SelectedValue = ""
                End If
                
                If TypeOf c Is CheckBox Then
                    CType(c, CheckBox).Checked = False
                End If
            End If
        Next
    End Sub
Comments [0] | | # 
# Wednesday, September 02, 2009
Wednesday, September 02, 2009 2:04:06 PM (GMT Daylight Time, UTC+01:00) ( ASP.NET | VB.NET )


I’ve been working on a project recently where I needed to prevent users from adding the same value twice to a string.

Here is a working demo.

<script runat="server" type="text/VB">

    Sub btnRemoveDupes_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim dupeValues As String
        dupeValues = lblWithDupes.Text
                
        'Remove duplicates
        dupeValues = RemoveDuplicates(dupeValues)
                
        'Reassign the label value without the duplicates
        lblNoDupes.Text = dupeValues
    End Sub


    Public Function RemoveDuplicates(ByVal items As String) As String
        Dim Result As StringBuilder = New StringBuilder()
        Dim newArray As Array
    
        newArray = Split(items, ",")
        For i As Integer = 0 To newArray.Length - 1
            If Result.ToString.IndexOf(newArray(i).ToString()) = -1 Then
                Result.Append(newArray(i).ToString() & ",")
            End If
        Next
        Return Result.ToString.Substring(0, Result.ToString.LastIndexOf(","))
    End Function

</script>

<form id="form1" runat="server">
    <div>
    
    Enter a comma delimited string with some duplicate values:<br />
    <asp:TextBox runat="server" ID="lblWithDupes" Width="400px" />
    <br /><br />
    
    The result without the duplicate values is:<br />
    <asp:Label runat="server" ID="lblNoDupes" /> <br /><br />
       
    <asp:Button ID="btnRemoveDupes" runat="server" Text="Button" OnClick="btnRemoveDupes_Click" /> 
    </div>
</form>
Comments [0] | | #