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

  • Home
  • SEARCH
  • 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 7502641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:56:44+00:00 2026-05-29T20:56:44+00:00

I’ve been trying to get this to work for a while but can’t (I

  • 0

I’ve been trying to get this to work for a while but can’t (I am fairly new to C# and OOP in general).

Basically, I have this piece of code on a second form:

private void button1_Click(object sender, EventArgs e)
{
    if (charCount > 2 && charCount < 30)
    {
        try
        {
            conn.Open();
        }
        catch (Exception ex)
        {
        //Error handling code here
        }
        finally
        {
            conn.Close();
        }
        //Run the SQL statements
        try
        {

        //SQL insert data is here

        }
        catch (Exception ie)
        {
            MessageBox.Show(ie.Message);
        }
        finally
        {
            //Close the connection
            if (conn.State != ConnectionState.Closed)
            {
                conn.Close();
            }

            mainForm.refreshCall();

            this.Close();
        }

    }
    else
    {
        MessageBox.Show("Part numbers can be between 2 and 30 characters.\n Yours was " + charCount + " characters long.", "Error");
    }

}

It all runs fine and does what it’s supposed to, which is insert some data into a SQL database (I took that code out to make it a little cleaner). So after all that happens, I try to execute mainForm.refreshCall(). This refreshCall method exists on my first form, or mainForm, and looks like this:

public static void refreshCall()
{
    SqlConnection conn = new SqlConnection("Data Source=DSERVER\\NEW_SQL;Initial Catalog=AWSoftware;Integrated Security=True");
    try
    {
        conn.Open();
        DataSet ds = new DataSet();
        SqlDataAdapter adapter = new SqlDataAdapter("SELECT part_num from dbo.CustomParts", conn);
        adapter.Fill(ds);
        this.listParts.DataSource = ds.Tables[0];
        this.listParts.DisplayMember = "part_num";
        conn.Close();
    }

    catch (SqlException odbcEx)
    {
        MessageBox.Show("There was an error connecting to the data source\nError Code: 1001", "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

However, this.listParts.DataSource and this.listParts.DisplayMember both tell me that they are not valid static properties Error, static method, or static field initialize. I am absolutely baffled by what that means. If someone would be so kind as to shed some light on this for me, I would greatly appreciate it!

  • 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-05-29T20:56:45+00:00Added an answer on May 29, 2026 at 8:56 pm

    Your refreshCall is marked as static, but you are referencing instantiated variables, in this case, your ListBox, so that won’t work. You either have to pass in the ListBox as a parameter reference, or just remove the static attribute.

    Simplest way:

    public void refreshCall() {
      // blah-blah
    }
    

    then the method call changes to:

    this.refreshCall();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to get this working for quite a while now but
I have been trying to get this code to work for a while now
I've been trying to get this to work for quite a while now but
This is crazy, I have been trying for hours to get this to work.
I have been trying to work this out for a while and I just
I've been trying to get this code to work for hours! All I need
I've been trying to get this module to work and no matter what I've
I have been trying to get this one section of my UI to immediatly
I have been trying to get around this error for a day now and
I have been trying to days now to get this website completed ... however,

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.