Archive für Oktober 2007

VB.NET: Function Wordwrap in ASP.NET

VB.NET:  Function Wordwrap in ASP.NET 

 

 

Viele kennen vielleicht die PHP-Funktion  „wordwrap“. Stand heute gibt es eine solche Funktion nicht im .NET Framework – Zumindest entzieht sich das meiner Kenntnis. 

Gerade im Bereich HTML-Layout ist es problematisch Daten aus <TEXTAREA> Feldern vernünftig zu formatieren.  

 

Grund genug eine Funktion wordwrap selbst zu bauen: 

 

 

 

Public Function wordrap(ByVal currentText As String, ByVal_ maxCharsPerLine As Integer) As String 

    Dim returnString As String = “” 

    Dim rx As New Regex(“(\S{” & maxCharsPerLine.toString & “})”,_ RegexOptions.IgnoreCase) 

    returnString = rx.Replace(currentText, “$1 “) 

     Return returnString 

End Function 

 

 

 

War diese Information hilfreich? Dann würde ich mich über eine Verlinkung freuen. 

Was this Information helpfull? I’d be very pleased if you link my page. 

ASP.NET: Dropdownlist dynamisch mit Items füllen

  

ASP.NET:  Fill Dropdownlist dynamic with Items: 

 

 

Um eine ASP Dropdownlist mit Items zu füllen benötigt man ein neues ListItem. In folgendem Beispiel wird über eine FOR NEXT Schleife iteriert und entsprechende Items hinzugefügt. 

Items oder Strings mit führenden Nullen auffüllen:Fill Items or Strings with leading zeros: 

Um eine einheitliche Formatierung zu gewährleisten fülle ich einstellige Werte mit führenden Nullen auf. Dies geschieht mit der Funktion „“PadLeft()“ des String-Objekts. 

Im Beispiel verwende ich den Literalausdruck “0″c – Alternativ könnte man auch die chr(0) Funktion verwenden. 

 

Option Explicit On 

Option Strict On 

 

Imports microsoft.visualbasic 

Imports System.Web.UI.WebControls 

 

Dim i As Integer = 0 

Dim ddlDs As New System.Web.UI.WebControls.DropDownList 

Dim lItem As New System.Web.UI.WebControls.ListItem 

 

 

For i = 0 To 20 

 ddlDs.Items.Add(New ListItem(i.ToString.PadLeft(2, “0″c), i.ToString))

  

Next 

 

SQL Server 2000 Performance steigern mit ‘SET NOCOUNT ON’

Beim SQL Server kann man die Leistung der Commands steigern:

Bei fast jeder gespeicherten Prozedur sollte am Anfang SET NOCOUNT ON gesetzt sein. Es zeichnet einen guten Programmierstil aus, am Ende SET NOCOUNT OFF zu setzen. [Durch SET NOCOUNT ON wird verhindert, dass SQL Server für jede Anweisung in einer gespeicherten Prozedur DONE_IN_PROC-Meldungen an den Client sendet. – Hrsg.]. Dieser Standard gilt auch für Trigger.

Quelle: http://www.microsoft.com/germany/msdn/library/servers/sqlserver/TSQLProgrammierstandards.mspx?mfr=true

VB.NET: Problem mit Konstanten in Strukturen

Problem with Constants in Structure 

 

Folgende Struktur ist gegeben: 

 

 

    Public Structure pagingStructure1 

        Public Const itemsPerPage As Integer = 100 

        Public totalItems As Integer 

        Public currentPage As Integer 

        Public totalPageCount As Integer 

    End Structure 

 

 

 

Im Hauptprogramm verursacht die ROT markierte Zeile einen Fehler:(Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated) 

 

 

Public Shared Function getPagingObject()As pagingStructure1 

 

        Dim oSqlCommand As New SqlCommand(sqlStr) 

        oSqlCommand.Connection = getConnection() 

 

        Dim ps As pagingStructure1 

        ps.totalItems = CInt(oSqlCommand.ExecuteScalar()) 

 

        ps.totalPageCount = ps.totalItems \ ps.itemsPerPage 

 

        oSqlCommand.Connection.Close() 

        oSqlCommand.Connection.Dispose() 

 

        Return ps 

    End Function 

 

 

