Dear All,
I am new to Programming,
I Am doing the 3 tier Architecture Example.
Here I am Getting The Following Error..
Object reference not set to an instance of an object.
The Code is As following
Default Aspx.cs:
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void BtnAdd_Click(object sender, EventArgs e)
{
CustomerBAL bal = new CustomerBAL();
try
{
if (bal.Insert(TxtCustomerID.Text,TxtFirstName.Text,TxtLastName.Text) > 0)
{
LblResult.Text = "Record Inserted Sucessfully";
}
else
{
LblResult.Text = "Records Not Inserted Sucessfully";
}
}
catch (Exception ex)
{
LblResult.Text = ex.Message;
}
finally
{
bal = null;
}
}
}
CustomerBAL.aspx.cs
public class CustomerBAL
{
public CustomerBAL()
{
}
public int Insert(string CustomerID,String FirstName, string LastName)
{
CustomerDAL Dal= new CustomerDAL();
try
{
return Dal.Insert(CustomerID,FirstName,LastName);
}
catch
{
throw;
}
finally
{
Dal= null;
}
}
}
CustomerDAL.aspx.cs
public class CustomerDAL
{
public CustomerDAL()
{
}
public int Insert(String CustomerID, String FirstName, String LastName)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Stcon"].ConnectionString);
string str1 = ("Insert Into Architechture_tier(CustomerID,FirstName,LastName) values (@CustomerID,@FirstName,@LastName)");
SqlCommand cmd = new SqlCommand(str1, con);
cmd.CommandType = CommandType.Text;
try
{
cmd.Parameters.Add(new SqlParameter("@CustomerID", SqlDbType.VarChar));
cmd.Parameters["@CustomerID"].Direction = ParameterDirection.Input;
cmd.Parameters["@CustomerID"].Size = 20;
cmd.Parameters["@CustomerID"].Value = CustomerID;
cmd.Parameters.Add(new SqlParameter("@FirstName", SqlDbType.VarChar));
cmd.Parameters["@FirstName"].Direction = ParameterDirection.Input;
cmd.Parameters["@FirstName"].Size = 25;
cmd.Parameters["@FirstName"].Value = FirstName;
cmd.Parameters.Add(new SqlParameter("@LastName", SqlDbType.VarChar));
cmd.Parameters["@LastName"].Direction = ParameterDirection.Input;
cmd.Parameters["@LastName"].Size = 25;
cmd.Parameters["@LastName"].Value = LastName;
con.Open();
return cmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
con.Close();
}
}
}
please Help me with The same...