Solved

Check Box List ASP.NET SQL

Posted on 2014-10-09
3
200 Views
Last Modified: 2014-10-14
I am working on a program that has a checkboxlist control.

What I am building is a type of messaging program.

The controlbox contains names from a db that also has emails attached to that name.

I want it so if the user selects multiple items that it pulls the emails from the checkbox list

I was thinking in may need to be something like foreach (listitems li in checkboxlist.items)
select from table.

I am not quite sure how to do this.

It has to construct an email and add all selected recipients to the email. Not a new email each time.
0
Comment
Question by:desiredforsome
  • 2
3 Comments
 
LVL 8

Expert Comment

by:johny_bravo1
ID: 40371148
Bind Email as ValueField of CheckboxList
  string items = string.Empty;
    foreach (ListItem i in chkUsers.Items)
    {
        if (i.Selected == true)
        {
            items += i.Value + ",";
        }
    }

Then you will have all the selected users' email addresses in "items" which you can use to send email
0
 

Accepted Solution

by:
desiredforsome earned 0 total points
ID: 40371310
I figured it out.

I used the IN for the select statement

 foreach (System.Web.UI.WebControls.ListItem item in CheckBoxList1.Items)
        {
            if (item.Selected)
            {
                // If the item is selected, add the value to the list.
                YrStrList.Add(item.Value);
            }
            else
            {
                // Item is not selected, do something else.
            }
        }
        
        String YrStr = String.Join("','", YrStrList.ToArray());

        string startquery = String.Concat("SELECT EMAILADDRESS FROM OUTLOOKREPORT.DBO.MFDIRECTORY WHERE name IN('", YrStr);
        
        selectemailquery = string.Concat(startquery, "')");
        List<String> emailfind = new List<string>();
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandText = selectemailquery;
        

        con.Open();
        SqlDataReader reader1 = cmd.ExecuteReader();
        while (reader1.Read())
        {
          
            emailfind.Add(reader1[0].ToString());

        }
        multiemail = string.Concat("\"",string.Join(",", emailfind.ToArray()), "\"");

Open in new window

0
 

Author Closing Comment

by:desiredforsome
ID: 40379422
I figured it out
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now