Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have 3 column in my table ID,Status,feedback.Id is identity,status value bydefault is false and I have to insert value of feedback using entity framework in n-tier arch.

I made 3 classes as

BO
C#
namespace problemvisa
{
    public class Bo
    {
        private int id;
 
        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private string status;
 
        public string Status
        {
            get { return status; }
            set { status = value; }
        }
        private string feedback;
 
        public string Feedback
        {
            get { return feedback; }
            set { feedback = value; }
        }
    }
}

2-DAL
C#
public class DAL
{
    public bool insert(Bo aa)
    {
        if (aa.Equals(null))
        {
            return false;
        }
        else
        {
            using (problemEntities1 context = new problemEntities1())
            {
                Table1 tt = new Table1();
                 context.AddToTable1(tt);
                 context.SaveChanges();
            }
            return true;
        }
    }
}

3.BAL
C#
public class BAL
{
    public bool insert(Bo ss)
    {
        DAL dd = new DAL();
        if (dd.insert(ss))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

IN aspx form
C#
protected void Button1_Click(object sender, EventArgs e)
{    
    Bo aa = new Bo();
    aa.Status = "false";
    aa.Feedback = TextBox1.Text;
    BAL ss = new BAL();
    ss.insert(aa);
}



One error is coming i.e.Cannot insert the value NULL into column 'feedback', table 'problem.dbo.Table1'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Posted
Updated 3-Mar-13 21:36pm
v2
Comments
muruga.n 4-Mar-13 3:37am    
Check with postback method, because it makes textbox value to be null.
1989priya 4-Mar-13 3:39am    
How to use postback method here ?

Ok First check your database that the ID field is Primary Key or not , if you have not set primary key then set it and check.
 
Share this answer
 
Comments
1989priya 4-Mar-13 3:57am    
Yaa,my id is primary key and I used identity on id but identity is not working
Because, your database table doesn't accepts null value for feedback column. Check this code very carefully:
C#
public class DAL
{
    public bool insert(Bo aa)
    {
        if (aa.Equals(null))
        {
            return false;
        }
        else
        {
            using (problemEntities1 context = new problemEntities1())
            {
                 Table1 tt = new Table1();
                 context.AddToTable1(tt);
                 context.SaveChanges();
            }
            return true;
        }
    }
}

If object aa is not null then the debugger will move to else block. You are not inserting the value of aa in your table. You are just creating the object of your Table1(it'll have all the column values as null) and directly instering that onject to the context. Try this:
C#
Table1 tt = new Table1();
tt.Status = aa.Status;
tt.Feedback = aa.Feedback;//status and feedback is your database columns.
context.AddToTable1(tt);
context.SaveChanges();



--Amit
 
Share this answer
 
Comments
1989priya 4-Mar-13 3:53am    
Thankx amit.......
It work
_Amy 4-Mar-13 3:54am    
Welcome. :)
You should accept the answer if it is working for you.
in the place of TextBox1.Text put one value and check whether it is working or not.
 
Share this answer
 
Comments
1989priya 4-Mar-13 3:45am    
no,its not working

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