server error object reference not set
Hej Jeg får denne fejl og kan ikke forstå hvorfor jeg heller ikke får nogle data ned i min dropdownlist inden jeg prøver en insertServer Error in '/' Application.
--------------------------------------------------------------------------------
Object reference not set to an instance of an object.
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 reference not set to an instance of an object.
Source Error:
Line 75: strBillede = filename.ToString();
Line 76: strvPris = Convert.ToSingle(tbPris.Text);
Line 77: strvKatNavn = myDrpBox.SelectedItem.Value.ToString();
Line 78: strSQL = "INSERT INTO varelist (VareNr, Beskrivelse, KortBeskrivelse, vVareNavn, Beskriv, Billed, Pris, KatNavn) Values ('";
Line 79: strSQL += strvVareNr + "', '";
Source File: D:\Users\Home\LocalUser\xxxxxx\wwwroot\SweetSpice\admin\varer_insert.aspx Line: 77
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
ASP.varer_insert_aspx.Insert(Object Sender, EventArgs E) in D:\Users\Home\LocalUser\xxxxxxxx\wwwroot\SweetSpice\admin\varer_insert.aspx:77
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page
Koden ser således ud:
<%@ Page Language="c#" ContentType="text/html" ResponseEncoding="iso-8859-1" debug="true" CodeBehind="varer_insert.aspx.cs" AutoEventWireup="false" Inherits="xxxxxxxx.DHL.administration.varer_insert" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.IO" %>
<link href="../sweetspice.css" rel="stylesheet" type="text/css">
<link href="../scrollbar.css" rel="stylesheet" type="text/css">
<div class="mainFont">
<script runat="server">
void Page_Load()
{
if(!IsPostBack)
{
LoadData();
}
}
void LoadData()
{
SqlConnection objConnection;
SqlCommand objCommand;
SqlDataAdapter OurDataAdapter;
objConnection = new SqlConnection("Server=localhost;User Id=xxxxxxx;Password=xxxx;Database=fgggfd;");
objConnection.Open();
objCommand = new SqlCommand ("SELECT * FROM ssKategori ORDER BY KatNavn DESC" ,objConnection);
drpData.DataTextField = "KatNavn";
drpData.DataSource = objCommand.ExecuteReader(CommandBehavior.CloseConnection);
drpData.DataBind();
drpData.Items.Insert(0, "-- Vælg kategori --");
objConnection.Close();
}
void Insert(object Sender, EventArgs E)
{
string TheFile = tbvBillede.PostedFile.FileName;
string filepath = Server.MapPath("") + "\\uploadedfiles\\";
string filename = TheFile.Substring(TheFile.LastIndexOf("\\")+1);
string fullpath = filepath + filename;
if (TheFile.Length > 0) {
string FileType = tbvBillede.PostedFile.ContentType;
int FileSize = tbvBillede.PostedFile.ContentLength;
if (FileType != "image/jpeg" & FileType != "image/pjpeg") {
OurSpan.InnerHtml = "Du kan <b>kun Only Uploade Jpegs.</b> Ikke?";
}
else if (FileSize > 12000) {
OurSpan.InnerHtml = "Du kan ikke uploade en fil større end 11k. Din er størrer.";
}
else {
try {
tbvBillede.PostedFile.SaveAs(fullpath);
OurSpan.InnerHtml = "En fil med navnet <b>" + filename + "</b> blev uploadet succesfuldt<br>";
OurSpan.InnerHtml += "Det blev uploadet til <b>" + filepath + "</b> directory";
}
catch (Exception Exc) {
OurSpan.InnerHtml = "Fejl ved upload af <b>" + filename + "</b><br>" + Exc.ToString();
}
}
}
else {
OurSpan.InnerText = "Venligst vælg en fil til upload";
}
string strvVareNr, strvBeskrivelse, strvKortBeskrivelse, strvVareNavn, strvBeskriv, strColliStr, strBillede, strvKatNavn;
float strvPris;
string strSQL;
DropDownList myDrpBox;
myDrpBox = (DropDownList) tbKatNavn.FindControl("drpData");
strvVareNr = tbVareNr.Text;
strvBeskrivelse = tbBeskrivelse.Text;
strvKortBeskrivelse = tbKortBeskrivelse.Text;
strColliStr = tbColliStr.Text;
strvVareNavn = tbNavn.Text;
strvBeskriv = tbKortBeskrivelse.Text;
strBillede = filename.ToString();
strvPris = Convert.ToSingle(tbPris.Text);
strvKatNavn = myDrpBox.SelectedItem.Value.ToString();
strSQL = "INSERT INTO varelist (VareNr, Beskrivelse, KortBeskrivelse, vVareNavn, Beskriv, Billed, Pris, KatNavn) Values ('";
strSQL += strvVareNr + "', '";
strSQL += strvBeskrivelse + "', ";
strSQL += strvKortBeskrivelse + ", '";
strSQL += strvVareNavn + "', '";
strSQL += strvBeskriv + "', '";
strSQL += strBillede + "', ";
strSQL += strvPris.ToString().Replace("," , ".") + ", ";
strSQL += strvKatNavn + ")";
//Response.Write(strSQL);
SqlConnection myConnection = new SqlConnection("Server=localhost;User Id=xxxxxxx;Password=xxxx;Database=fgggfd;");
myConnection.Open();
SqlCommand myCommand = new SqlCommand(strSQL, myConnection );
myCommand.ExecuteNonQuery();
myConnection.Close();
lblBesked.Text ="Du har oprettet en Varer som er " + tbNavn.Text;
btInsert.Enabled=false;
}
void Blank(Object Sender, EventArgs e)
{
tbVareNr.Text="";
tbNavn.Text="";
tbKortBeskrivelse.Text="";
tbPris.Text="";
btInsert.Enabled=true;
}
void Udskriv(Object Sender, EventArgs e)
{
Response.Redirect("varer_admin.aspx");
}
</script>
<form runat="server">
<h3>Indtast en ny Varer.</h3>
<table width="100%">
<tr>
<td width="140">VareNr</td>
<td><asp:textbox id="tbVareNr" runat="server" Width="250px"/></td>
</tr>
<tr>
<td valign="top">Varer Navn:</td>
<td valign="top"><asp:textbox ID="tbNavn" Width="250px" runat="server"/></td>
</tr>
<tr>
<td width="140">Varebeskrivelse:</td>
<td valign="top"><asp:textbox id="tbBeskrivelse" runat="server" Width="250px" TextMode="MultiLine" Rows="5"/></td>
</tr>
<tr>
<td valign="top">Varer Beskrivelse:</td>
<td valign="top"><asp:textbox ID="tbKortBeskrivelse" Width="250px" TextMode="MultiLine" Rows="2" runat="server"/></td>
</tr>
<tr>
<td valign="top">Varer Pris:</td>
<td valign="top"><asp:textbox ID="tbPris" Width="200px" runat="server"/></td>
</tr>
<tr>
<td valign="top">Varer Pris 1:</td>
<td valign="top"><asp:textbox ID="tbPris1" Width="200px" runat="server"/></td>
</tr>
<tr>
<td valign="top">Varer Pris 10:</td>
<td valign="top"><asp:textbox ID="tbPris10" Width="200px" runat="server"/></td>
</tr>
<tr>
<td valign="top">Varer Pris 50:</td>
<td valign="top"><asp:textbox ID="tbPris50" Width="200px" runat="server"/></td>
</tr>
<tr>
<td valign="top">Varer Pris 100:</td>
<td valign="top"><asp:textbox ID="tbPris100" Width="200px" runat="server"/></td>
</tr>
<tr>
<td valign="top">Varer Pris 200:</td>
<td valign="top"><asp:textbox ID="tbPris200" Width="200px" runat="server"/></td>
</tr>
<tr>
<td width="140">Collistr:</td>
<td><asp:textbox id="tbColliStr" runat="server" Width="250px"/></td>
</tr>
<tr>
<td width="140">Lager Antal:</td>
<td><asp:textbox id="tbLagerAntal" runat="server" Width="250px"/></td>
</tr>
<tr>
<td valign="top">Varer Billede:</td>
<td valign="top"><input ID="tbvBillede" Width="250" type="file" runat="server"/><br />Upload billed max 10KB</td>
</tr>
<tr>
<td valign="top">Kort Billede:</td>
<td valign="top"><input ID="tbvKort" Width="250" type="file" runat="server"/><br />Upload billed max 10KB</td>
</tr>
<tr>
<td valign="top">Varer Kategori:</td>
<td valign="top"><asp:panel ID="tbKatNavn" runat="server"><asp:dropdownlist ID="drpData" DataTextField="KatNavn" DataValueField="KatNr" Width="250px" runat="server"/></asp:panel></td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btInsert" runat="server" Text="Indsæt varer" OnClick="Insert"/>
<asp:button ID="btBlank" OnClick="Blank" runat="server" Text="Slet felter"/>
<asp:button ID="btUdskriv" OnClick="Udskriv" runat="server" Text="Gå til Varer administration"/>
<br />
<br />
<br />
<asp:label ID="lblBesked" runat="server" Font-Bold="true"/>
<br><span id="OurSpan" runat="server" />
</td>
</tr>
</table>
</form>
</div>