Hej! Problemet är löst, hade inte initierat choose_cat. Dropdownlistan är inte längre tom, jag satte den utanför Reapeatern. :)System.NullReferenceException och dropdownlist
Jag får ett felmeddelande hela tiden när jag ska redigera en länk i mitt länkscript. Jag använder mig av en dropdownlista för att visa kategorier, och binder data till den.
På default.aspx så har jag en codebehind-sida som heter links.vb, och klassen edit_link som ser ut så här:
<code>Public Class edit_link : Inherits UserControl
protected withevents choose_cat as dropdownlist
protected Withevents tbl_links_edit as repeater
Function fix(str)
str = Replace(str,"'","")
fix = str
End Function
Sub Page_Load(sender as object, e as eventargs)
choose_category()
edit()
End Sub
'lista kategorier
Sub choose_category()
'plocka ut unika kategorier ut länktabellen
Dim SQL As string = "SELECT DISTINCT category FROM tbl_links"
Dim Cn As OleDbConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & Server.MapPath("db_links.mdb"))
Dim Cm As OleDbCommand = new OleDbCommand(SQL,Cn)
Cn.Open()
'skapa ett OleDbDataReader-objekt
Dim objDataReader As OleDbDataReader = Cm.ExecuteReader()
'skapa en ny samling av listposter (option-val i dropdownlistan i default.aspx)
Dim ddlBox As new ListItemCollection
ddlBox.Add(new ListItem("Välj"))
ddlBox.Add(new ListItem())
while(objDataReader.Read())
'lägg till val i kategoridropdownlistan
ddlBox.Add(new ListItem(objDataReader("category").toString()))
end while
'bind data till dropdownlistan
choose_cat.DataSource = ddlBox
choose_cat.DataBind()
'stäng objekt
objDataReader.Close()
Cn.Close()
End Sub
'redigera länk
Sub edit()
Dim Cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & Server.MapPath("db_links.mdb"))
Dim SQL As string = "SELECT url,sitedescription FROM tbl_links WHERE Id=" & fix(Request.Querystring("id"))
Dim Cm As New OleDbCommand(SQL,Cn)
Dim dbRead As OleDbDataReader
Dim Container As object
Cn.Open()
dbRead = Cm.ExecuteReader()
tbl_links_edit.DataSource = dbRead
tbl_links_edit.DataBind()
dbRead.Close()
Cn.Close()
End Sub
'utför redigering av länk
Sub performedit(sender as object,e as eventargs)
Dim eUrl As string = fix(Request.Form("url"))
Dim eDescription As string = fix(Request.Form("description"))
Dim eCategory As string = choose_cat.SelectedItem.toString
If eCategory = "" Then eCategory = fix(Request.Form("alt_category"))
Dim SQL As string = "UPDATE tbl_links SET URL='" & eURL & "',SiteDescription='" & eDescription & "' WHERE Id=" & fix(Request.Querystring("id"))
Dim Cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & Server.MapPath("db_links.mdb"))
Dim Cm As New OleDbCommand(SQL,Cn)
Cm.Connection.Open()
Cm.ExecuteNonQuery()
Cn.Close
Response.Redirect("default.aspx?action=edit&id=" & Request.Querystring("id") & "&cat=" & Request.Querystring("cat"))
End Sub
End Class</code>
i default.aspx så laddar jag användarkontrollen edit.ascx, och koden i användarkontrollen ser ut så här:
<code><%@ Control Language="VB" Inherits="edit_link" src="links.vb" %>
<asp:repeater id="tbl_links_edit" runat="server">
<HeaderTemplate>
<span class="head">Redigera länk</span>
</HeaderTemplate>
<ItemTemplate>
<p>
URL:
<br />
<input type="text" size="40" value='<%# Container.DataItem("url") %>' name="url" />
<br />
<br />
Namn:
<br />
<input type="text" size="40" value='<%# Container.DataItem("SiteDescription") %>' name="description" />
<br />
<br />
Kategori:
<br />
<asp:DropDownList id="choose_cat" runat="server"></asp:DropDownList>
<input type="text" size="30" name="alt_category" />
</p>
<asp:button onClick="performedit" text="Uppdatera" runat="server" />
</ItemTemplate>
</asp:repeater>
</code>
Felmeddelandet jag får kan ses här: http://www.dileno.hel.nu/l3/default.aspx?action=edit&id=89&cat=Litteratur
Jag har tre andra klasser till i links.vb och exakt samma kod eller likadan kod där fungerar problemfritt.
Klassen links ser ut så här:
<code>Public Class links : Inherits Page
protected withevents categories As dropdownlist
protected withevents tbl_links As repeater
protected withevents iu_holder as placeholder
Dim CnStr As string = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" & Server.MapPath("db_links.mdb")
Function fix(str)
str = Replace(str,"'","")
fix = str
End Function
Sub Page_Load()
If Not Session("axxezz")=True Then Response.Redirect("login.aspx")
'visa kategorier
show_categories()
Dim Action As string
Action = fix(Request.Querystring("action"))
Select Case Action
Case ""
'visa länkar
Dim SQL As string
Select Case fix(Request.Querystring("cat"))
Case ""
SQL = "SELECT Id,URL,SiteDescription,dt,Clicks FROM tbl_links WHERE Id IN" & _
" (SELECT TOP 20 SiteId FROM tbl_stats ORDER BY ID DESC) ORDER BY Clicks DESC"
Case Else
SQL = "SELECT Id,URL,SiteDescription,dt,Clicks FROM tbl_links WHERE category = '" & fix(Request.Querystring("cat")) & "' ORDER BY Clicks DESC"
End Select
Dim Cn As new OleDbConnection(CnStr)
Dim Cm As new OleDbCommand(SQL,Cn)
Dim dbRead As OleDbDataReader
Dim Container As object
Cn.Open()
dbRead = Cm.ExecuteReader()
tbl_links.DataSource = dbRead
tbl_links.DataBind()
dbRead.Close()
Cn.Close()
Case "click"
click()
Case "logout"
Session.Contents.Remove("axxezz")
Response.Redirect("login.aspx")
Case "delete"
delete()
Case "edit"
iu_holder.Controls.Add(Page.LoadControl("edit.ascx"))
Case "new"
iu_holder.Controls.Add(Page.LoadControl("insert.ascx"))
Case "cat"
iu_holder.Controls.Add(Page.LoadControl("cat.ascx"))
End Select
End Sub
'kategorier i dropdownlistan
Sub show_categories()
'plocka ut unika kategorier ut länktabellen
Dim SQL As string = "SELECT DISTINCT category FROM tbl_links"
Dim Cn As OleDbConnection = new OleDbConnection(CnStr)
Dim Cm As OleDbCommand = new OleDbCommand(SQL,Cn)
Cn.Open()
'skapa ett OleDbDataReader-objekt
Dim objDataReader As OleDbDataReader = Cm.ExecuteReader()
'skapa en ny samling av listposter (option-val i dropdownlistan i default.aspx)
Dim ddlBox As new ListItemCollection
ddlBox.Add(new ListItem("Välj"))
ddlBox.Add(new ListItem())
while(objDataReader.Read())
'lägg till val i kategoridropdownlistan
ddlBox.Add(new ListItem(objDataReader("category").toString()))
end while
'bind data till dropdownlistan
categories.DataSource = ddlBox
categories.DataBind()
'stäng objekt
objDataReader.Close()
Cn.Close()
End Sub
'klick på länk
Sub Click()
'SQL-satser för uppdatering & ny info som sätts in databasen
Dim uSQL As string = "UPDATE tbl_links SET Clicks = Clicks --1 WHERE Id=" & fix(Request.Querystring("dilenoid"))
Dim iSQL As string = "INSERT INTO tbl_stats(dt,SiteId) VALUES('" & Now.ToString("d") & "'," & fix(Request.Querystring("dilenoid")) & ")"
Dim Cn As new OleDbConnection(CnStr)
Cn.Open()
'uppdatera antal klicks i databasen
Dim uCm As new OleDbCommand(uSQL,Cn)
uCm.ExecuteNonQuery()
'sätt in Id & dagens datum för gällande url i statistiktabellen
Dim iCm As new OleDbCommand(iSQL,Cn)
iCm.ExecuteNonQuery()
Cn.Close()
'skicka vidare till egentliga url:en
Response.Redirect(fix(Request.Querystring("url")))
End Sub
'radera länk
Sub delete()
Dim lSQL As string = "DELETE * FROM tbl_links WHERE Id=" & fix(Request.Querystring("id"))
Dim sSQL As string = "DELETE * FROM tbl_stats WHERE SiteId=" & fix(Request.Querystring("id"))
Dim Cn As New OleDbConnection(CnStr)
Cn.Open()
Dim lCm As New OleDbCommand(lSQL,Cn)
lCm.ExecuteNonQuery()
Dim sCm As New OleDbCommand(sSQL,Cn)
sCm.ExecuteNonQuery()
Cn.Close
Response.Redirect("default.aspx?cat=" & Request.Querystring("cat"))
End Sub
End Class
</code>
Proceduren show_categories i klassen links är mycket snarlik proceduren choose_category i klassen edit_link, men skillnaden är den att här fungerar proceduren för att visa en dropdownlista, medan den inte fungerar i show_categories.
Jag är mycket tacksam för hjälp. :)Sv: System.NullReferenceException och dropdownlist
<code>protected withevents choose_cat as dropdownlist = new dropdownlist</code>
Dock är dropdownlistan tom, fast jag vet att det finns kategorier i databasen.
Se här: http://www.dileno.hel.nu/l3/default.aspx?action=edit&id=2&cat=Sv: System.NullReferenceException och dropdownlist