Link to home
Start Free TrialLog in
Avatar of ndr-itsolutions
ndr-itsolutions

asked on

Read email address from database

I have a web form created in Web Dev express and one of the fields is a drop down for a resource, in the database the table has resource name and resource email address, so to select it I can run the following:

select email from Resource_Ref_data where resource = ('strResource')

strResource is the drop down text and is populated from the Resource name field.

I want, when the submit button is clicked on this form to send an email automatically to the selected resource, by using the above statement.

My issue is, how can i run this statement and then assign the output to strResourceEmail ?

So that in turn I can modify my send mail routine:
 mail.To.Add("strResourceEmail")
Avatar of esolve
esolve
Flag of South Africa image

You html:

      <asp:DropDownList ID="ddlResource" runat="server" AppendDataBoundItems="True">
        <asp:ListItem Selected="True">Please Select</asp:ListItem>
      </asp:DropDownList>
      <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />
   
Your code behind:

 protected void Page_Load(object sender, EventArgs e)
    {
      if (!Page.IsPostBack)
      {
        //populate ddl (Should be in a BusinessLayer)
        DataSet ds = new DataSet();
        DataTable dt = new DataTable("Resource");
        dt.Columns.Add("ResourceName");
        dt.Columns.Add("ResourceEmail");
        ds.Tables.Add(dt);

        SqlConnection cn = new SqlConnection("SERVER=.;DATABASE=usersDB;UID=sa;PWD=whatever;");
        SqlCommand cm = new SqlCommand();
        cm.CommandText = "SELECT Name as ResourceName, Email as ResourceEmail FROM members";
        cm.Connection = cn;
        SqlDataAdapter da = new SqlDataAdapter(cm);
        da.Fill(ds, "Resource");

        ddlResource.DataSource = ds;
        ddlResource.DataMember = "Resource";
        ddlResource.DataTextField = "ResourceName";
        ddlResource.DataValueField = "ResourceEmail";
        ddlResource.DataBind();
      }

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
      if (ddlResource.SelectedValue != null)
      {
        string sEmail = ddlResource.SelectedValue;

        //code to send email
      }
    }

Not sure if this is what you want. Otherwise paste some code

Hope it helps
Avatar of ndr-itsolutions
ndr-itsolutions

ASKER

Dim strBuild, strEnv, strComponent, strSubComponent, strResource, strStatus, strComments, strDate As String
        strBuild = TextBox1.Text
        strEnv = EnvironmentDropDown.Text
        strComponent = ComponentDropDown.Text
        strSubComponent = SubComponentDropdown.Text
        strResource = ResourceDropDown.Text
        strStatus = StatusDropDown.Text
        strDate = Format(CDate(Calendar1.SelectedDate), "MM-dd-yyyy")
        strComments = Comments.Text
        'declaring sql connection. 
        Dim sqlcon As New SqlConnection("Data Source=SQL01;Initial Catalog=EMA;Persist Security Info=True;User ID=EMA_Service;Password=********")
        'checking if connection is open. if open close it and open again.
        If sqlcon.State = ConnectionState.Open Then
            sqlcon.Close()
        End If
        sqlcon.Open()
        Dim strcommand As String
        'the insert command is stored in strcommand
        'Table Name : Release_Tracker
        'Column of table : Environment, Current_Version, Component, SubComponent, Resource, Status, Date, Comments
        strcommand = "Insert into Release_Tracker(Environment,Current_Version,Component,SubComponent,Resource,Status,Date,Comments) values ('" + strEnv + "','" + strBuild + "','" + strComponent + "','" + strSubComponent + "','" + strResource + "','" + strStatus + "','" + strDate + "','" + strComments + "')"
        Dim sqlcomm As New SqlCommand(strcommand, sqlcon)
        Dim o As String = sqlcomm.ExecuteNonQuery()
        SubmitLabel.Text = "Entry Added!"
        'Send Email
        'create the mail message
        'Read in the email address
        If sqlcon.State = ConnectionState.Open Then
            sqlcon.Close()
        End If
        
        Dim mail As New MailMessage()
        'set the addresses
        mail.From = New MailAddress("test@email.com")
        mail.To.Add("strResourceEmail")
        'set the content
        mail.Subject = "EMA - Task Awaiting"
        mail.Body = "The following task has been added: <br></br><b>Environment: " + strEnv + "</b><br></br>" + strBuild + "<br></br>" + strComponent + "<br></br>" + strSubComponent + "<br></br>" + strResource + "<br></br>" + strComments + "<br></br>" + strDate
        mail.IsBodyHtml = True
        'send the message
        Dim smtp As New SmtpClient("smtp.server")
        smtp.Send(mail)

    End Sub

Open in new window


And the code from the page
<asp:DropDownList ID="ResourceDropDown" runat="server" DataSourceID="Resource" 
        DataTextField="Resource" DataValueField="Resource" Height="23px" 
        Width="217px">
</asp:DropDownList>
	
<asp:SqlDataSource ID="Resource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:EMAConnectionString %>" 
        SelectCommand="SELECT [Resource] FROM [Resource_Ref_Data]">
</asp:SqlDataSource>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of esolve
esolve
Flag of South Africa 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
Spot ON