Click here to Skip to main content
15,908,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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
C#
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
C#
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)");
        //str1= str1+ "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...
Posted
Updated 5-Nov-12 6:28am
v4
Comments
MT_ 5-Nov-12 1:35am    
Zaid, Why don't you simply try to debug and tell which line is throwing issue ?
zaid Qureshi 5-Nov-12 1:39am    
the issue is coming in the try...catch...Finally block...
sarathtamil 5-Nov-12 1:45am    
which value is assigned in bal?
manognya kota 5-Nov-12 1:44am    
At what object are you getting the exception while debugging?Are all the object instance present?
zaid Qureshi 5-Nov-12 1:50am    
from the If Statement in the try Block its directly going to the Catch Block..and creating the Error..


There was an error in the Connection String...

Thanks Everyone..For Your Valuable Efforts and time...
 
Share this answer
 
Comments
Sarrrva 5-Nov-12 3:53am    
well Good Work
Hi,

Almost every day, beginning C# programmers ask:
Why do I get the error "Object reference not set to an instance of an object"?
The reason is that the program is trying to access a member of a reference type variable which is set to null.


For more Info:


http://en.csharp-online.net/CSharp_FAQ:_What_does_Object_reference_not_set_to_an_instance_of_an_object_mean[^]

regards and thanks
sarva
 
Share this answer
 
Comments
Sarrrva 5-Nov-12 2:29am    
Funny down voters
try this it will help u
cmd.connection=con;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900