-2

I have this code that gets the value from a SQL Query and place the value in a textbox. I want to put it to another class, and access it from the main class. But my problem is, The class wont recognize the button(txtbox_ticketnum) as it is from the main class. help!

 using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        {

            using (SqlCommand com_retrieve = new SqlCommand("usp_SelectTop1Ticket", con))
            {
                com_retrieve.CommandType = CommandType.StoredProcedure;
                con.Open();
                try
                {
                    txtbox_ticketnum.Text = com_retrieve.ExecuteScalar().ToString();
                    MessageBox.Show("Ticket Has been saved. Your Ticket Number: " + com_retrieve.ExecuteScalar().ToString(), "Ticket Filed");
                }
                catch (SqlException)
                {
                    MessageBox.Show("The database has encountered an error");
                }
                catch (Exception)
                {
                    MessageBox.Show("The server has encountered an error");
                }
            }
        }
user1954418
  • 963
  • 7
  • 21
  • 29
  • 5
    Well no - which instance of the class would you expect it to use, for one thing? I would suggest you put down both SQL and GUIs for the moment, and learn the core of C# - how you refer to different objects etc. This is best done through console apps, IMO - where there's very little code to look at. Once you're confident with the basics of C#, it's easier to apply them to more complex scenarios. (Then you'll need to learn about threading or asynchrony - as you shouldn't be performing a SQL query in the UI thread...) – Jon Skeet Jul 01 '13 at 06:09
  • I would suggest you to use a separate layer (project) in Visual Studio Solution Explorer where to keep the SQL Queries intro separate classes. From start you can make them with public access, after following @JonSkeet's advice, you'll decide what kind of access will provide. By having a separate public layer for SQL, you'll reference it into your GUI layer (project with WindowForms) and you'll use it. For more info, write'me, I'll see. – mihai Jul 01 '13 at 06:25
  • exact duplicate of [Access form component from another class](http://stackoverflow.com/questions/6803970/), or medium dup of [Access class from another form](http://stackoverflow.com/questions/15696066/access-class-from-another-form) or [Accessing methods from another class](http://stackoverflow.com/questions/4090773/) or [Accessing variables from another class](http://stackoverflow.com/questions/4057843/) or [Access another class methods](http://stackoverflow.com/questions/16694449/) or ... – quetzalcoatl Jul 01 '13 at 06:40

1 Answers1

0

It seems that your button (txtbox_ticketnum) is private - that is default Visual Studio behaviour. You can either change it (set "Modifiers" property of the txtbox_ticketnum to the "public"), or add your own property (better solution)

public partial class MyForm {
  ...
  public String TicketNumText {
    get {
      return txtbox_ticketnum.Text; 
    }
    set {
      txtbox_ticketnum.Text = value; 
    }
  }

  ...

  MyForm form = new MyForm();

  ...

  con.Open();

  try
    {
      form.TicketNumText = com_retrieve.ExecuteScalar().ToString();
       ...
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215