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!
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!
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
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
ASKER
<%@ Register TagPrefix="Control1" TagName="Menu" Src="../menu/menu.ascx" %>
<%@ Page Language="VB"%>
<%@ Import Namespace="System.Data.Ole Db" %>
<%@ 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.OL EDB.4.0;DA TA SOURCE=C:\Inetpub\wwwroot\ forms\btfo rms.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=fa lse"
'Create a command object
Dim objCommand as OleDbCommand
objCommand = New OleDbCommand(strSQL, objConnection)
'Get a datareader
Dim objDataReader as OleDbDataReader
objDataReader = objCommand.ExecuteReader(C ommandBeha vior.Close Connection )
'Do the DataBinding
formResults.DataSource = objDataReader
formResults.DataBind()
'Close the datareader/db connection
objDataReader.Close()
end sub
Sub btnSendFeedback_Click(send er 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@brandont rust.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> </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.g if" width="283">
</div>
</td>
<td width="5%" bgColor="#ff9900"><IMG height="89" src="../images/orangebglog o.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"><CONTROL 1: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:textb ox></TD>
</TR>
<TR>
<TD width="147">Your Email Address:</TD>
<TD>
<asp:textbox id="txtEmail" runat="server"></asp:textb ox></TD>
</TR>
<TR>
<TD width="147">Your Work Base:</TD>
<TD>
<asp:textbox id="txtLocation" runat="server"></asp:textb ox></TD>
</TR>
</TABLE>
<P>
<asp:CheckBoxList id="formResults" runat="server" DataValueField="FormID" DataTextField="FormName">< /asp:Check BoxList><B R>
<asp:button id="btnSendFeedback" onclick="btnSendFeedback_C lick" 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>
</td>
</tr>
</table>
</body>
</HTML>
<%@ Page Language="VB"%>
<%@ Import Namespace="System.Data.Ole
<%@ 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.OL
'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=fa
'Create a command object
Dim objCommand as OleDbCommand
objCommand = New OleDbCommand(strSQL, objConnection)
'Get a datareader
Dim objDataReader as OleDbDataReader
objDataReader = objCommand.ExecuteReader(C
'Do the DataBinding
formResults.DataSource = objDataReader
formResults.DataBind()
'Close the datareader/db connection
objDataReader.Close()
end sub
Sub btnSendFeedback_Click(send
'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@brandont
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> </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.g
</div>
</td>
<td width="5%" bgColor="#ff9900"><IMG height="89" src="../images/orangebglog
</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"
</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"><CONTROL
<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:textb
</TR>
<TR>
<TD width="147">Your Email Address:</TD>
<TD>
<asp:textbox id="txtEmail" runat="server"></asp:textb
</TR>
<TR>
<TD width="147">Your Work Base:</TD>
<TD>
<asp:textbox id="txtLocation" runat="server"></asp:textb
</TR>
</TABLE>
<P>
<asp:CheckBoxList id="formResults" runat="server" DataValueField="FormID" DataTextField="FormName"><
<asp:button id="btnSendFeedback" onclick="btnSendFeedback_C
</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"
</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"
</td>
</tr>
</table>
</DIV>
</td>
<td>
</td>
</tr>
</table>
</body>
</HTML>
ASKER
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
#2 do the binding of the checkbox list formResults when Page.IsPostback=false
ASKER
I have done both these things but still no luck!
ASKER
A slightly more readable version of the code!
<%@ Page Language="VB"%>
<%@ Import Namespace="System.Data.Ole Db" %>
<%@ 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.OL EDB.4.0;DA TA SOURCE=C:\Inetpub\wwwroot\ intranet\f orms\btfor ms.mdb;"
Dim objConnection as OleDbConnection
objConnection = New OleDbConnection(connString )
objConnection.Open()
Dim strSQL as String = "SELECT * FROM Forms WHERE AvailableElectronically=fa lse"
Dim objCommand as OleDbCommand
objCommand = New OleDbCommand(strSQL, objConnection)
Dim objDataReader as OleDbDataReader
objDataReader = objCommand.ExecuteReader(C ommandBeha vior.Close Connection )
Page.IsPostback=false
formResults.DataSource = objDataReader
formResults.DataBind()
objDataReader.Close()
end sub
Sub btnSendFeedback_Click(send er 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_C lick" 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>
<%@ Page Language="VB"%>
<%@ Import Namespace="System.Data.Ole
<%@ 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.OL
Dim objConnection as OleDbConnection
objConnection = New OleDbConnection(connString
objConnection.Open()
Dim strSQL as String = "SELECT * FROM Forms WHERE AvailableElectronically=fa
Dim objCommand as OleDbCommand
objCommand = New OleDbCommand(strSQL, objConnection)
Dim objDataReader as OleDbDataReader
objDataReader = objCommand.ExecuteReader(C
Page.IsPostback=false
formResults.DataSource = objDataReader
formResults.DataBind()
objDataReader.Close()
end sub
Sub btnSendFeedback_Click(send
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_C
</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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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