I'm a newbie...and stuck on System.NullReferenceException error - please help!

G

Guest

OK - I have a very simple 6 field form and I'm getting this error upon
submission:

System.NullReferenceException: Object reference not set to an instance
of an object. at Intellect.Website.RequestInfoPage.cmdPost_Click(Object
sender, EventArgs e)

My Validation controls work great but as soon as I submit the form I
get the error msg stated above. I'm very new to C# so it's probably
something stupid I'm not seeing cuz you don't know what you don't know!

On top of getting the error stated above, my 2 fields from the form
collection are not coming through in the test email. What am I doing
wrong here?

Thanks in advance as any help would be greatly appreciated!



Here is my aspx code which is pretty straightforward:

---------------------------------START-------------------------------------------------------------
<form runat="server" method="post">
<TABLE class="contentTable" cellSpacing="0" cellPadding="0"
border="0">
<TR>
<TD>

<TABLE border="0">
<tr>
<td colspan="2">
<asp:Label id="Message" runat="server" />
</td>
</tr>
<TR>
<TD>
<asp:Label id="Label1" runat="server">First
Name:</asp:Label></TD>
<TD>
<asp:TextBox id="txtFirstName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="rfvFirstName" runat="server"
ControlToValidate="txtFirstName" ErrorMessage="First name is
required."></asp:RequiredFieldValidator></TD>
</TR>
<TR>
<TD>
<asp:Label id="Label2" runat="server">Last
Name:</asp:Label></TD>
<TD>
<asp:TextBox id="txtLastName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="rfvLastName" runat="server"
ControlToValidate="txtLastName" ErrorMessage="Last name is
required."></asp:RequiredFieldValidator></TD>
</TR>
<TR>
<TD>
<asp:Label id="Label3"
runat="server">Company:</asp:Label></TD>
<TD>
<asp:TextBox id="txtCompany" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="rfvCompany" runat="server"
ControlToValidate="txtCompany" ErrorMessage="Company name is
required."></asp:RequiredFieldValidator></TD>
</TR>
<TR>
<TD>
<asp:Label id="Label4" runat="server">Phone:</asp:Label></TD>
<TD>
<asp:TextBox id="txtPhone" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="rfvPhone" runat="server"
ControlToValidate="txtPhone" ErrorMessage="Phone number is
required."></asp:RequiredFieldValidator></TD>
</TR>
<TR>
<TD>
<asp:Label id="Label5" runat="server">Email:</asp:Label></TD>
<TD>
<asp:TextBox id="txtEmail" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="rfvEmail" runat="server"
ControlToValidate="txtEmail" ErrorMessage="Email address is
required."></asp:RequiredFieldValidator></TD>
</TR>
<TR>
<TD>
<asp:Label id="Label6" runat="server">Notes:</asp:Label></TD>
<TD>
<asp:TextBox id="txtNotes" runat="server"></asp:TextBox></TD>
</TR>
<TR>
<TD></TD>
<TD>
<asp:LinkButton id="cmdPost" onclick="cmdPost_Click"
runat="server" Text="Submit"></asp:LinkButton></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
</form>
------------------------------------------END-----------------------------------------------




And then here is my code behind file code:

------------------------------------START--------------------------------------


public class RequestInfoPage : BasePage
{

public System.Web.UI.WebControls.Label Label1;
public System.Web.UI.WebControls.TextBox txtFirstName;
public System.Web.UI.WebControls.RequiredFieldValidator rfvFirstName;
public System.Web.UI.WebControls.Label Label2;
public System.Web.UI.WebControls.TextBox txtLastName;
public System.Web.UI.WebControls.RequiredFieldValidator rfvLastName;
public System.Web.UI.WebControls.Label Label3;
public System.Web.UI.WebControls.TextBox txtCompany;
public System.Web.UI.WebControls.RequiredFieldValidator rfvCompany;
public System.Web.UI.WebControls.Label Label4;
public System.Web.UI.WebControls.TextBox txtPhone;
public System.Web.UI.WebControls.RequiredFieldValidator rfvPhone;
public System.Web.UI.WebControls.Label Label5;
public System.Web.UI.WebControls.TextBox txtEmail;
public System.Web.UI.WebControls.RequiredFieldValidator rfvEmail;
public System.Web.UI.WebControls.Label Label6;
public System.Web.UI.WebControls.TextBox txtNotes;
public System.Web.UI.WebControls.Button cmdPost;
public System.Web.UI.WebControls.Label Message;
protected MasterPage _contentMaster;

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form
Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{

}
#endregion

public void cmdPost_Click(Object sender, System.EventArgs e)
{
if(Page.IsValid)
{
System.Web.Mail.MailMessage objEmail = new
System.Web.Mail.MailMessage();
objEmail.To = "(e-mail address removed)";
objEmail.From = "(e-mail address removed)";
//objEmail.Cc = "(e-mail address removed)";
objEmail.Subject = "Test Email from Site";
objEmail.Body = Request.Form["txtFirstName"] + ", " +
Request.Form["txtLastName"];
//objEmail.Body = "Let me know if you got this test!";
objEmail.Priority = System.Web.Mail.MailPriority.High;
//SmtpMail.SmtpServer = "localhost";

try
{
System.Web.Mail.SmtpMail.Send(objEmail);
//Response.Flush();
Message.Text = "Message Sent...";

}
catch (Exception exc)
{
Response.Write("Send failure: " + exc.ToString());
}

}

}


protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

Title = "Request Information";
NavMenuControl.ShowLogos(LogoTypes.AllLogos);
NavMenuControl.ShowPracticeMenu(PracticeType.None);
}


}

-------------------------------------------END----------------------------------
 
M

Marc Scheuner

On top of getting the error stated above, my 2 fields from the form
collection are not coming through in the test email. What am I doing
wrong here?

You're accessing them in an old-style ASP fashion - no need for that.

Try this instead:

if(Page.IsValid)
{
MailMessage objEmail = new MailMessage();
objEmail.To = "(e-mail address removed)";
objEmail.From = "(e-mail address removed)";
objEmail.Subject = "Test Email from Site";
objEmail.Body = txtFirstName.Text + ", " + txtLastName.Text;
..... and so on

All your control on an ASP.NET form that have a "runat=server"
attribute will be accessible in your code behind as REAL OBJECT
INSTANCES! :) Use it, make the best of it !

Marc
 
G

Guest

Thanks Marc! So, what do I do syntax-wise to correct the
System.NullReferenceException error? To the seasoned .Net (C#)
developer this is probably a no-brainer, but I'm still a newbie with
C#! :)

-------------------------------------------------------------------------------------------------------
 
C

Chris Dunaway

Thanks Marc! So, what do I do syntax-wise to correct the
System.NullReferenceException error? To the seasoned .Net (C#)
developer this is probably a no-brainer, but I'm still a newbie with

What line of code is specified in the exception message? If Marc is
right, it might be the Request object that is causing your problem. If
you change to the method he recommended, your exception may go away.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top