WCF/WF - how to check data is exist in the table or not in the linq to sql

Asked By skumari woonna on 24-May-10 09:54 AM

I have 1 table.In this table i have data.the table name is apmod.

mid mname       aid
1   user        1
2   register    1
3   employee    1

based on the above data i am finding mid.i wrote the following code.
 public int GetMID(string Mname)
        {
            Appmetric db = new Appmetric();
            try
            {
                int mid = (from p in db.ApMod
                                where p.MName == Mname
                                select p.MID).FirstOrDefault();


                return mid; //this returns mod id
            }
            catch (Exception e)
            {
                throw new Exception("MId is not available", e);
            }

        }

the above code is working iam getting mid.but i want the following code

1)suppose mid is not existed i want to create mid in the table.
1)suppose mid is exists i want to return value.

plz help me its very urgent

haritian pinio replied to skumari woonna on 24-May-10 11:00 AM

maybe this should work:

 public int GetMID(string Mname)
        {
            Appmetric db = new Appmetric();
            try
            {
                int mid = (from p in db.ApMod
                                where p.MName == Mname
                                select p.MID).FirstOrDefault();

  if(mid == 0) // 0 is default for int, returned when mid is not found

{

mid = (from p in db.ApMod

 select p.MID).Max() + 1;

                return mid; //this returns mod id
            }
            catch (Exception e)
            {
                throw new Exception("MId is not available", e);
            }

        }

Mash B replied to skumari woonna on 24-May-10 02:34 PM
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);
            }
 
        }
skumari woonna replied to Mash B on 25-May-10 01:41 AM
Hi mash,
thanks for reply.I used code.but at time of using apmod mod = new apmod();      in this iam getting error.replace of apmod i gave table name.iam getting the following error

 The type or namespace name 'AppModules' could not be found .how to solve the problem
Mash B replied to skumari woonna on 25-May-10 02:38 AM
ok comment tht statement and initialize the properties using

db.ApMod
.mname = "name";
skumari woonna replied to Mash B on 25-May-10 02:45 AM
I used above code.in the first line  public int GetMID(string Mname)
iam getting the following error:-

not all code paths return a value
how to silve the problem
Mash B replied to skumari woonna on 25-May-10 03:04 AM
comment this code line

apmod mod = new apmod();

and write

 if(mid > 0)
{

}
else
{
    d.Apmod.mname = "name";
 
}