Link to home
Start Free TrialLog in
Avatar of lucinda_rhian
lucinda_rhian

asked on

Getting the results from a dataset populated checkboxlist

Hi

I am trying to create a page whcih does the following using VB.NET:

I want to display a form the results of which are emailed, part of the form contains a checkboxlist, the contents of which are populated from a dataset I have.

I can get the checkboxlist populated with the relevant sections of the dataset, and I can make the form email.  The bit I can't do is get the multiple selections from the checkboxlist in to the email body content!

Thanks!
Avatar of mcgants
mcgants

try this:

Dim item As ListItem
For Each item In cbl1.Items
  If item.Selected = True Then
    txt1.Text &= item.Text & " "
  End If
Next

replace item.text with item.value if you just want the value,
replace txt1.text with whatever variable you are using for your body text

hope this helps,
mcg
Avatar of lucinda_rhian

ASKER

I tried this and it picks up nothing even though some of the check boxes are checked.

Could my problem be related to the fact that my dataset and id of my checkboxlist control are the same?  ie.  I create a dataset and then use the dataset name as the id of my chcklistbox to get the data from in in the checklistbox, then reference the id in the code above to extract the selections.

Thanks
<%@ Register TagPrefix="Control1" TagName="Menu" Src="../menu/menu.ascx" %>
<%@ Page Language="VB"%>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Web.Mail" %>
<HTML>
<HEAD>
<title>Form Request</title>
<script language="VB" runat="server">
sub Page_Load(sender as Object, e as EventArgs)

Dim connString as String
connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=C:\Inetpub\wwwroot\forms\btforms.mdb;"
   
'Open a connection
Dim objConnection as OleDbConnection
objConnection = New OleDbConnection(connString)
objConnection.Open()
   
'Specify the SQL string
Dim strSQL as String = "SELECT * FROM Forms WHERE AvailableElectronically=false"

'Create a command object
Dim objCommand as OleDbCommand
objCommand = New OleDbCommand(strSQL, objConnection)

'Get a datareader
Dim objDataReader as OleDbDataReader
objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)

'Do the DataBinding
formResults.DataSource = objDataReader
formResults.DataBind()    
   
'Close the datareader/db connection
objDataReader.Close()
end sub
                  
                  Sub btnSendFeedback_Click(sender as Object, e as EventArgs)

                        'Create an instance of the MailMessage class
                        Dim objMM as New MailMessage()

                        'Set the properties - send the email to the person who filled out the
                        'feedback form.
                        objMM.To = "lucinda.marchant@brandontrust.org"
                        objMM.From = txtEmail.Text

                        'Send the email in text format
                        objMM.BodyFormat = MailFormat.Text
                        '(to send HTML format, change MailFormat.Text to MailFormat.Html)

                        'Set the priority - options are High, Low, and Normal
                        objMM.Priority = MailPriority.Normal

                        'Set the subject
                        objMM.Subject = "BT Form Request"

                              Dim item As ListItem
                              Dim txt1 As String
                              For Each item In formResults.Items
                              If item.Selected = True Then
                                    txt1 &= item.Text & " "
                              End If
                              Next

                        'Set the body
                        objMM.Body = txt1

                         
                        'Specify to use the default Smtp Server
                        SmtpMail.SmtpServer = "earth.brandontrust.org"
                         
                        'Now, to send the message, use the Send method of the SmtpMail class
                        SmtpMail.Send(objMM)

                        panelSendEmail.Visible = false
                        panelMailSent.Visible = true
                  End Sub
                  </script>
                  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
      </HEAD>
      <body>
            <table cellPadding="0" width="100%" border="0">
                  <tr>
                        <td>&nbsp;</td>
                        <td align="center" width="900">
                              <DIV align="center">
                                    <table cellSpacing="0" cellPadding="0" width="900" align="center" border="0">
                                          <tr>
                                                <td width="95%" bgColor="#ff9900" height="91">
                                                      <div align="left"><IMG height="64" src="../images/atBrandon.gif" width="283">
                                                      </div>
                                                </td>
                                                <td width="5%" bgColor="#ff9900"><IMG height="89" src="../images/orangebglogo.gif" width="200">
                                                </td>
                                          </tr>
                                    </table>
                              </DIV>
                              <DIV align="center">
                                    <table cellSpacing="0" cellPadding="0" width="100%" align="center" bgColor="#0f298f">
                                          <tr>
                                                <td width="100%"><IMG height="4" src="../images/spacer.gif" width="1">
                                                </td>
                                          </tr>
                                    </table>
                              </DIV>
                              <DIV align="center">
                                    <table cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">
                                          <tr>
                                                <td vAlign="top" width="140" bgColor="#ff9900"><CONTROL1:MENU id="Menu1" runat="server"></CONTROL1:MENU></td>
                                                <td vAlign="top">
                                                      <table cellSpacing="12" cellPadding="0" width="100%" border="0">
                                                            <tr>
                                                                  <td width="100%">
                                                                        <div align="left">
                                                                              <h1 align="center">Hard Copy Forms Request</h1>
                                                                              <P>
                                                                                    <asp:panel id="panelSendEmail" runat="server">
                                                                                          <FORM id="Form1" runat="server">
                                                                                                <P>Please request forms not available electronically by filling in the form below:</P>
                                                                                                <P>
                                                                                                      <TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300" border="0">
                                                                                                            <TR>
                                                                                                                  <TD width="147">Your Name:</TD>
                                                                                                                  <TD>
                                                                                                                        <asp:textbox id="txtName" runat="server"></asp:textbox></TD>
                                                                                                            </TR>
                                                                                                            <TR>
                                                                                                                  <TD width="147">Your Email Address:</TD>
                                                                                                                  <TD>
                                                                                                                        <asp:textbox id="txtEmail" runat="server"></asp:textbox></TD>
                                                                                                            </TR>
                                                                                                            <TR>
                                                                                                                  <TD width="147">Your Work Base:</TD>
                                                                                                                  <TD>
                                                                                                                        <asp:textbox id="txtLocation" runat="server"></asp:textbox></TD>
                                                                                                            </TR>
                                                                                                      </TABLE>
                                                                                                <P>
                                                                                                      <asp:CheckBoxList id="formResults" runat="server" DataValueField="FormID" DataTextField="FormName"></asp:CheckBoxList><BR>
                                                                                                      <asp:button id="btnSendFeedback" onclick="btnSendFeedback_Click" runat="server" Text="Request Forms"></asp:button>
                                                                                          </FORM>
                                                                                    </asp:panel>
                                                                              </P>
                                                                              <P>
                                                                                    <asp:panel id="panelMailSent" runat="server" Visible="False">
                                                                                          <P>
                                                                                                <H2>Thank You</H2>
                                                                                          <P>Your forms have been requested and will be posted out to you shortly.</P>
                                                                                    </asp:panel>
                                          
                                                                              <P><IMG height="1" src="../images/spacer.gif" width="733"></P>
                                                                        </div>
                                                                  </td>
                                                            </tr>
                                                      </table>
                                                </td>
                                          </tr>
                                    </table>
                              </DIV>
                              <DIV align="center">
                                    <table cellSpacing="0" cellPadding="0" width="100%" align="center" bgColor="#0f298f">
                                          <tr>
                                                <td width="100%"><IMG height="8" src="../images/spacer.gif" width="1">
                                                </td>
                                          </tr>
                                    </table>
                              </DIV>
                        </td>
                        <td>&nbsp;
                        </td>
                  </tr>
            </table>
      </body>
