I am beginner in asp.net.
I have an web application solution in asp.net webforms with 3 projects named:
1) TestiModels where are custom classes.
2)TestiServices where are are asmx web services
3)TestiApp where are UI files like default.aspx. inside this project consume webservices
For two days I am getting this error at default.aspx file:
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 70: u.gender = RadioButtonList1.SelectedValue;
Line 71: u.photo_path = FullPath;
Line 72: int retVal = service.InsertUser(u);
Line 73: if (retVal == -1)
Line 74:
Source File: c:\Users\Jon\TestiApp\TestiApp\Default.aspx.cs Line: 72
Models are located in a separate project named TestiModels
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace TestiModels
{
public class User
{
private int _userid;
private string _username;
private string _password;
private string _firstname;
private string _lastname;
private string _email;
private DateTime _created_date;
private DateTime _last_login_date;
private string _gender;
private string _photo_path;
// get set
public int userId
{
get;
set;
}
public string username
{
get;
set;
}
public string password
{
get;
set;
}
public string firstname
{
get;
set;
}
public string lastname
{
get;
set;
}
public DateTime created_date
{
get;
set;
}
public string email
{
get;
set;
}
public DateTime last_login_date
{
get;
set;
}
public string gender
{
get;
set;
}
public string photo_path
{
get;
set;
}
}
}
ASMX WEB SERVICES are located in a separate project named TestiServices
[WebMethod]
public Int32 InsertUser(User u)
{
int userId = 0;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
try
{
SqlCommand cmd = new SqlCommand("Register");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Username", u.username);
cmd.Parameters.AddWithValue("@Password", u.password);
cmd.Parameters.AddWithValue("@Email", u.email);
cmd.Parameters.AddWithValue("@Firstname", u.firstname);
cmd.Parameters.AddWithValue("@Lastname", u.lastname);
cmd.Parameters.AddWithValue("@Gender", u.gender);
cmd.Parameters.AddWithValue("@PhotoPath", u.photo_path);
cmd.Connection = con;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
userId = Convert.ToInt32(cmd.ExecuteScalar());
switch (userId)
{
case -1:
return -1;
case -2:
return -2;
default:
return 0;
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (con.State != ConnectionState.Closed)
{
con.Close();
}
}
}
Default.aspx.cs file inside TestiApp project
protected void RegisterUser(object sender, EventArgs e)
{
string FullPath = "";
TestiServices.App_Code.WebService1 service = new TestiServices.App_Code.WebService1();
TestiModels.User u = new TestiModels.User();
u.username = txtUsername.Text.Trim();
u.password = txtPassword.Text.Trim();
u.email = txtEmail.Text.Trim();
u.firstname = txtF.Text.Trim();
u.lastname = txtL.Text.Trim();
u.gender = RadioButtonList1.SelectedValue;
u.photo_path = FullPath;
int retVal = service.InsertUser(u);
if (retVal == -1)
Page.ClientScript.RegisterStartupScript(this.GetType(), "myScript", "UAlert();", true);
else if (retVal == -2)
Page.ClientScript.RegisterStartupScript(this.GetType(), "myScript", "EAlert();", true);
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "myScript", "SAlert();", true);
}
}
I hope in your help guys!