Click here to Skip to main content
15,901,284 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am having a problem i.e.,
I have a subject column in database...now i can add subject to the gridview using entity framework
but unable to check whether the subjects are already present in my database...please can anyone help me by suggesting the code.

C#
onlinequizEntities ob = new onlinequizEntities();
protected void Page_Load(object sender, EventArgs e)
{
   show();
}

protected void btn_subjsub_Click(object sender, EventArgs e)
{
   //having problem here...i want to check here whether subjects r already present in the
   //gridview, if present it will stop adding
   Sub ob1 = new Sub();
   ob1.Subject = txt_adminsub.Text;
   ob.Sub.AddObject(ob1);
   ob.SaveChanges();
   show();
}

protected void btn_subres_Click(object sender, EventArgs e)
{
   Response.Redirect("Subject.aspx");
}

public void show()
{
   var res = from abc in ob.Sub select abc;
   gv.DataSource = res;
   gv.DataBind();
}

public void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
   if (e.CommandName == "Delete")
   {
      int id1 = Convert.ToInt32(e.CommandArgument);
      var res = (from test1 in ob.Sub where test1.Sub_id==id1 select test1).ToList();
      foreach (var row in res)
      {
         ob.Sub.DeleteObject(row);
      }
      ob.SaveChanges();
      Response.Redirect("");
   }
   else if (e.CommandName == "Edit")
   {
      int id1 = Convert.ToInt32(e.CommandArgument);
      var sub = (from test in ob.Sub where test.Sub_id==id1 select test).ToList();
      foreach(var r in sub)
      {
         if (r.Subject == txt_adminsub.Text)
         {
            Response.Redirect("");
         }
         else
         {
            r.Subject = txt_adminsub.Text;
         }
      }
      ob.SaveChanges();
      Response.Redirect("");
   }
}
Posted
Updated 26-Dec-13 6:04am
v3
Comments
Sampath Lokuge 26-Dec-13 9:38am    
Can you put the code snippet which you have written so far for the EF Query?
Member 10488678 26-Dec-13 10:34am    
onlinequizEntities ob = new onlinequizEntities();
protected void Page_Load(object sender, EventArgs e)
{

show();

}
protected void btn_subjsub_Click(object sender, EventArgs e)
{
//having problem here...i want to check here whether subjects r already present in the //gridview, if present it will stop adding
Sub ob1 = new Sub();
ob1.Subject = txt_adminsub.Text;
ob.Sub.AddObject(ob1);
ob.SaveChanges();
show();


}

protected void btn_subres_Click(object sender, EventArgs e)
{
Response.Redirect("Subject.aspx");
}
public void show()
{
var res = from abc in ob.Sub select abc;
gv.DataSource = res;
gv.DataBind();
}


public void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")

{
int id1 = Convert.ToInt32(e.CommandArgument);
var res = (from test1 in ob.Sub where test1.Sub_id==id1 select test1).ToList();
foreach (var row in res)
{
ob.Sub.DeleteObject(row);
}
ob.SaveChanges();
Response.Redirect("");


}


else if (e.CommandName == "Edit")

{
int id1 = Convert.ToInt32(e.CommandArgument);
var sub = (from test in ob.Sub where test.Sub_id==id1 select test).ToList();
foreach(var r in sub)
{
if (r.Subject == txt_adminsub.Text)
{
Response.Redirect("");
}
else
{
r.Subject = txt_adminsub.Text;
}

}
ob.SaveChanges();
Response.Redirect("");
}
ZurdoDev 26-Dec-13 9:50am    
Where are you stuck?
Member 10488678 26-Dec-13 10:37am    
i have uploaded the code in above comment..please go through
ZurdoDev 26-Dec-13 10:44am    
Can you specify where you are stuck instead of just showing a bunch of code?

1 solution

Try something like

C#
using (onlinequizEntities  ob= new onlinequizEntities ())
        {
Sub ob1 = new Sub();
ob1.Subject = txt_adminsub.Text;
 if(!ob.Sub.Any(subject => subject.Subject == ob1.Subject))
        {
                ob.Sub.AddObject(ob1);
                ob.SaveChanges();
        }
}


Try and revert
PS : The best way is to set which ever Unique column as Unique Constraint in Table since the above
cannot ensure uniqueness in case of concurrent insertion or updations from different users.
Hope this helps...
 
Share this answer
 
v2
Comments
Member 10488678 26-Dec-13 11:25am    
thanx...it works
JoCodes 26-Dec-13 11:28am    
Thanks and Welcome .I have just modified to make it more clean . Also , added a PS . Please note.

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