Controlling the SRC of an IFRAME from code behind.

Category : ASP.Net, C#, VB.Net

Make sure to add the ” runat=’server’ ” in the markup.

 myIFrame.Attributes["src"] = "http://www.google.com";

Cannot open database “” requested by the login. The login failed. Login failed for user ‘IIS APPPOOL\DefaultAppPool’.

Category : C#, VB.Net

Cannot open database “” requested by the login. The login failed.
Login failed for user ‘IIS APPPOOL\DefaultAppPool’.

I got this error when I added an app onto IIS 7 and was trying to hit my local Database. Here is the fix:

1) In IIS7 – Click ‘Application Pools’ (left side)


2) On Top Right – Click ‘Set Application Pool defaults’


3) Finally, set ‘Process Model > Identity’ to ‘Local System’

Finding Controls inside of a nested repeater.

Category : VB.Net

This one almost killed me… I have a nested repeater and could not for the life of me figure out how to find the controls in the nested repeater…

I was doing ONE thing wrong… instead of placing the ItemDataBound event declaration in the Parents ItemDataBound event… I had to put it in the ItemCreated event… and that was it.  After that I could easily find my controls.

Protected Sub RepeaterPARENT_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.RepeaterItemEventArgs) Handles RepeaterPARENT.ItemCreated
 
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim repeaterCHILD As Repeater = New Repeater
            repeaterCHILD = TryCast(e.Item.FindControl("RepeaterCHILD"), Repeater)
            AddHandler repeaterCHILD.ItemDataBound, AddressOf repeaterCHILD_ItemDataBound
        End If
End Sub
 
Protected Sub repeaterCHILD_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.RepeaterItemEventArgs)
 
   If e.Item.ItemType = ListItemType.Header Then
            Qty = 0.0
   End If
 
   If CType(e.Item.FindControl("HiddenQuantity"), HiddenField) IsNot Nothing Then
            Dim HiddenQuantity As HiddenField = CType(e.Item.FindControl("HiddenQuantity"),
 HiddenField)
            Qty += CDbl(HiddenQuantity.Value)
   End If
 
   If CType(e.Item.FindControl("LabelQTYSubtotal"), Label) IsNot Nothing Then
            Dim LabelNew As Label = New Label
            LabelNew = CType(e.Item.FindControl("LabelQTYSubtotal"), Label)
            LabelNew.Text = Qty.ToString
   End If
End Sub

Creating and manipulating a dataTable

Category : ASP.Net, VB.Net

This structure keeps coming up :

 dsSchools = mydata
 
'Create table to bind to dataGrid
Dim dt As New DataTable
Dim nameCol, activeCol As New DataColumn
dt.Columns.Add(nameCol)
dt.Columns.Add(activeCol)
Dim dRow As DataRow = dt.NewRow
 
For Each dr As DataRow In dsSchools.Tables(0).Rows
       If Not dr("title") Is DBNull.Value Then dRow(nameCol) = dr("title")
       If Not dr("IsActive") Is DBNull.Value Then dRow(activeCol) = dr("isActive").ToString
       dt.Rows.Add(dRow)
Next

Important difference between for each and for next loops.

Category : C#, VB.Net

For Each loops is a read-only loop! In other words, you cannot mutate the reference you are referring to.

A For Next loop allows mutation, plus I like For Next’s because they allow me to determine if I am on the final loop or not…

Examples,

For x as integer = 0 to myStringArray.Count – 1

myStringArray(x) += “add this to end”

if x = myStringArray.Count – 1 then myStringArray(x) += “, and this is the last element!”
Next

cheers!
t

