Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to work with data base in C# with sql compact 3.5 but in each form one of query that related with on table is execute i click on button and see the correct result butif i change the condition and click on button the query execute with the the old condition and i see the old result it peer that the sqlcashed the old data
any one can help me???
this my code:
C#
 private DataTable select(string con)
        {

            DataTable k = new System.Data.DataTable();
            try
            {

                string str = "Data Source=C:\\Documents and Settings\\almas\\Desktop\\BazarKhodro\\khodro.sdf";
                Qconnection.ConnectionString = str;
                Qcommand.Connection = Qconnection;
                Qcommand.Parameters.AddWithValue("@type", comboBox1 .Text );
                Qcommand.Parameters.AddWithValue("@name", textBox3.Text);
                Qcommand.Parameters.AddWithValue("@lname", textBox4.Text);
                string commandText = " select kharid.date, moshtari.name, moshtari.lname, kharid.type, kharid.modelfrom, kharid.modelto, kharid.tip, kharid.karkardfrom, kharid.karkardto, kharid.color, kharid.pricefrom, kharid.priceto , kharid.parvande FROM kharid INNER JOIN moshtari ON kharid.mid = moshtari.id where "+con;

                Qcommand.CommandText = commandText;
                Qcommand.CommandType = CommandType.Text;
                SqlCeDataAdapter a = new SqlCeDataAdapter();
                a.SelectCommand = Qcommand;
             a.Fill(k);
                Qconnection.Open();
                Qconnection.Close();
                return k;

            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
                return k;
            }
        }

private void button1_Click(object sender, EventArgs e)
        {
            if (textBox3.Text == "" || textBox4.Text == "")
                MessageBox.Show("لطفا نام و نام خانوادگي را وارد نماييد");
            else
            {

                if (radioButton9.Checked == true)
                {
                    string u = "moshtari.name like '%'+@name+'%' and moshtari.lname like '%'+@lname+'%' ";
                   dataGridView1.DataSource= select(u);
                }
                else if (radioButton8.Checked == true)
                {
                    string u = "moshtari.name like '%'+@name+'%' and moshtari.lnamename like '%'+@lname+'%' and kharid.parvande=" + "'فعال'";
                    dataGridView1.DataSource = select(u);
                }
                else
                {
                    string u = "moshtari.name like '%'+@name+'%' and moshtari.lnamename like '%'+@lname+'%' and kharid.parvande!=" + "'فعال'";
                    dataGridView1.DataSource = select(u);
                }
            }
        }
Posted
Updated 28-Nov-12 18:15pm
v2

1 solution

Hi,

Update your code as below.

C#
private DataTable select(string con)
{ 
   DataTable k = new System.Data.DataTable();
   try
   { 
      string str = "Data Source=C:\\Documents and Settings\\almas\\Desktop\\BazarKhodro\\khodro.sdf";
      Qconnection.ConnectionString = str;
      Qcommand.Connection = Qconnection;
      Qcommand.Parameters.AddWithValue("@type", comboBox1 .Text );
      Qcommand.Parameters.AddWithValue("@name", textBox3.Text);
      Qcommand.Parameters.AddWithValue("@lname", textBox4.Text);
      string commandText = " select kharid.date, moshtari.name, moshtari.lname, kharid.type, kharid.modelfrom, kharid.modelto, kharid.tip, kharid.karkardfrom, kharid.karkardto, kharid.color, kharid.pricefrom, kharid.priceto , kharid.parvande FROM kharid INNER JOIN moshtari ON kharid.mid = moshtari.id where "+con;
      Qconnection.Open();
      Qcommand.CommandText = commandText;
      Qcommand.CommandType = CommandType.Text;
      SqlCeDataAdapter a = new SqlCeDataAdapter(Qcommand);      
      a.Fill(k);                
      Qconnection.Close();
   }
   catch (Exception ex)
   { 
      throw new Exception(ex.Message);                
   }
   return k;
}
 
private void button1_Click(object sender, EventArgs e)
{
    string u="";
    if (textBox3.Text == "" || textBox4.Text == "")
        MessageBox.Show("لطفا نام و نام خانوادگي را وارد نماييد");
    else
    { 
        if (radioButton9.Checked == true)
        {
            u = " moshtari.name like '%'+@name+'%' and moshtari.lname like '%'+@lname+'%' ";           
        }
        else if (radioButton8.Checked == true)
        {
           u = "moshtari.name like '%'+@name+'%' and moshtari.lnamename like '%'+@lname+'%' and kharid.parvande=" + "'فعال'";
        }
        else
        {
           u = "moshtari.name like '%'+@name+'%' and moshtari.lnamename like '%'+@lname+'%' and kharid.parvande!=" + "'فعال'";          
        }
   }
   dataGridView1.DataSource= select(u);
   dataGridView1.DataBind();
}


Hope this will resolve your problem.
I think you are facing the problem because you are not binding the gridview.
 
Share this answer
 
v2
Comments
f.sarikhani 29-Nov-12 1:47am    
dataGridView1.DataBind();
dose not work and i exam the
.DataBindings();
but show this error:
C:\Documents and Settings\almas\Desktop\BazarKhodro\BazarKhodro\jkharid.cs(174,35): error CS1955: Non-invocable member 'System.Windows.
Forms.Control.DataBindings' cannot be used like a method.
Mohd. Mukhtar 29-Nov-12 2:27am    
I think you are using windows form. So try with
dataGridView1.Grid.DataSource= select(u);
or
dataGridView1.reftesh();
f.sarikhani 29-Nov-12 3:36am    
with this command also dont solve my problem
Mohd. Mukhtar 29-Nov-12 4:29am    
What value are you setting from comboBox1 .Text as I am not able to see any variable.

Comment the below line in your code
Qcommand.Parameters.AddWithValue("@type", comboBox1 .Text ); and then try. and let me know what result you are getting from database.
f.sarikhani 1-Dec-12 0:16am    
in run time the type select from combobox and the query must execute with this type in first time this do ok but for other time the form must closed and reopened

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