简体   繁体   中英

Textbox doesn't return the value

I have this class ( Class1 ) that retrieves value from a query. Now I would like to make it object oriented thats why I wrote it that way. Now the problem is, The TextBox ( TicketNum ) wont return the value and display it in the form. I dont know why. Help.

 public partial class Main : System.Web.UI.Page
{
   private String _TicketNum;

    public String TicketNum
    {
        get { return _TicketNum; }
        set { _TicketNum = value; }
    }
}

  public class Class1
{
    Main main = new Main();
    public void SelectTop1()
    {
        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
                {                     
                    main.TicketNum = 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");
                }
            }
        }
    }
}

  protected void btn_Save_Click(object sender, EventArgs e)
    {
         Class1 selectTop1 = new Class1();
            selectTop1.SelectTop1();
    }

Your string TicketNum will always be empty because you have not assigned it a value.

Fortunately strings do not connect to textboxes automatically, this would be very frustrating.

To assign the string to the text box then:

string TicketNum = txtbox.Text;

By the sounds of your question then this may not be the case, it sounds like you want a textbox to appear in your form at this point. So to do this you need to create a new textbox for your form. You can do this in the toolbox, or dynamically. I will show you how to do it dynamically:

TextBox txtbox = new TextBox();
txtbox.ID = "textBox1";
form1.Controls.Add(txtbox);

Ofcourse you would then name these textboxes to a more suited name. Then you would then go back to your string ticketnum and update it to call the name textbox ID.

Edit

I have answered what your question asked, but from looking at your comments I can see your question was of poor quality. It sounds more like you want to pass the information from a txtbox in the main class to this class. OOP makes this pretty simple. in the main class you need to store the txtbox into a string. Then you need to create a constructor in your new class, then simply pass the string parameter to your new class constructor.

If you are unsure on this, I would advise looking into Constructors , and for an over all of OOP look into this msdn article .

Edit2

Before SO becomes google, I belive you should read up on OOP again, you have not understood it. In this class you could use inheritance rather than calling the main class in this class, not sure why you would do this anyway. If you read the articles I gave you I would have thought you would have saw your errors by now and fixed this issue.

Take a look at the following:

public partial class Main : System.Web.UI.Page
{
    public String TicketNum
    {
        get { return textbox.Text; }
        set { textbox.Text = value; }
    }

    protected void btn_Save_Click(object sender, EventArgs e)
    {
        Class1 selectTop1 = new Class1();
        TicketNum = selectTop1.SelectTop1();
    }
}

public class Class1
{
    public string SelectTop1()
    {
        // assign the value from SQL to the response variable
        string response = "test";
        return response;
    }
}

Note the following:

  • The TicketNum property sets / gets the value to / from the textbox.Text property
  • There is not need (please read wrong / why would you? ) to create an instance of the Main class inside the Class1 class
  • The SelectTop1 method returns a string value, which is used in the Main class (maybe you could even make it static, so you don't need an instance of the Class1 class to call it)
  • The MessageBox class is used in WinForms, maybe you should use some JavaScript for alerts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM