Jag håller på att göra en sajt där informationen som visas baseras på vilken info som tas emot från default.aspx?category=xxx&subcat=xxx och har nu stött på ett fel i samband med codebehind. Dim tbl_news As Object Tack så mycket! Ska testa det senare. Felet ligger i att tbl_news inte blivit initialiserat. Får jag fråga varför du inte använder webb-kontroller ístället för att inkludera sidor? :)Codebehind och "Object variable or With block variable not set"
Så här ser min codebehindsida ut:
news.aspx.vb
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.Page
Imports System.Web.UI.Control
Imports System.Data
Imports System.Data.OleDb
Public Class PL : Inherits Page
Sub Page_Load
Dim tbl_news As object
Dim Cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("dbms\db_news.mdb"))
Dim SQL As string = "SELECT n_name,n_news,n_date FROM tbl_news"
Dim Cm As New OleDbCommand(SQL,Cn)
Dim dbRead As OleDbDataReader
Dim Container As object
Cn.Open()
dbRead = Cm.ExecuteReader()
tbl_news.DataSource = dbRead '<--- det är där felet ligger!!
tbl_news.DataBind()
dbRead.Close()
Cn.Close()
End Sub
End Class
Sidan som klassen och infon från databasen ska visas på heter main.aspx och ser ut så här:
<% Page Language="VB" Strict="False" Debug="True" inherits="PL" src="behind/news.aspx.vb" %>
<span class="head">Startsidan</span>
<form id="news" runat="server">
<asp:repeater id="tbl_news" runat="server">
<ItemTemplate><%# Container.DataItem("n_date") %><br />
<%# Container.DataItem("n_news") %><br />
<%# Container.DataItem("n_name") %><br /><br /></ItemTemplate>
</asp:repeater>
</form>
Slutligen har vi sidan default.aspx, där, beroende på info i subcat-querystringen, exekveras en sida. Så här ser default.aspx ut:
<% Page Language="VB" Strict="True" Debug="True" %>
<%
Dim cat As string = Request.Querystring("category")
Dim subcat As string = Request.Querystring("subcat")
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Predictive Sales</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="style.css" type="text/css" rel="stylesheet" />
</head>
<body>
<table cellspacing="1" cellpadding="2" border="0">
<tbody>
<tr>
<td><img alt="Predictive Sales" src="gfx/logo.gif" border="0" /></td>
</tr>
<tr>
<td>
<table cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td valign="top">
<!--#include file="menu.aspx"-->
</td>
<td width="5"></td>
<td valign="top" width="600">
<%
Select Case subcat
Case ""
Server.Execute("main.aspx")
Case "main"
Server.Execute("main.aspx")
Case "company"
Server.Execute("about/aboutcompany.aspx")
Case "companyculture"
Server.Execute("about/companyculture.aspx")
Case "management"
Server.Execute("about/management.aspx")
Case "vdspeaks"
Server.Execute("about/vdspeaks.aspx")
Case "aboutservices"
Server.Execute("services/aboutservices.aspx")
Case "fieldsales"
Server.Execute("services/fieldsales.aspx")
Case "telesales"
Server.Execute("services/telesales.aspx")
Case "telemarketing"
Server.Execute("services/telemarketing.aspx")
Case "salesconsulting"
Server.Execute("services/salesconsulting.aspx")
Case "abouttechnology"
Server.Execute("tech/abouttechnology.aspx")
Case "predictivedialer"
Server.Execute("tech/predictivedialer.aspx")
Case "telestarcms"
Server.Execute("tech/telestarcms.aspx")
Case "aboutcustomers"
Server.Execute("customers/aboutcustomers.aspx")
Case "canaldigital"
Server.Execute("customers/canaldigital.aspx")
Case "foraldranatet"
Server.Execute("customers/foraldranatet.aspx")
Case "ap-fastigheter"
Server.Execute("customers/ap-fastigheter.aspx")
Case "monster"
Server.Execute("customers/monster.aspx")
Case "staffphilosophy"
Server.Execute("recruitment/staffphilosophy.aspx")
Case "workforus"
Server.Execute("recruitment/workforus.aspx")
Case "unoccupiedjobs"
Server.Execute("recruitment/unoccupiedjobs.aspx")
Case "contact"
Server.Execute("contact.aspx")
Case Else
Server.Execute("404.aspx")
End Select
%>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</body>
</html>
Nu är det så att jag får följande problem när jag kör default.aspx?category=main&subcat=main och alltså exekverar main.aspx och den sidans codebehind-sida. Följande felmeddelande genereras:
Object variable or With block variable not set.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object variable or With block variable not set.
Source Error:
Line 24: dbRead = Cm.ExecuteReader()
Line 25:
Line 26: tbl_news.DataSource = dbRead
Line 27: tbl_news.DataBind()
Line 28:
Source File: e:\web\pred\behind\news.aspx.vb Line: 26
Stack Trace:
[NullReferenceException: Object variable or With block variable not set.]
Microsoft.VisualBasic.CompilerServices.LateBinding.InternalLateSet(Object o, Type& objType, String name, Object[] args, String[] paramnames, Boolean OptimisticSet, CallType UseCallType) +1994
Microsoft.VisualBasic.CompilerServices.LateBinding.LateSet(Object o, Type objType, String name, Object[] args, String[] paramnames) +28
PL.Page_Load() in e:\web\pred\behind\news.aspx.vb:26
System.Web.Util.ArglessEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +10
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +731
Felet ligger alltså på rad 26 och härrör till tbl_news.DataSource = dbRead.
Nu undrar jag vad problemet är, samt hur man löser det. MYCKET tacksam för hjälp. :DSv: Codebehind och "Object variable or With block variable not set"
Bör vara:
Dim tbl_news As System.Web.UI.WebControls.Repeater
eller:
Protected WithEvents tbl_news As System.Web.UI.WebControls.Repeater
//Johan NSv: Codebehind och "Object variable or With block variable not set"
Sv: Codebehind och "Object variable or With block variable not set"
Du måste minst deklarera det som New Object.
//Mikael.NETSv: Codebehind och "Object variable or With block variable not set"