Archive for tag: ASP.NET

Loop DataTable

How to loop items in a DataTable

        Dim sbTable As New StringBuilder

        Dim DT As DataTable = DS.Tables("Meta") 'Get your DataTable somehow

        Dim y As Integer = 0

        Dim n As Integer = 0

        sbTable.Append("<table border=""1"">")


        For y = 0 To DT.Columns.Count - 1

            sbTable.Append("<th>" & HttpUtility.HtmlEncode(DT.Columns.Item(y).ColumnName) & "</th>")



        For n = 0 To DT.Rows.Count - 1


            For y = 0 To DT.Columns.Count - 1

                sbTable.Append("<td>" & HttpUtility.HtmlEncode(DT.Rows(n).Item(y)) & "</td>")





Read AppSettings in web.config

How to read appSettings values from web.config.


    <add key="KeyName" value="The value here"/>



Dim s As String = System.Configuration.ConfigurationManager.AppSettings("KeyName")

Css and JavaScript Urls

Including javascript and css can be a pain if not all pages are placed in the same paths relative to the css and javascript files. Here's a way to avoid the problem.

    <link id="common-css" runat="server" href="~/css/common.css" rel="stylesheet" />

    <script src="<%= ResolveUrl("~/js/jquery-1.9.1.min.js")%>" type="text/javascript"></script>

Get Computer Name

Dim host As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(Request.ServerVariables.Item("REMOTE_HOST"))

Dim strComputerName As String = host.HostName

Literal1.Text = strComputerName

ADODBD connection from ConnectionString

Dim connectionStringName As String = "TestConn"

Dim s As New StringBuilder

Dim a As ConnectionStringSettings = ConfigurationManager.ConnectionStrings(connectionStringName)



s.Append("ConnectionString: " & a.ConnectionString & "<br />")

s.Append("Name: " & (a.Name & "<br />"))

s.Append("ProviderName: " & a.ProviderName & "<br />")



Dim sqlConn As String = ConfigurationManager.ConnectionStrings(connectionStringName).ConnectionString

Dim connBuilder As SqlConnectionStringBuilder = New SqlConnectionStringBuilder(sqlConn)

s.Append("DataSource: " & connBuilder.DataSource & "<br />")

s.Append("InitialCatalog: " & connBuilder.InitialCatalog & "<br />")

s.Append("UserID: " & connBuilder.UserID & "<br />")

s.Append("Password: " & connBuilder.Password & "<br />")

s.Append("<hr />")



' Build an ADODBD connection string

Dim d As New StringBuilder


d.Append(String.Format("Data Source={0};", connBuilder.DataSource))

d.Append(String.Format("Initial Catalog={0};", connBuilder.InitialCatalog))

d.Append(String.Format("User Id={0};", connBuilder.UserID))

d.Append(String.Format("Password={0};", connBuilder.Password))

d.Append("<hr />")



Literal2.Text = s.ToString & d.ToString

Trapping HttpRequestValidationException

Protected Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Error
If Server.GetLastError.GetType Is GetType(System.Web.HttpRequestValidationException) Then
Response.Redirect("ErrorTrap.aspx?Err=Naughty chars entered!")
'Response.Write("You entered inappropriate characters.<br>")
'Response.Write("Start Over or click Back.")
End If
End Sub

Parsing pubDate in RSS

When reading data from an RSS feed using XMLdataSources and DataLists you often get very confused when it comes to parsing the pubDate. Below is an example showing how it can be done.

<%# String.Format("{0:g}", DateTime.Parse(XPath("pubDate")))%>