Wenn man allerdings über den namespace auf die Kostante zugreift, geht es: 

 

      ps.totalPageCount = ps.totalItems \ mdt.pagingStructure1.itemsPerPage 

 

 

Da ich das aber stilistisch nicht so gut finde, habe ich in der Deklaration eine ReadOnly-Property eingebaut: 

 

    Public Structure pagingStructure1 

        ‘Public Const itemsPerPage As Integer = 100 

        Public totalItems As Integer 

        Public currentPage As Integer 

        Public totalPageCount As Integer 

 

        Friend ReadOnly Property itemsPerPage() As Integer 

            Get 

                Return 100 

            End Get 

        End Property 

    End Structure 

 

 

 

 

Damit funktioniert jetzt auch der direkte Zugriff (oben ROT markiert)  

 

Eine Erklärung dafür habe ich nicht, in einem MS-Forum habe ich gelesen das es ein VS 2005 Bug sein soll, kann das zum jetzigen Zeitpunkt weder bestätigen, noch dementieren.  Wäre gut wenn jemand hierzu eine Referenz oder eine Erklärung posten könnte. 

 

Attribute border outdated

Warning Validation (XHTML 1.0 Transitional): Attribute ‘border’ is considered outdated. A newer construct is recommended.

In Visual Studio 2005 erscheint folgende Warnung:
“Warning Validation (XHTML 1.0 Transitional): Attribute ‘border’ is considered outdated. A newer construct is recommended.”
In meinem Beispiel hatte ich im Image-Tag folgendes Konstrukt:

<img height=”1″ alt=”" src=”images/black_dot.gif” mce_src=”images/black_dot.gif” width=”100%” border=”0″ />

Nach der neuen Microsoft Empfehlung macht man das jetzt so:


<img height=”1″ alt=”" src=”images/black_dot.gif” mce_src=”images/black_dot.gif” width=”100%” style=”border:none;” />

um einen Rahmen anzuzeigen so:

<img height=”1″ alt=”" src=”images/black_dot.gif” mce_src=”images/black_dot.gif” width=”100%” style=”border:7px;” />


Folge: Keine Warnmeldung mehr.

XHTML 1.0 Transitional

Elemente „ALT“

Elemente „Neu“

 

 

<menu />

Use another list type.

<u />

Use CSS property “text-decoration” and set its value to “underline”.

<s />

Use CSS property “text-decoration” and set its value to “line-through”.

<strike />

Use CSS property “text-decoration” and set its value to “line-through”.

<basefont />

Use CSS styling and set the “font-family” property to desired font.

<font />

Use CSS styling to set the property of the element, such as a DIV or SPAN.The CSS properties include:

  • color
  • font-family
  • font-size
  • text-decoration
  • text-align
  • text-indent
  • text-transform
  • font-weight

<applet />

Use the <object /> tag

<isindex /> 

Use the <input /> tag

<i />

Use the <em /> tag

<b />

Use the <strong /> tag

 

 

Elements / Attributes

Newer Constructs

 

 

Iframe

 

name

Use the ID attribute

align

Use the CSS property “text-align”

body

 

background

Use the CSS property “background-image” or “background”.

bgcolor

Use the CSS property “background-color” or “background”.

text

Use the CSS property “color” for the “body” style

link

Use the CSS section “a:link” and set the property “color”.

vlink

Use the CSS section “a:visited” and set the property “color”.

alink

“a:hover” and set the property “color”.

div / p / h1…h6

 

align

Use the CSS property “text-align”.

ul / ol / li

 

type  

Use the CSS property “list-style-type” or “list-style”.

compact      

This attribute has been removed from the HTML DOM 1.0

hr

 

align  

Use the CSS property “text-align”.

noshade      

Use the CSS property “color”.

size   

Use the CSS property “height”.

width 

Use the CSS property “width”.

pre

 

width 

Use the CSS property “width”.

name

Use the attribute “id”.

br

 

clear 

Use the CSS property “clear”.

object

 

align

Use the CSS property “vertical-align” and “text-align” in the parent tag

border

Use the CSS property “border”.

hspace

Use the CSS property “padding”.

vspace

Use the CSS property “padding”.

img

 

name

Use the attribute “id”.

align

Use the CSS property “vertical-align” and “text-align” in the parent tag

