views:

114

answers:

3

hi ,

i am using ADO.NET Entity framework for connect db and get data.I wanna do if there is no data in object it will write "EMPTY FIELD" if its full than it will write to listview coloumn data from the db.I am getting 'System.NullReferenceException' ERROr. when there is a null object in objectcontext.Dont return a" EMPTY ROW" string. here is my codes ;

   using (ITSEntities arama = new ITSEntities())
            {
                var sql = "SELECT VALUE s_tesis FROM ITSEntities.TB_SAGLIK_TESIS AS s_tesis WHERE s_tesis.TESIS_AD like @p1";
                ObjectQuery<TB_SAGLIK_TESIS> sorgu = new ObjectQuery<TB_SAGLIK_TESIS>(sql, arama).Include("TB_IL").Include("TB_TESIS_TIPI").Include("TB_TESIS_TURU");
                sorgu.Parameters.Add(new ObjectParameter("p1", String.Format("{0}%", btnAra.Text)));

                                   listTesis.Items.Clear();

                foreach (var item in sorgu)
                {

                    ListViewItem listitem = new ListViewItem { Text = item.KODU.ToString() };
                    listitem.SubItems.Add(item.TESIS_AD);
                    listitem.SubItems.Add(String.IsNullOrEmpty(item.TB_IL.ADI) ? "EMPTY ROW" : item.TB_IL.ADI);
                    listitem.SubItems.Add(String.IsNullOrEmpty(item.TB_TESIS_TIPI.TIP_AD) ? "EMPTY ROW" : item.TB_TESIS_TIPI.TIP_AD);
                    listitem.SubItems.Add(String.IsNullOrEmpty(item.TB_TESIS_TURU.TESIS_TURU) ? "EMPTY ROW" :item.TB_TESIS_TURU.TESIS_TURU);
                    listTesis.Items.Add(listitem);
                }
            }
        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.InnerException.ToString());
        }
A: 

You may want to try item.attr.IsNull()

-I believe that the cast to string is causing it to flag the item as null before the IsNullOrEmpty Test.

apocalypse9
A: 

I haven't used entity-framework. But, can you enumerate over "sorgu" when the SQL query returns no rows?

to test that out, you could wrap the foreach in a try/catch and catch the NullReferenceException. Some people would use this NullReferenceException catch to setup what you're doing, but I wouldn't recommend it.

Jim Schubert
+1  A: 

I don't know EF, but you're dereferencing 2 objects in the following lines:

ListViewItem listitem = new ListViewItem { Text = item.KODU.ToString() };
listitem.SubItems.Add(String.IsNullOrEmpty(item.TB_IL.ADI) ? "EMPTY ROW" : item.TB_IL.ADI);
listitem.SubItems.Add(String.IsNullOrEmpty(item.TB_TESIS_TIPI.TIP_AD) ? "EMPTY ROW" : item.TB_TESIS_TIPI.TIP_AD);
listitem.SubItems.Add(String.IsNullOrEmpty(item.TB_TESIS_TURU.TESIS_TURU

If the container object (KODU, TB_IL, TB_TESIS_TIPI. or TB_TESIS_TURU) is ever null, then you'd get a NullReferenceException.

My guess is that these are table names, and that some rows don't have a corresponding JOIN to those tables. In any case, you'd probably need to rewrite those as:

ListViewItem listitem = new ListViewItem { Text = (item.KODU ?? "").ToString() };
listitem.SubItems.Add(
    (item.TB_TL == null || String.IsNullOrEmpty(item.TB_IL.ADI)) 
    ? "EMPTY ROW" : item.TB_IL.ADI
);

To make it a bit cleaner, a method:

string EmptyRowIfNull<T>(T o, Func<T, string> p) {
   string s;
   if (o != null) {
       s = p(o);
   }
   return string.IsNullOrEmpty(s) ? "EMPTY ROW" : s;
}

listitem.SubItems.Add(EmptyRowIfNull(item.TB_IL, t => t.ADI));
listitem.SubItems.Add(EmptyRowIfNull(item.TB_TESIS_TIPI, t => t.TIP_AD));
listitem.SubItems.Add(EmptyRowIfNull(item.TB_TESIS_TURU, t => t.TESIS_TURU));
Mark Brackett
thanx y r my savior :)
Ibrahim AKGUN