0

I have the following SP:

ALTER proc [CRM].[s_CRM_GetEmail]
(
    @EmailTemplateID int
)
AS 
BEGIN
    SELECT 
    CASE 
    WHEN @EmailTemplateID = 1 THEN 'This is EmailTemplate 1'
        WHEN @EmailTemplateID = 2 THEN 'This is EmailTemplate 2'
    END AS EmailBody
    FROM [CRM].[CRMData]
    WHERE CRM_ID = 1 
END

The following is my webform:

    <form id="form1" runat="server">

        <div>
<asp:DropDownList ID="Type" runat="server">
    <asp:ListItem Value="0" Selected="True">Select Email Template:</asp:ListItem>
    <asp:ListItem Value="1">Bike Template</asp:ListItem>
    <asp:ListItem Value="2">Car Template</asp:ListItem>
</asp:DropDownList>
    </div>
        <p><asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="OK" /></p>

</form>
 <textarea name="'emailbody" rows="20" cols="100"/> </textarea>

Lastly, the following is my code thus far:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;


namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        SqlCommand cmd = new SqlCommand();
        SqlConnection con = new SqlConnection();
        SqlDataAdapter sda = new SqlDataAdapter();
        DataSet ds = new DataSet();

        protected void Page_Load(object sender, EventArgs e)
        {
         con.ConnectionString = "Data Source=Server1;Initial Catalog=Server_Logistics;Integrated Security=True";
         con.Open();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            String id = Type.SelectedValue;
            int emailid = Int32.Parse(id);

            using (SqlCommand cmd = new SqlCommand("CRM.s_CRM_GetEmail", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@EmailTemplateID", SqlDbType.Int).Value = emailid;

                using(SqlDataReader rdr = cmd.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        Console.WriteLine(rdr["EmailBody"]);
                    }
                }
            }
        }
    }


}

I want to extract the contents based upon user drop-down box selection. So far I've got it to connect to DB but I'm not sure how to extract the data and display. Ideally I want to extract it in the textarea on the webform, but either way just want to extract data. I would be grateful for any guidance.

I've got it to finally extract! I've replaced Console.WriteLine(rdr["EmailBody"]); with Response.Write((rdr["EmailBody"])); Next question is is there a way to get this populating in the textarea on the webform?

skippy
  • 172
  • 4
  • 17
  • I've got it to finally extract! I've replaced Console.WriteLine(rdr["EmailBody"]); with Response.Write((rdr["EmailBody"])); Next question is is there a way to get this populating in the textarea on the webform? – skippy Jan 26 '21 at 14:21
  • You need to better understand the [life cycle of an asp.net form](https://learn.microsoft.com/en-us/previous-versions/aspnet/ms178472(v=vs.100)). Then you can start to better understand how a POST operation works and how to handle populating control values with data on postback. – Igor Jan 26 '21 at 14:27
  • As a side note if you are building something new then I would strongly recommend you take a step back and learn MVC or learn an SPA framework instead. ASP.NET webforms is considered legacy framework by microsoft. It is only good for maintaining and extending existing code basis. – Igor Jan 26 '21 at 14:29
  • thanks for your comments @igor. It's really something the business want quick and I don't have the skills to deliver. I am proposing to learn MVC and SPA frameworks, but for now just wanted a quick, easy solution. I feel I'm nearly there, got the correct data extracting after user selection just wanted it displaying in the textarea. I know it something to do with the property settings and pointing to it, but not sure how at the moment. – skippy Jan 26 '21 at 14:32
  • 1
    Add `runat="server"` in your `textarea` in the `.aspx` form as well as an `ID`. Now you can access it from your code behind in the postback and set the value. – Igor Jan 26 '21 at 14:35
  • That's amazing @igor exactly what I was trying to work out. So do I set it by something like emailbodywrite = (rdr(["EmailBody"]));? emailbodywrite being the textarea id – skippy Jan 26 '21 at 14:43
  • Never mind fixed it - emailbodywrite.InnerHtml = rdr["EmailBody"].ToString(); in case anybody was wondering. – skippy Jan 26 '21 at 14:54
  • **See [C# Data Connections Best Practice?](https://stackoverflow.com/questions/17552829/c-sharp-data-connections-best-practice)** – Charlieface Jan 26 '21 at 16:13

0 Answers0