border

Use the CSS property “border”.

hspace

Use the CSS property “padding”.

vspace

Use the CSS property “padding”.

map

 

name

Use the attribute “id”.

input

 

align

Use the CSS property “vertical-align” and “text-align”

legend

 

name

Use the attribute “id”.

table

 

align

Set table’s CSS properties left-margin and right-margin at “auto” for centering, or right-margin to 0 for right alignment, or the left-margin to 0 for left alignment.

bgcolor

Use the CSS property “background-color”.

tr

 

bgcolor       

Use the CSS property “background-color”.

td, th

 

nowrap

Use the CSS property “word-wrap”.

bgcolor

Use the CSS property “background-color”.

width 

Use the CSS property “width”.

height

Use the CSS property “height”.

 

Attribute bgcolor is considered outdated

Warning 1 Validation (XHTML 1.0 Transitional): Attribute ‘bgcolor’ is considered outdated. A newer construct is recommended.  

In Visual Studio erscheint folgende Warnung:
“Warning 1 Validation (XHTML 1.0 Transitional): Attribute ‘bgcolor’ is considered outdated. A newer construct is recommended.”

Im Body-Tag hatte ich folgendes Konstrukt:

                <body bgcolor=”#FFFFFF”>

Dieses kann man mit diesem String ersetzen:

                  <body style=”background-color:#FFFFFF”>

Folge: Die Warnmeldung verschwindet.

Trans SQL Datum Zeit konvertieren

T-SQL: Datum und Zeit in einer Abfrage konvertieren

Convert DateTime Beispiel:

select count(mID) from dataArchive where Erfassungszeit > convert(datetime,’2007-06-26 00:00:01.000′,121 ) AND Erfassungszeit < convert(datetime,’2007-06-26 23:59:59.000′,121 )

SELECT über mehrere Datenbanken

Datenbankabfrage über mehrere Datenbanken

zum Beispiel:
use pubs

select a.au_fname, a.au_lname,
e.FirstName, e.LastName
from authors as a
join Northwind.dbo.Employees as e
on (a.au_fname = e.FirstName)

SQL Server 2000: Transaktionsprotokoll verkleinern

Im MS-Queryanalyzer folgenden Befehl absetzen:

DBCC SHRINKFILE(pubs_log, 2)

Wobei “pubs_log” mit “MeinDatenbankName_log” ersetzt wird.

Details stehen auch hier: http://support.microsoft.com/kb/272318/

net use fuer Drucker oder Verzeichnisse

Den Befehl “net use” kann man verwenden um automatisch Zugriff auf Netzwerressourcen zu erhalten:
 

Zum Beispiel:

net use lpt2 \\192.168.160.1\HPLASERJET meinSicheresPasswort /user:administrator

 In diesem Beispiel wird der freigegebene Drucker HPLASERJET an den lokalen Port LPT2 connected.

Read Contenttype

VB.NET - Vor dem Webrequest den Contenttype lesen

Häufig könnte es nützlich sein vor dem Lesen der Daten den ContentType zu kennen.
Diese Variante habe ich mir ausgedacht um das zu handeln.

Für Verbesserungsvorschläge wäre ich sehr Dankbar! -> ralf.reinartz@web.de

Erst die Imports:
‘http://msdn2.microsoft.com/de-de/library/system.net.mime.contenttype(VS.80).aspx
‘Für den Contenttype wird die Mime-Klasse benötigt:
Imports System.Net.Mime

Imports
System.Web.HttpUtility
Imports System.Diagnostics
Imports System.IO
Imports System.Net
Imports System.Text

…….

Dim IoStream As System.IO.Stream
Dim StrRead As System.IO.StreamReader

‘ Einen WebRequest für den URL erzeugen
Dim wRequest As System.Net.HttpWebRequest = CType(System.Net.WebRequest.Create(URL), System.Net.HttpWebRequest)


……

Dim oContentType As ContentType
oContentType =
New ContentType(wRequest.GetResponse.ContentType)

Dim charset As String
charset = oContentType.CharSet()

If charset = “” Then
charset = “iso-8859-1″
End If

StrRead = New System.IO.StreamReader(IoStream, System.Text.Encoding.GetEncoding(charset), False)


……
StrRead.Close()
IoStream.Close()

|