开发者

The statically inserted items in combobox doubles on button click for more than once

开发者 https://www.devze.com 2023-01-27 15:27 出处:网络
I have a combobox that has following items inserted public void SetOperationDropDown() { //ByDefault the selected text in the cmbOperations will be -SELECT OPERATIONS-.

I have a combobox that has following items inserted

public void SetOperationDropDown()
    {
    //ByDefault the selected text in the cmbOperations will be -SELECT OPERATIONS-.
    cmbOperations.SelectedItem = "-SELECT OPERATIONS-";

    //This is for adding four operations with value in operation dropdown
    cmbOperations.Items.Insert(0, "PrimaryKeyTables");
    cmbOperations.Items.Insert(1, "NonPrimaryKeyTables");
    cmbOperations.Items.Insert(2, "ForeignKeyTables");
    cmbOperations.Items.Insert(3, "NonForeignKeyTables");
    cmbOperations.Items.Insert(4, "UPPERCASEDTables");
    cmbOperations.Items.Insert(5, "lowercasedtables");
    }
开发者_如何学运维

But as the user clicks on the button more than once the value gets doubled or any unwanted thing happens to the value.

the button click is

private void btnConnect_Click(object sender, EventArgs e)
    {
    //Function call for validating the textboxes entry
    ValidateForm();

    //Variable to store server address
    string localHost = "192.168.10.3";

    //Variable to store userId and password of the database
    string logInDetails = "gp";

    try
        {
        //Checking for the Valid entries in textboxes if all entries are correct then call functions accordingly
        if((txtPassword.Text == logInDetails) && (txtUsername.Text == logInDetails) && (txtHost.Text == localHost))
            {

            //If connected then give this message to user
            lblMessage.Visible = true;
            lblMessage.Text = "You are connected to the SQL Server....";

            if(lblMessage.Text != string.Empty)
                {
                //Function call for binding the dropdown with all DB names 
                BindDBDropDown();

                //Function call for binding the operation names in dropdown 
                SetOperationDropDown();

                }
            }
        else
            {
            //Else give the error message to user
            lblMessage.Text = "Invalid Credentials";
            }
        }
    catch(Exception ex)
        {
        //All the exceptions are handled and written in the EventLog.
        EventLog log = new EventLog("Application");
        log.Source = "MFDBAnalyser";
        log.WriteEntry(ex.Message);
        }
    }

Can anyone help me out?


public void SetOperationDropDown()
{
if(CmbOperations.Items.Count == 0)
{
//ByDefault the selected text in the cmbOperations will be -SELECT OPERATIONS-. 
cmbOperations.SelectedItem = "-SELECT OPERATIONS-"; 
//This is for adding four operations with value in operation dropdown 
cmbOperations.Items.Insert(0, "PrimaryKeyTables"); 
cmbOperations.Items.Insert(1, "NonPrimaryKeyTables"); 
cmbOperations.Items.Insert(2, "ForeignKeyTables"); 
cmbOperations.Items.Insert(3, "NonForeignKeyTables"); 
cmbOperations.Items.Insert(4, "UPPERCASEDTables"); 
cmbOperations.Items.Insert(5, "lowercasedtables"); 


}
else
{
int? cbSelectedValue = null;
if(!string.IsNullOrEmpty(cmbOperations.SelectedValue))
cbSelectedValue = convert.toInt32(cmbOperations.SelectedValue);
}
//load your combo again
if(cbSelectedValue != null)
cmbOperations.SelectedValue = cbSelectedValue.ToString();
}

There may be small syntax errors since I didn't used VS.


only call the SetOperationDropDown() when the load of your page is not a postback

 if (!IsPostBack) {
    SetOperationDropDown();
 }


Disable the button when the user clicks it, reset the cmbOperations SelectItem to your "do nothing" value, reenable the button when you've finished processing the request.


This is tagged under WinForms, so I don't think post backs are applicable here. Looking in your btnStartAnalysis_Click method, I don't see it calling SetOperationDropDown. Try going into DEBUG mode and put a break point in SetOperationDropDown. Then click your button and see whether your break point is hit. If it is, then refer to your stack trace to see where SetOperationDropDown is being called from.

If the WinForms tag is incorrect and you're actually using WebForms/ASP.NET, then do what Stefanvds and Marcel suggested. But I think it's important to figure out where SetOperationDropDown is being incorrectly called from.

0

精彩评论

暂无评论...
验证码 换一张
取 消