Drop down list not populated

Thd drop down list on this form is not populated. It should.
I'm suspecting my connecting string is not correct.
Also when I submit the form , I'm getting this error on LINE 41:
Please help

=================
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 39:     Dim I as Integer
Line 40:     DBConn = New OleDbConnection("Provider=sqloledb;server=localhost;Initial Catalog=test;DataSource=local;User Id=pojo;Password=CANCER6768;")
Line 41:     DBCommand = New OleDbDataAdapter("Select Distinct CustomerEmail from Customers Where GroupName = '" & ddlGroupName.SelectedItem.Text & "'", DBConn)
Line 42:     DBCommand.Fill(DSPageData,"Emails")
Line 43:     TheMailMessage.From = txtFrom.Text
 ==============================================================


<%@ Import Namespace="System.Data.OLEDB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Web.Mail" %>
<%@ Page Language="vb" Debug="true" CodeBehind="index.aspx.vb" AutoEventWireup="false" Inherits="test.index" %>
<HTML>
      <HEAD>
            <TITLE>Email Blast</TITLE>
            <script runat="server">
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
    If Not IsPostBack Then
        Dim DBConn as OleDbConnection
        Dim DBCommand As OleDbDataAdapter
        Dim DSPageData as New DataSet
        DBConn = New OleDbConnection("Provider=sqloledb;server=localhost;Initial Catalog=testDB;DataSource=local;User Id=MOPLK;Password=OLFACTORY;")
           
           
        DBCommand = New OleDbDataAdapter ("Select Distinct GroupName from Customers Order By GroupName", DBConn)
        DBCommand.Fill(DSPageData, "Groups")
        ddlGroupName.DataSource = DSPageData.Tables("Groups").DefaultView
        ddlGroupName.DataBind()
       

    End If
End Sub
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
    Dim DBConn as OleDbConnection
    Dim DBCommand As OleDbDataAdapter
    Dim DSPageData as New DataSet
  Dim ThemailMessage As System.Web.Mail.MailMessage
    Dim TheMailConnection  
 
 
'System.Web.Mail.MailMessage
'Dim messageText As String =3D ""
'Dim strHTML as String =3D ""
   
   
   
    Dim I as Integer
    DBConn = New OleDbConnection("Provider=sqloledb;server=localhost;Initial Catalog=testDB;DataSource=local;User Id=MOPLK;Password=OLFACTORY;")
    DBCommand = New OleDbDataAdapter("Select Distinct CustomerEmail from Customers Where GroupName = '" & ddlGroupName.SelectedItem.Text & "'", DBConn)
    DBCommand.Fill(DSPageData,"Emails")
    TheMailMessage.From = txtFrom.Text
    TheMailMessage.Subject = txtSubject.Text
    TheMailMessage.Body = txtMessage.Text
    For I = 0 to DSPageData.Tables("Emails"). Rows.Count - 1
        TheMailMessage.To = DSPageData.Tables("Emails"). Rows(0).Item("CustomerEmail")
        TheMailConnection.Send(TheMailMessage)
    Next
    if DSPageData.Tables("Emails").Rows.Count = 1 then
        lblMessage.Text = "Your message has been sent to 1" _
            & " person. You can now send another email blast."
    Else
        lblMessage.Text = "Your message has been sent to " _
            & DSPageData.Tables("Emails").Rows.Count _
            & " people. You can now send another email blast."
    End If
End Sub
            </script>
      </HEAD>
      <BODY BACKGROUND="./lp.gif" TEXT="darkslateblue" LINK="darkred" VLINK="darkred" ALINK="red"
            LEFTMARGIN="140">
            <form runat="server">
                  <asp:Label id="lblTitle" Font-Size="25pt" Font-Name="Tahoma" Text="Email Blast" runat="server" />
                  <BR>
                  <IMG SRC="/test/images/line.gif"> <Font Face="Tahoma">
                        <BR>
                        <BR>
                        <asp:Label id="lblMessage" Font-Size="12pt" Font-Name="Tahoma" Font-Bold="True" runat="server"
                              Text="Select the group that you wish to send the email to &#13;&#10;        then enter your message." />
                        <BR>
                        <BR>
                        <asp:dropdownlist id="ddlGroupName" runat="server" DataTextField="GroupName"></asp:dropdownlist>
                        <BR>
                        <IMG SRC="/test/images/line.gif"> <Font Face="Tahoma">
                              <BR>
                              <Table>
                                    <TR>
                                          <TD>
                                                <B>From:</B>
                                          </TD>
                                          <TD>
                                                <asp:TextBox id="txtFrom" Columns="40" MaxLength="100" runat="server" />
                                                <asp:RequiredFieldValidator id="rfvFrom" ControlToValidate="txtFrom" Display="Dynamic" Font-Name="Verdana" Font-Size="10pt"
                                                      runat="server">
    <BR>The From field is Required!
