You must Sign In to post a response.
  • Category: ASP.NET

    SpeechSynthesizer with Repeater control in Asp.Net C#

    Can anyone please help me to solve is issue.
    Am using repeater control to load the data from database. Its working properly, though am going this for visually impaired persons I am using SpeechSynthesizer to serve an audio output to the users.

    Without using repeater the audio is working perfectly , but I need to load 100's of data to the page, so I bound to repeater. Using repeater the SpeechSynthesizer is not functioning. Please check my code and do the needful.

    SOURCE CODE:


    <%@ Page language="C#" autoeventwireup="true" codefile="Repeater.aspx.cs" inherits="Repeater"
    async="true" %>

    <%@ Register assembly="AjaxControls" namespace="AjaxControls" tagprefix="cc1" %>
    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">

    <asp:ScriptManager id="ScriptManager1" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel id="UpdatePanel1" runat="server">
    <ContentTemplate>
    <div>
    <asp:Repeater id="rptCustomers" runat="server">
    <HeaderTemplate>
    <table border="1">
    <tr>
    <th scope="col" style="width: 80px">S.No
    </th>
    <th scope="col" style="width: 80px">Option A
    </th>
    <th scope="col" style="width: 120px">Option B
    </th>
    </tr>
    </HeaderTemplate>
    <ItemTemplate>
    <tr>
    <td>
    <asp:Label id="lbl_Sno" runat="server" text='<%# Bind("OrtAna_SNo") %>'></asp:Label>
    </td>
    <td>
    <asp:RadioButton id="rdOrtAna_ADesc" runat="server" text='<%# Bind("OrtAna_ADesc") %>'
    groupname="Option" />
    </td>
    <td>
    <asp:RadioButton id="rdOrtAna_BDesc" runat="server" text='<%# Bind("OrtAna_BDesc") %>'
    groupname="Option" />
    </td>
    </tr>
    <tr>
    <td colspan="3">
    <asp:Button id="btn_Spk" runat="server" text="Read" commandname="Speak" />
    </td>
    </tr>
    </ItemTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    </asp:Repeater>
    </div>
    </ContentTemplate>
    </asp:UpdatePanel>
    </form>
    </body>
    </html>


    CODE BEHIND:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Speech;
    using System.Speech.Synthesis;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Threading;


    public partial class Repeater : System.Web.UI.Page
    {
    SpeechSynthesizer sp1 = new SpeechSynthesizer();
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    bindgrid();
    }
    }

    public SqlConnection Con()
    {
    string Connectionstring = string.Empty;
    Connectionstring = ConfigurationManager.ConnectionStrings["DBCon"].ToString();
    SqlConnection conn = new SqlConnection(Connectionstring);
    return conn;
    }

    public void bindgrid()
    {
    SqlConnection conn = Con();
    SqlCommand cmd = new SqlCommand("select * from dbo.OrtAna order by OrtAna_SNo", conn);
    cmd.CommandType = CommandType.Text;
    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
    {
    DataTable dt = new DataTable();
    sda.Fill(dt);
    rptCustomers.DataSource = dt;
    rptCustomers.DataBind();
    }
    }
    override protected void OnInit(EventArgs e)
    {
    base.OnInit(e);
    rptCustomers.ItemCommand += new RepeaterCommandEventHandler(rptCustomers_ItemCommand);
    }
    protected void rptCustomers_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
    RadioButtonList rbt1 = (RadioButtonList)(rptCustomers.FindControl("rdOrtAna_ADesc"));
    RadioButtonList rbt2 = (RadioButtonList)(rptCustomers.FindControl("rdOrtAna_BDesc"));

    sp1.Volume = 100;
    sp1.Rate = -3;

    if (e.CommandName == "Speak")
    {
    if (e.Item.ItemIndex == 0)
    {
    sp1.SpeakAsync(rbt1.Text); // Here the code gets keep on loading.
    sp1.Pause();
    Thread.Sleep(1500);
    sp1.Resume();
    sp1.SpeakAsync(rbt2.Text);
    sp1.Pause();
    Thread.Sleep(1500);
    }
    if (e.Item.ItemIndex == 1)
    {
    sp1.SpeakAsync(rbt1.Text);
    sp1.Pause();
    Thread.Sleep(1500);
    sp1.Resume();
    sp1.SpeakAsync(rbt2.Text);
    sp1.Pause();
    Thread.Sleep(1500);
    }
    if (e.Item.ItemIndex == 2)
    {
    sp1.SpeakAsync(rbt1.Text);
    sp1.Pause();
    Thread.Sleep(1500);
    sp1.Resume();
    sp1.SpeakAsync(rbt2.Text);
    sp1.Pause();
    Thread.Sleep(1500);
    }
    if (e.Item.ItemIndex == 3)
    {
    sp1.SpeakAsync(rbt1.Text);
    sp1.Pause();
    Thread.Sleep(1500);
    sp1.Resume();
    sp1.SpeakAsync(rbt2.Text);
    sp1.Pause();
    Thread.Sleep(1500);
    }
    if (e.Item.ItemIndex == 4)
    {
    sp1.SpeakAsync(rbt1.Text);
    sp1.Pause();
    Thread.Sleep(1500);
    sp1.Resume();
    sp1.SpeakAsync(rbt2.Text);
    sp1.Pause();
    Thread.Sleep(1500);
    }
    if (e.Item.ItemIndex == 5)
    {
    sp1.SpeakAsync(rbt1.Text);
    sp1.Pause();
    Thread.Sleep(1500);
    sp1.Resume();
    sp1.SpeakAsync(rbt2.Text);
    sp1.Pause();
    Thread.Sleep(1500);
    }
    //need more itemindex to be looped; because having 100 of records to be accessed.
    }
    }
    }







    With Warm Regards,
    Narendran Namachivayam
    +91 9940 678 414
    http://www.chennaitrekkers.org/
    https://www.facebook.com/chennaicoastalcleanup/
  • #765993
    Hi,
    Can you please tell that did you found rbt1 properly using FindControl?
    If yes then rbt1.Text contains proper text you want?
    I suggest you to do following:
    Instead of this:
    RadioButtonList rbt1 = (RadioButtonList)(rptCustomers.FindControl("rdOrtAna_ADesc"));
    Use this:
    RadioButtonList rbt1 = (RadioButtonList)(e.Item.FindControl("rdOrtAna_ADesc"));

  • #765996
    Hi,
    Thanks a lot for your valuable response.
    It is also not working for me. The rbt1 is showing null, but already we have loaded the data from db na?


    Narendran Namachivayam,

  • #766000
    I found the answer from

    http://www.aspforums.net/Threads/130496/SpeechSynthesizer-with-Repeater-control/Replies/1#Replies


    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TT.aspx.cs" Inherits="TT" Async="true" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Text to Speech </title>
    </head>
    <body>
    <form id="form1" runat="server">

    <div>
    <asp:Repeater ID="rptOA" runat="server">
    <HeaderTemplate>
    <table border="1">
    <tr>
    <th scope="col" style="width: 80px">
    S.No
    </th>
    <th scope="col" style="width: 80px">
    Option A
    </th>
    <th scope="col" style="width: 120px">
    Option B
    </th>
    </tr>
    </HeaderTemplate>
    <ItemTemplate>
    <tr>
    <td>
    <asp:Label ID="lbl_Sno" runat="server" Text='<%# Bind("OrtAna_SNo") %>'></asp:Label>
    </td>
    <td>
    <asp:RadioButton ID="rdOrtAna_ADesc" runat="server" Text='<%# Bind("OrtAna_ADesc") %>'
    GroupName="Option" />
    </td>
    <td>
    <asp:RadioButton ID="rdOrtAna_BDesc" runat="server" Text='<%# Bind("OrtAna_BDesc") %>'
    GroupName="Option" />
    </td>
    </tr>
    <tr>
    <td colspan="3">
    <asp:Button ID="btn_Spk" runat="server" Text="Read" CommandName="Speak" />
    </td>
    </tr>
    </ItemTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    </asp:Repeater>
    </div>


    </form>
    </body>
    </html>

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Speech;
    using System.Speech.Synthesis;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Threading;
    public partial class TT : System.Web.UI.Page
    {
    SpeechSynthesizer sp1 = new SpeechSynthesizer();
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    binddata();
    }
    }

    public SqlConnection Con()
    {
    string Connectionstring = string.Empty;
    Connectionstring = ConfigurationManager.ConnectionStrings["DBCon"].ToString();
    SqlConnection conn = new SqlConnection(Connectionstring);
    return conn;
    }

    public void binddata()
    {
    SqlConnection conn = Con();
    SqlCommand cmd = new SqlCommand("select * from dbo.OrtAna order by OrtAna_SNo", conn);
    cmd.CommandType = CommandType.Text;
    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
    {
    DataTable dt = new DataTable();
    sda.Fill(dt);
    rptOA.DataSource = dt;
    rptOA.DataBind();
    }
    }
    protected override void OnInit(EventArgs e)
    {
    base.OnInit(e);
    rptOA.ItemCommand += new RepeaterCommandEventHandler(rptOA_ItemCommand);
    }

    protected void rptOA_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
    RadioButton rbt1 = (RadioButton)(e.Item.FindControl("rdOrtAna_ADesc"));
    RadioButton rbt2 = (RadioButton)(e.Item.FindControl("rdOrtAna_BDesc"));
    sp1.SelectVoiceByHints(VoiceGender.Male);
    sp1.Volume = 100;
    sp1.Rate = 0;

    if (e.CommandName == "Speak")
    {
    sp1.SpeakAsync(rbt1.Text);
    Thread.Sleep(100);
    sp1.SpeakAsync(rbt2.Text);
    }
    }
    }

  • #766006
    what error you got ? please elaborate so that we can help you better in order to resolve a issue.
    Meanwhile, have you tried using gridview control ? just try with it, I think it will work with gridview

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #766008
    Hi,

    Nice to here to resolve the issue by your own, After I read your post I comment only one thing, OnRowCommand event for each index you make separate code, that is 100% wrong, because in post itself you mentioned that you have 100's of records, for 100 records are you maintain separate, separate code blocks? Samething if you observe in your resolution code block they wrote only one solution for all the indexes. So, keep remember this point for future development.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #766383
    Thanks a lot Mr. Naveen.


    Need help for :

    http://www.dotnetspider.com/forum/344715-SpeechSynthesizer-and-SpeechRecognition-using-aspnet-c.aspx

    Regards,
    Narendran Namachivayam


  • Sign In to post your comments