Here is the code
public int GetMID(string Mname)
{
Appmetric db = new Appmetric();
// The database table object inside LINQ, instantiate it
apmod mod = new apmod();
try
{
int mid = (from p in db.ApMod
where p.MName == Mname
select p.MID).FirstOrDefault();
if(mid >0 ) // just verify wht nid returns if not found and change the condition accordingly
{
return mid; //this returns mod id
}
else
{
// set all properties, these are nothing but fields your database tables
mod.mname = "name";
mod.aid = "1";
Appmetric.ApMod.InsertOnSubmit(mod);// still not inserted into database, just added into the LINQ Database object
Appmetric.SubmitChanges(); //make change to the database now
}
}
catch (Exception e)
{
throw new Exception("MId is not available", e);
}
}