Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 471
  • Last Modified:

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!
0
lucinda_rhian
Asked:
lucinda_rhian
  • 5
  • 2
1 Solution
 
mcgantsCommented:
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
0
 
lucinda_rhianAuthor Commented:
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
0
 
lucinda_rhianAuthor Commented:
<%@ 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>
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
lucinda_rhianAuthor Commented:
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!
0
 
NetDeveloperCommented:
# 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
0
 
lucinda_rhianAuthor Commented:
I have done both these things but still no luck!
0
 
lucinda_rhianAuthor Commented:
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>
0
 
NetDeveloperCommented:
Hi,

Page.IsPostback=false
        formResults.DataSource = objDataReader
        formResults.DataBind()    


is not correct

it must be

If Page.IsPostback=false Then
        formResults.DataSource = objDataReader
        formResults.DataBind()    
End If

try now :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now