</asp:RequiredFieldValidator>
                                          </TD>
                                    </TR>
                                    <TR>
                                          <TD>
                                                <B>Subject:</B>
                                          </TD>
                                          <TD>
                                                <asp:TextBox id="txtSubject" Columns="40" MaxLength="100" runat="server" />
                                                <asp:RequiredFieldValidator id="rfvSubject" ControlToValidate="txtSubject" Display="Dynamic" Font-Name="Verdana"
                                                      Font-Size="10pt" runat="server">
    <BR>The Subject field is Required!
</asp:RequiredFieldValidator>
                                          </TD>
                                    </TR>
                                    <TR>
                                          <TD VAlign="top">
                                                <B>Message:</B>
                                          </TD>
                                          <TD>
                                                <asp:TextBox id="txtMessage" Columns="40" Rows="5" runat="server" TextMode="MultiLine" />
                                                <asp:RequiredFieldValidator id="rfvMessage" ControlToValidate="txtMessage" Display="Dynamic" Font-Name="Verdana"
                                                      Font-Size="10pt" runat="server">
    <BR>The Message field is Required!
</asp:RequiredFieldValidator>
                                          </TD>
                                    </TR>
                              </Table>
                              <BR>
                              <asp:button id="butOK" text="  OK  " Type="Submit" OnClick="SubmitBtn_Click" runat="server" />
                        </Font>
            </form>
            </FONT>
      </BODY>
</HTML>
iyiolaAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
daffodilsConnect With a Mentor Commented:
Okay, so the DropDownList is not getting populated in the first place !

Have you tested the connection .. as to if you are getting connected to the database in the first place?
Looks like you are not sure...
>>I'm sure there is data in teh database but not sure I'm connected to it.

Create a new test page. Put a DataGrid into it and using your connection string, databind it to the Customers Table.
Does it connect to the Database and show all records on the DataGrid??
If not, check your Connection String.

Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
    If Not IsPostBack Then
        Dim DBConn as OleDbConnection
        Dim DBAdpater As OleDbDataAdapter
        Dim DSTest as New DataSet

        DBAdapter = New OleDbDataAdapter ("Select * from Customers", DBConn)
        DBAdpater.Fill(DSTest, "Customers")
        dgTest.DataSource = DSTest.Tables("Customers").DefaultView
        dgTest.DataBind()
  End If
End Sub
0
 
thrill_houseCommented:
Try changing:
 DBCommand = New OleDbDataAdapter("Select Distinct CustomerEmail from Customers Where GroupName = '" & ddlGroupName.SelectedItem.Text & "'", DBConn)

to

 DBCommand = New OleDbDataAdapter("Select Distinct CustomerEmail from Customers Where GroupName = '" & ddlGroupName.SelectedValue & "'", DBConn)

I've never used ddl.SelectedItem.Text, so I don't know if this will work or not, but it might be looking at an empty item (which may not exist), which is why it is throwing an error.
-Thrill
0
 
iyiolaAuthor Commented:
Hi Thrill, Thanks but one more thing. The drop down is not populating. I don't knw why this is so.
There is record in the database.
Any suggestion?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
vinhthuy_nguyenCommented:
Hi,

did you try to run your query directly in SQL Server?
how about the DataValueField in your ddl, I don't see it in the property ?
Just my thinking.Just forget it if you went through already
0
 
daffodilsCommented:
Set the DataTextField and DataValueField

"DataTextField" to the column name you want to display on the combo box.
"DataValueField" to the column name you want to retrieve as value.
0
 
iyiolaAuthor Commented:
Hi daffodis and vinhthuy unguyen
I did already, still not working.

<asp:dropdownlist id="ddlGroupName" runat="server" DataTextField="GroupName"></asp:dropdownlist>
0
 
daffodilsCommented:
It should be as..
<asp:dropdownlist id="ddlGroupName" runat="server" DataTextField="GroupName" DataValueField="GroupName"></asp:dropdownlist>

Try commenting out line 41.
Then do a simple Response.Write for "ddlGroupName.SelectedItem.Text" in the SubmitBtn_Click sub.

Does it print the Group Name??
0
 
iyiolaAuthor Commented:
No Response.write did not print the Group Name. I'm sure there is data in teh database but not sure I'm connected to it.  I'm not getting any errors again, but the  drop down list is not populating. It's suppose to when get to our home page (this page)

0
All Courses

From novice to tech pro — start learning today.