</HTML>
ahh!  I was trying to post a well-formatted cut down version of my page - but pressed send too soon - I giess you cant edit your own posts here then!
# 1 set the DataValueField / DataTextField = to a column of a db field for formResults

#2 do the binding of the checkbox list formResults when Page.IsPostback=false
I have done both these things but still no luck!
A slightly more readable version of the code!

<%@ Page Language="VB"%>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Web.Mail" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
  <HEAD>
    <title>Form Request</title>
    <script language="VB" runat="server">
      sub Page_Load(sender as Object, e as EventArgs)
   
        Dim connString as String
        connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=C:\Inetpub\wwwroot\intranet\forms\btforms.mdb;"
        Dim objConnection as OleDbConnection
        objConnection = New OleDbConnection(connString)
        objConnection.Open()
        Dim strSQL as String = "SELECT * FROM Forms WHERE AvailableElectronically=false"
        Dim objCommand as OleDbCommand
        objCommand = New OleDbCommand(strSQL, objConnection)
        Dim objDataReader as OleDbDataReader
        objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)
        Page.IsPostback=false
        formResults.DataSource = objDataReader
        formResults.DataBind()    
        objDataReader.Close()

      end sub
                  
      Sub btnSendFeedback_Click(sender as Object, e as EventArgs)

        Dim objMM as New MailMessage()
        objMM.To = "me@somedomain.com"
        objMM.From = txtEmail.Text
        objMM.BodyFormat = MailFormat.Text
        objMM.Priority = MailPriority.Normal
        objMM.Subject = "BT Form Request"
      
        Dim item As ListItem
        Dim txt1 As String
      
        For Each item In formResults.Items
          If item.Selected = True Then
            txt1 &= item.Text & ", "
          End If
        Next

        objMM.Body = txt1
        SmtpMail.SmtpServer = "smtp.somedomain.com"
        SmtpMail.Send(objMM)
        panelSendEmail.Visible = false
        panelMailSent.Visible = true

      End Sub
    </script>
  </HEAD>
  <body>
    <h1 align="center">Hard Copy Forms Request</h1>
    <asp:panel id="panelSendEmail" runat="server">
      <P>Please request forms not available electronically by filling in the form below:</P>    
      <FORM id="Form1" runat="server">
        <TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300" border="0">
          <TR>
            <TD width="147">Your Name:</TD>
            <TD><asp:textbox id="txtName" runat="server"/></TD>
              </TR>
          <TR>
            <TD width="147">Your Email Address:</TD>
            <TD><asp:textbox id="txtEmail" runat="server"/></TD>
          </TR>
          <TR>                                                                  <TD width="147">Your Work Base:</TD>
      <TD><asp:textbox id="txtLocation" runat="server"/></TD>                               
          </TR>
        </TABLE>
        <asp:CheckBoxList id="formResults"                        
                                    runat="server"
                                    DataValueField="FormName"
                                    DataTextField="FormName"/>
        <BR>
        <asp:button id="btnSendFeedback" onclick="btnSendFeedback_Click" runat="server" Text="Request Forms"/>          
      </FORM>
    </asp:panel>
   
    <asp:panel id="panelMailSent" runat="server" Visible="False">
      <H2>Thank You</H2>
      <P>Your forms have been requested and will be posted out to you shortly.</P>
    </asp:panel>
  </body>
</HTML>
ASKER CERTIFIED SOLUTION
Avatar of NetDeveloper
NetDeveloper
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial