Tuesday, 9 July 2013

CRUD (Create, Read, Update, Delete) operations in Entity Framework with ListBox,ChechBox List Control....



 Read, Update, Delete) operations in Entity Framework with ListBox,ChechBox List Control.... 


//code  for  retrive  data  from database  with SrNo..................................
protected void btnsearch_Click(object sender, EventArgs e)
{
try
{
int i = 0;
EmptyControl();
int x = (int.Parse(txtsrno.Text));
var res = (from obj in dbentity.UserRegistrations where obj.SrNo == x select obj).FirstOrDefault();
if (res != null)
{
txtname.Text = res.Name;
rbgender.SelectedValue = res.Gender;
txtmobileno.Text = res.MobileNo;
txtaddress.Text = res.Address;
txtemailid.Text = res.EmailId;
txtpassword.Text = res.Password;
txtcpassword.Text = res.Password;
// How  to retrive data from data base in CheckBox List with using split function
string[] qul = res.Qulification.Split(',');

foreach (ListItem li in cblQulification.Items)
{
if (qul[i] == li.Text)
{
li.Selected = true;
i++;
if (i == qul.Length)
break;

}

}

//retrive data from data base in CheckBox List with using split function
string[] city = res.CityName.Split(',');
foreach (var c in city)
{
lbcity.Items.Add(c);
}

uploadPreview.ImageUrl = "~/Image/" + res.UserImage;
}
else
{
EmptyControl();
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Red;
lblmessage.Text = "This SrNo is  not exist in database";
}
}
catch (Exception ex)
{
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Red;
lblmessage.Text = ex.Message;
}
}

//code  for  create function empty/clear all asp control on web application..................................
private void EmptyControl()
{
txtname.Text = "";
txtaddress.Text = String.Empty;
txtmobileno.Text = "";
txtemailid.Text = ""; ;
txtpassword.Text = "";
txtcpassword.Text = "";
cblQulification.SelectedValue = "";
lbcity.Items.Clear();
uploadPreview.ImageUrl = "";
foreach (ListItem tem in cblQulification.Items)
{

tem.Selected = false;



}
}
//code  for  update  data  in  database  with SrNo..................................
protected void btnUpdate_Click(object sender, EventArgs e)
{
try
{
UserRegistration ur = new UserRegistration();
foreach (ListItem li in cblQulification.Items)
{
if (li.Selected)
{
qul = qul + li.Value.ToString() + ",";
}
}
qul = qul.Remove(qul.Length - 1);

foreach (var kane in lbcity.Items)
{
city += kane + ",";
}
city = city.Remove(city.Length - 1);


string exe = Path.GetExtension(uploadImage.FileName).ToLower();
string filename = uploadImage.FileName;
if (exe == ".png" || exe == ".jpg" || exe == ".jpeg")
{
if (uploadImage.FileBytes.Length <= 10855)
{
//  code for  generate unique  name with extension
filename = Guid.NewGuid().ToString() + Path.GetExtension(filename);
int x = (int.Parse(txtsrno.Text));
var res = (from obj in dbentity.UserRegistrations where obj.SrNo == x select obj).FirstOrDefault();
if (res != null)
{

res.Name = txtname.Text;
res.Gender = rbgender.SelectedValue;
res.MobileNo = txtmobileno.Text;
res.Password = txtpassword.Text;
res.Qulification = qul;
res.Address = txtaddress.Text;
res.EmailId = txtemailid.Text;
res.UserImage = filename;
uploadImage.SaveAs(Server.MapPath("~/Image/") + filename);
res.CityName = city;
dbentity.SaveChanges();
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Green;
lblmessage.Text = "Update Successfully......";

}
else
{
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Red;
lblmessage.Text = "Update Successfully......";

}

}
else
{
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Red;
lblmessage.Text = "Please update image less than 10.6 kb";

}
}
else
{

lblmessage.Visible = true;
lblmessage.ForeColor = Color.Red;
lblmessage.Text = "Please update png,jpg,jpeg file for image upload";

}
}
catch (Exception ex)
{
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Red;
lblmessage.Text = ex.Message;
}

}


//code  for  delete  data  from database  with SrNo..................................
protected void btndelete_Click(object sender, EventArgs e)
{

try
{
// ConnectivitywithEntityFramework.Data.UserRegistration ur = new Data.UserRegistration();
DirectoryInfo dd = new DirectoryInfo(Server.MapPath("~/Image"));
foreach (FileInfo ff in dd.GetFiles())
{
if (ff.Name == Path.GetFileName(uploadPreview.ImageUrl))
ff.Delete();
}

int x = (int.Parse(txtsrno.Text));
var res = (from obj in dbentity.UserRegistrations where obj.SrNo == x select obj).FirstOrDefault();
if (res != null)
{
dbentity.UserRegistrations.DeleteObject(res);
dbentity.SaveChanges();
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Green;
lblmessage.Text = "Delete Successfully ";

}
else
{
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Red;
lblmessage.Text = "There  is  no  any  data ";

}
}
catch (Exception ex)
{
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Red;
lblmessage.Text = ex.Message;
}

}




}
}

0 comments:

Post a Comment