Solved

Drop down list not populated

Posted on 2004-10-21
371 Views
Last Modified: 2008-02-01
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>
0
Question by:iyiola
    8 Comments
     
    LVL 8

    Expert Comment

    by:thrill_house
    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
     

    Author Comment

    by:iyiola
    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
     
    LVL 4

    Expert Comment

    by:vinhthuy_nguyen
    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
     
    LVL 8

    Expert Comment

    by:daffodils
    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
     

    Author Comment

    by:iyiola
    Hi daffodis and vinhthuy unguyen
    I did already, still not working.

    <asp:dropdownlist id="ddlGroupName" runat="server" DataTextField="GroupName"></asp:dropdownlist>
    0
     
    LVL 8

    Expert Comment

    by:daffodils
    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
     

    Author Comment

    by:iyiola
    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
     
    LVL 8

    Accepted Solution

    by:
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
    In this Experts Exchange video Micro Tutorial, I'm going to show how small business owners who use Google Apps can save money by setting up what is called a catch-all email address in their Gmail accounts. By using the catch-all feature, small busin…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    913 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

    16 Experts available now in Live!

    Get 1:1 Help Now