Click here to Skip to main content
15,914,820 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have created one email sender, in that all email ids come from database listed into checkedlistbox, i have problem that if email id not enter into database then its showing me blank checkbox

for exmaple
1 sample@gamil.com
2
3 mudas3213@gmail.com
4 priyadas256@yahoo.in

i want to hide the blank email id i.e: "2" one i don't want to show in checklistbox
if any possibility to do this.
so how to solve this problem ?
please help me
Posted

There are several ways to do this.

Using SQL
If you are using SQL in your database for example could you not do a
SQL
select * from emails where (not (email is null))


Using Dataset
If you are binding directly to a datatable (from a dataset) set then you could use a dataview or linq expression.

Linq
C#
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

DataTable emails= ds.Tables["Emails"];

var query =
    from email in emails.AsEnumerable()
    where order.Field<string>("EmailAddress") != ""
    select new
    {
        EmailAddress = order.Field<string>("EmailAddress")
    };

chkBoxList.DataSource = query.ToList;
chkBoxList.DataBind();


DataView
C#
DataView emailsView = new DataView(emailsDS.Tables["Emails"],
                                 "EmailAddress <> ''",
                                 "EmailAddress",
                                 DataViewRowState.CurrentRows);
chkBoxList.DataSource = emailsView;
chkBoxList.DataBind();
 
Share this answer
 
v2
Comments
Sandeep Mewara 6-Jun-12 8:06am    
5!
checking null is not a good idea because empty is different than null!
linq:
var a = (from a in ctx.emails Where a.email.Length!=0 select a).ToList();
 
Share this answer
 
This is the code to retrieve data from database into Checkedlistbox

C#
string connString = "Data Source=DURGESH-PC;Initial Catalog=SCF;Persist Security Info=True;User ID=sa;Password=durgesh";
                string sql = "select email from studentdetails where (not (email is null))";
                SqlConnection conn = new SqlConnection(connString);
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string data1 = dt.Rows[i]["email"].ToString();
                    checkedListBox1.Items.Add(data1);
                }
 
Share this answer
 
change the sql select statement

SQL
select email from studentdetails where email !=''
 
Share this answer
 

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