locked
sql query for null values RRS feed

  • Question

  • gud afternoon all,
    i am working on checkboxes... i have 15 checkboxes... i hav put values for  it... whenever i check the checkbox.. it adds the data into the database...
    but when i check the second checkbox it adds the data to the column but keeping other columns null...
    now i want to display only those columns which are "not null".... as i dont know in backend whic column is kept  null....
     so i want a single query from where i could fetch only those columns which are not null...
    Saturday, August 20, 2011 6:35 AM

Answers

  • If you just want a list of those columns that are not null you can do something like this in ADO.NET (i'm not sure if you're using straight ADO.NET or an ORM or Linq to SQL).  This will load a List collection with the names of the columns that are not null.

     

    Do something like this:

     

     

    using System.Data.SqlClient;
    
    using System.Data.SqlTypes;
    
    
    
    
    
     string sqlSelect = "SELECT value1, value2, value3 FROM MyTable";
    
    
    
     SqlDataAdapter da = new SqlDataAdapter(sqlSelect, sqlConnectString);
    
    
    
     DataTable dt = new DataTable();
    
     da.Fill(dt);
    
     
    
     IList<string> not_null_columns = new List<string>();
    
     
    
     foreach (DataRow row in dt.Rows)
    
     {
    
     foreach (DataColumn col in dt.Columns)
    
     {
    
     if (row[col].ToString() != SqlString.Null)
    
     {
    
     not_null_columns.Add(col.ColumnName);
    
     }
    
     }
    
     }
    
    




     

     

     


    Tom Overton



    • Proposed as answer by edhickey Monday, August 22, 2011 4:50 PM
    • Marked as answer by Andrew_Zhu Monday, August 29, 2011 6:28 AM
    Sunday, August 21, 2011 11:57 PM