Monday, September 1, 2014

Entity Framework Doesn't add to the Database

I'm using entity framework to register a customer using Windows Form but after calling the add method and calling the savechange method the row doesn't insert and i don't know why here is my code: there is a primary key column named Cid which is identity


it is a register form when ever from the main form the user click on the register button this form loads and he enters his data and press the submit button.but when i click the submit button it doesn't add to database after the call to .SaveChanges() I close my forms and see the data from the server explorer (R-click on the tables and show table data) and it is empty.and even I tried putting a breakpoint in the register form and see if the 'db' has any data which is none


here is my code:



public partial class RegisterForm : Form
{
PachinEntities1 db=new PachinEntities1();
private Customer temp;
public RegisterForm()
{
InitializeComponent();
}

private void button2_Click(object sender, EventArgs e)
{
this.Close();
}

private void RegisterForm_Load(object sender, EventArgs e)
{
temp=new Customer();
DateTime gocal = DateTime.Now;
PersianCalendar pcal=new PersianCalendar();
int year=pcal.GetYear(gocal);
int month = pcal.GetMonth(gocal);
int day = pcal.GetDayOfMonth(gocal);
string pday = string.Format("{0}/{1}/{2}", year, month, day);
temp.Date = pday;
txt_Date.Text = pday;
}

private void button1_Click(object sender, EventArgs e)
{
if (txt_Name.Text.Trim().Length != 0)
{

if (txt_Tell1.Text.Trim().Length != 0)
{
if (txt_Address.Text.Trim().Length != 0)
{
temp.Name = txt_Name.Text;
temp.Tell1 = txt_Tell1.Text;
temp.Tell2 = txt_Tell2.Text;
temp.Address = txt_Address.Text;
db.Customers.Add(temp);
db.SaveChanges();
}
else
{
MessageBox.Show("آدرس را وارد کنید");
}
}
else
{
MessageBox.Show("شماره تلفن را وارد کنید");
}
}
else
{
MessageBox.Show("نام را وارد کنید");
}
}
}


No comments:

Post a Comment