Hello,
I'm trying to solve this problem for days but I can't figure out where i'm going wrong.
I have written two classes to retrieve data from a SQL Server database; ContentDB.vb and ContentDetails.vb.
In ContentDB there are, amongst others, two methods, GetMainMenu and GetContent.
GetMainMenu gives a list of items and works great. When I try to use GetContent (which should get me 1 single result) from codebehind, I get a NullReferenceException error.
My code in ContentDB:
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Imports DataPackage.CMS
Namespace DataUtility.CMS
Public Class ContentDB
Private connectionString As String
Public Sub New()
' De standaard connectionstring ophalen
connectionString = WebConfigurationManager.ConnectionStrings("csPlantRepository").ConnectionString
End Sub
Public Sub New(ByVal connectionString As String)
' een specifieke connectiestring ophalen
Me.connectionString = connectionString
End Sub
Public Function GetMainMenu(ByVal SiteID As Integer) As List(Of ContentDetails)
Dim conn As New SqlConnection(connectionString)
Dim cmd As New SqlCommand("SELECT [contentID], [titel], [ouderID], [siteID], [auteur], [url], [volgnummer] FROM [CMS_Content] WHERE (([siteID] = 1) AND ([ouderID] = 0)) ORDER BY [volgnummer]", conn)
cmd.CommandType = CommandType.Text
'cmd.Parameters.Add(New SqlParameter("@siteID", SqlDbType.Int, 4))
'cmd.Parameters("@siteID").Value = SiteID
'cmd.Parameters.Add(New SqlParameter("@ouderID", SqlDbType.Int, 4))
'cmd.Parameters("@ouderID").Value = 0
Dim menuList As New List(Of ContentDetails)
Try
conn.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
Do While reader.Read()
Dim content As New ContentDetails(CInt(reader("contentID")), CStr(reader("titel")), CInt(reader("ouderID")), CInt(reader("siteID")), CStr(reader("auteur")), CStr(reader("url")), CInt(reader("volgnummer")))
menuList.Add(content)
Loop
reader.Close()
Return menuList
Catch ex As Exception
Finally
conn.Close()
End Try
End Function
Public Function GetContent(ByVal siteID As Integer) As ContentDetails
Dim conn As New SqlConnection(connectionString)
Dim cmd As New SqlCommand("SELECT TOP 1 contentID, titel, ouderID, siteID, auteur, url, volgnummer WHERE siteID = @siteID ORDER BY volgnummer", conn)
cmd.CommandType = CommandType.Text
cmd.Parameters.Add(New SqlParameter("@siteID", SqlDbType.Int, 4))
cmd.Parameters("@siteID").Value = siteID
Try
conn.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleRow)
reader.Read()
Dim item As New ContentDetails(CInt(reader("contentID")), CStr(reader("titel")), CInt(reader("ouderID")), CInt(reader("siteID")), CStr(reader("auteur")), CStr(reader("url")), CInt(reader("volgnummer")))
reader.Close()
Return item
Catch ex As Exception
Finally
conn.Close()
End Try
End Function
End Class
End Namespace
My code in Codebehind:
Imports System
Imports System.Data
Imports DataPackage.CMS
Imports DataUtility.CMS
Partial Class content
Inherits System.Web.UI.Page
Dim websiteID As Integer = "1"
Dim redirectString As String = "Default.aspx"
Private db As ContentDB = New ContentDB()
Private file As ContentDetails = New ContentDetails()
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
websiteID = Request.QueryString("siteID")
Session("siteID") = 0
Select Case websiteID
Case 1
file = db.GetContent(websiteID)
Session("siteID") = websiteID
Dim bestandsnaam As String = file.url.ToString
redirectString = "iDFM/" & bestandsnaam
Response.Redirect(redirectString)
Case 2
file = db.GetContent(websiteID)
Session("siteID") = websiteID
redirectString = "Wiki/" & file.url.ToString
Response.Redirect(redirectString)
Case 3
file = db.GetContent(websiteID)
Session("siteID") = websiteID
redirectString = "WSA/" & file.url.ToString
Response.Redirect(redirectString)
Case Else
redirectString = "Account/FoutmeldingWebsite.aspx"
Response.Redirect(redirectString)
End Select
End Sub
End Class
The error is caused bij "file.url.ToString" in the codebehind.
Does anyone have an idea which dumb rookie mistake i'm not seeing?
Thanks, martin