Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
 public partial class New_Quote : Form
    {
        public string busnamevar = "";
public New_Quote()
        {
            InitializeComponent();
            st = "value";
            FillCombo();
        }
  void FillCombo()
        {
           
            string sql = "select cusname from new_customer where busname='"+st+"'";
     //**** string sql = "select cusname from new_customer where busname='"+busnamevar+"'";  
           
            MySqlDataReader reader; 
            try
            {
                con = new MySqlConnection(str);
                con.Open();
              MySqlCommand cmd = new MySqlCommand(sql, con);                
                   
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    string scname = reader.GetString("cusname");
                    comboBox1.Items.Add(scname);
                }
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }


If i use busname in where clause the value specified in the "st" then it is working fine but if I use the "public string variable busnamevar" then comobobox not contains the values from the table column.

I tried to copy the public string to local variable but it is not working
Posted

1 solution

does your select statement returns values if the busname is empty???
you set
C#
busnamevar = "";

and you are not assigning any name for the variable.Assign some name and see whether it works or not

Edited: As the OP mentioned that he is passing value from another form

Check this to know how to pass values between the forms

passing values between two windows forms[^]

Hope it helps

Update:

Instead of declaring the variable busnamevar as a string varibale,declare it as a property
and call the method Fillcombo once the value is set

C#
private string _busnamevar = string.Empty;
       public string Busnamevar
       {
           get
           {
               return _busnamevar;
           }
           set
           {
               _busnamevar = value;
               FillCombo();
           }
       }


The query should be
C#
string sql = "select cusname from new_customer where busname='"+Busnamevar+"'";
 
Share this answer
 
v4
Comments
SrinivasTiru 16-Jul-13 9:01am    
In another Windows Form(Form1) i assigned value for busnamevar and then redirecting to Form2. In Form2 when i assigned value to variable 'st' then it is working fine.
Naz_Firdouse 16-Jul-13 9:07am    
where you are referring the value assigned to busnamevar from Form1 in this page???
you are declaring a new variable here and not at all assigning any value to the variable. so pass the value you assigned in another form to this form and use that value
SrinivasTiru 16-Jul-13 23:28pm    
In Form1 i used like this
Form2 fr = new Form2();
fr.busnamevar=label1.text;
fr.show();


Then in Form2 i want to use that busnamevar in select query.
Naz_Firdouse 18-Jul-13 2:37am    
did u tried calling your fillCombo method in the setter of Busnamevar?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900