Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8605715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:54:29+00:00 2026-06-12T02:54:29+00:00

I am creating a web user control using C# to play some .wav files

  • 0

I am creating a web user control using C# to play some .wav files in the server. Following is my code.

public partial class WaveFilePlayer : System.Web.UI.UserControl
{
    //private string[] files;
    private static string[] files;

    protected void ButtonLoad_Click(object sender, EventArgs e)
    {
        string resourcePath = ConfigurationManager.AppSettings["ResourcePath"];
        string searchPattern = ConfigurationManager.AppSettings["SearchPattern"];

        files = System.IO.Directory.GetFiles(path, searchPattern);
    }

    protected void ButtonPlay_Click(object sender, EventArgs e)
    {
        int selectedIndex = ListBoxFiles.SelectedIndex;

        SoundPlayer soundPlayer = new SoundPlayer(files[selectedIndex]);
        soundPlayer.Play();
    }

As seen in the code above I declare string[] files as a member variable. Then I assign it in the ButtonLoad_Click method. But it throws a NullReferenceException when I try to access it in the ButtonPlay_Click method unless string[] files is declared static.

Does it mean that a new object of System.Web.UI.UserControl is not created while loading the user control in a asp.net page? And does it mean that when multiple clients (browsers) try to play the .wav files, only one instance of the string[] files is created at the server to be used by all clients?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:54:30+00:00Added an answer on June 12, 2026 at 2:54 am

    I suspect the problem isn’t that it can’t be accessed, at all. I strongly suspect that the problem is that you’ve got two different requests, and therefore two different instances of WaveFilePlayer – so when you’re executing ButtonPlay_Click, the files variable will be null unless you’ve written some code to persist it between requests.

    Options here would be view state or some server-side session. You should not use a static variable, as that will basically mean you’ve got a single variable shared between all requests (for all users).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a web user control and want to get some html from a
I am creating a web user control for showing Alert on AspxGridView Columns(Delete/Edit) click
I have simple web user control (the code I found somewhere in the web):
I have a UserControl-derived control the displays some information fetched from a web server.
I am creating a web control by addin the html in the code behind,
I am creating a web user control for a simple poll. I am currently
I am creating Web User Controls via code (my C# code write-out markup, code-behind
I'm creating a web application on Google AppEngine where I want the user to
I am creating a web application that will have many users. Each user has
I am interested in creating a simple web application that will take in user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.