Monday 28 July 2008

ConfigurationManager not recognized in console application

So you follow best practice advice and store your database connection string in your console applications app.config file. Good so far, except when you try to access the setting in code you get ConfigurationManager not recognized!

Solution Add a reference to System.ConfigurationManager in your project references.

E.g.

Public ReadOnly Property conString() As String
Get
Return ConfigurationManager.ConnectionStrings("your connection string Name").ConnectionString
End Get
End Property

Thursday 24 July 2008

ASP.NET Not Working - XML Parsing Error: not well-formed

God bless this guy:

http://theshiva.us/technicalblog/archive/2007/03/26/fix-for-asp-net-xml-parsing-error-not-well-formed-line-number-1-column-2.aspx

He gives a no bs explanation of what to do if when you set up an ASP.NET web application on a server you see this:

XML Parsing Error: not well-formed etc....


ASP.NET 2.0 Framework

%Windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -i

ASP.NET 1.1 Framework

%Windir%\Microsoft.NET\Framework\v1.1.4322\aspnet_regiis.exe -i

Wednesday 23 July 2008

Skin Up - Using Skin Files to set Appearance of controls.

OK here's what I've got so far.

  1. Create a new skin file and save it
  2. Design your control layout on a test page
  3. Paste the finished formatting code into your skin file
  4. Set the Theme attribute in the Page Directive to point at your new Skin.
  5. Use it and re-use it.
mf...

DaysInMonth - get the number of days in a month

' get number of days in current month
Dim days As Integer = Date.DaysInMonth(Now.Year, Now.Month)
Me.lblDays.Text = days

Format a number with comma seperators

'Format a number to have commas
Dim bla As String = "1000000000"
Dim myNum As Integer
myNum = CType(bla, Integer)
Me.lblStrNumber.Text = Format(myNum, "n")

Friday 18 July 2008

Login failed for user NT AUTHORITY\NETWORK SERVICE

I've just been banging my head against a brick wall, and unfortunately, trawling through a lot of nonsense written on various forums about what to do when you see this:

Cannot open database "slcms" requested by the login. The login failed.
Login failed for user 'NT AUTHORITY\NETWORK SERVICE'.


Let's say your connection string looks like this:

Data Source=MYHOST;Initial Catalog=MyDatabase;Integrated Security=True;User ID=myUser;Password=myPassword

This: Integrated Security=True; and this: User ID=myUser;Password=myPassword are mutually exclusive.

Integrated security means 'use my domain credentials'

User ID=myUser;Password=myPassword means 'use these SQL credentials'

So SQL Server can't decide between them.

Solution: Remove this bit : Integrated Security=True;

Wednesday 2 July 2008

GridView - Delete Button

Here's an example of adding a Delete Button to a GridView control.

Ok so let's say you have a DataTable of Contacts bound to a GridView control called gvContacts.
Your DataTable has three column contactID,firstname, lastname.

'The Grid


> DataKeyNames="contactID"< AutoGenerateColumns="false"
<columns>
<asp:boundfield datafield="Contact" headertext="firstname">
<asp:templatefield headertext="firstname">
<itemtemplate>
<asp:label id="firstname" runat="server" text="'<%#">'></asp:Label><
</itemtemplate><
</asp:TemplateField><
<asp:templatefield headertext="lastname"><
<itemtemplate><
<asp:label id="lastname" runat="server" text="'<%#">'></asp:Label><
</itemtemplate><
</asp:TemplateField><
<asp:templatefield headertext="Delete"><
<itemtemplate><
><asp:button commandname="Delete" id="btnDelete" text="Delete Contact" commandargument=" runat="server" />
</itemtemplate><
</asp:TemplateField><
</columns><


'The Row Deleting Event is the bit that actually does the delete operation

Protected Sub gvContacts_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvContacts.RowDeleting

Dim contactID As Integer = Me.gvContacts.DataKeys(e.RowIndex).Value
DeleteContact(ContactID)

End Sub

'Here we add a clientside confirmation script to be kind to the user. When the Delete button is clicked an alert is triggered asking the user
' if they are sure they want to delete the record.

Protected Sub gvContacts_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvContacts.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim b As Button = e.Row.FindControl("btnDelete") 'CType(e.Row.Cells(4).Controls(0), Button)
b.OnClientClick = String.Format("return confirm('Please confirm you wish to delete this record ?');")
End If

End Sub