Arraylists can hold and compare different kinds of data.`

Category : VB.Net

I JUST FOUND THIS INTERESTING, THIS MAY OR MAY NOT BE HELPFUL… BUT I STARTED PLAYING WITH AN ARRAYLIST IN VB AND FOUND OUT THAT YOU CAN HOLD MULTIPLE TYPES INSIDE OF A SINGLE ARRAY.

TAKE A LOOK AT THE CODE BELOW – SEE THAT I STORE A STRING, 2 INTEGERS, AND A BOOLEAN ALL WITHIN THE SAME ARRAYLIST. I THINK THAT’S COOL…BUT I’M A DORK.

EVEN COOLER, YOU WILL SEE THAT THE ARRAYLIST CAN MAKE DECISIONS BASED ON THE TYPE IT IS LOOKING AT… (IS THE BOOLEAN TRUE OR FALSE?, DOES THE ARRAYLIST CONTAIN THIS STRING?, WHAT IS THE RESULT OF ADDING THIS ITEM TO THIS ITEM, AND CONCATENATING A STRING AND AN INT)

I IMAGINE THIS IS PROBABLY A TERRIBLE PRACTICE BECAUSE YOU WOULD HAVE TO KNOW THE TYPE AT EACH ELEMENT, BUT INTERESTING NONE THE LESS.

 
        Dim arrList As New ArrayList
 
        Dim theString As String = "TR"
        Dim theInt As Integer = 54
        Dim theBool As Boolean = False
        Dim theInt2 As Integer = 46
 
        arrList.Add(theString)
        arrList.Add(theInt)
        arrList.Add(theBool)
        arrList.Add(theInt2)
 
        MessageBox.Show(arrList(0))
        MessageBox.Show(arrList(1))
        MessageBox.Show(arrList(2))
 
 
        'Now to tests
 
 
        MessageBox.Show(arrList.Contains("TE"))  'Should be false
        MessageBox.Show(arrList.Contains(54)) 'Should be true
        MessageBox.Show(arrList.Contains(False)) 'Should be true
        MessageBox.Show(arrList(1) + arrList(3))
        MessageBox.Show(arrList(0) & arrList(1))
 
 
 
        'MessageBox.Show(arrList(0))
        'MessageBox.Show(arrList(1))
        'MessageBox.Show(arrList(2))

CHEERS!
T

C# Vs. VB – Arrays….

Category : C#, VB.Net

I would just like to point out two areas where both C# and VB.Net beat the other one in the usage of arrays.

C# PRO:

You can redefine your array simply in C# by simply reinitializing the array:

//Declare array
int[ ] myInts = new int[20];
//Now reinitialize like this
myInts = new int[40];

In VB you have to reinitialize with the ReDim keyword

Dim myInts(20)
ReDim myInts(40)

Not a big deal but I give the better implementation to C# on this one…however.
===========================================

In VB.Net you can actually reinitialize an array AND MAINTAIN the data within the current array!
This is accomplished by using the ‘Preserve’ keyword. (this is not available in C#)

'Declare array
Dim myInts() As Integer = { 1,2,3,4 }
'Resize array and keep the data with the Preserve keyword
ReDim Preserve myInts(20)

Create a string that makes a table/columns/rows into a Dataset

Category : ADO / SQL, VB.Net

Here is one cool way to get a string into a dataset:

First build your string so that is correctly formatted XML. For instance:

Dim strXCustInfo As String = "<Root><CusTable><Name>Todd </Name><Address>123 Fourth Street</Address></CusTable>" & _
                                "<CusTable><Name>Dawn </Name><Address>432 Washington Ave</Address></CusTable></Root>"

Now that we have this, we need to convert the string into a Stringbuilder.

Dim srCustInfo As New StringReader(strXCustInfo )

Now create your dataset and call the .ReadXml method on the dataset to get the above string right into a ready to be stored dataset! WOOT!

 Dim ds As New DataSet
  ds.ReadXml(srCustInfo)

Hope this helps someone!

Filling a dropdown programatically in vb.net

Category : VB.Net

This is very simple but something I can never remember how to do!

 
'So we have a TABLE of data and want to put a title and value into a dropdown on our page.
For row As Integer = 0 To DT.Rows.Count - 1
            If Not DT.Rows(row).Item("Title") Is DBNull.Value And Not DT.Rows(row).Item("ID") Is DBNull.Value Then
 
                Dim a As New ListItem
                a.Value = districtDT.Rows(row).Item("Title")
                a.Text = districtDT.Rows(row2).Item("ID")
 
                myDropDown.Items.Add(a)
 
            End If
        Next

There you go!
t