Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi my Friends.
1.I have 3 fields : 1. year 2. month 3.price
2.I have two TextBox for year and price and a DropDownList for select a month
3.now I want to select or search a one and only one record by selecting value from DropDownList and year and want to show in GridView
now my problem is that I don't know how to set query for this
this is my code that don't work:


protected void Button5_Click(object sender, EventArgs e)
    {
        String cstr = @"Data Source=FAZI-PC\REZA_FAZI;Initial Catalog=ejra_db;Integrated Security=True";
        SqlConnection scon = new SqlConnection(cstr);
        connection.Open();
        String searchstr = String.Format("SELECT * From shakhes WHERE year,month={0}'{1}'",
                                           TextBox3.Text,DropDownList1.SelectedIndex);
                                      
        SqlCommand searchcmd = new SqlCommand(searchstr,scon);
        SqlDataReader dr = searchcmd.ExecuteReader();

        if (dr.HasRows)
        {
            dr.Read();
            TextBox4.Text=dr["price"].ToString();
            int ddl = Int32.Parse(dr["month"].ToString());
            DropDownList1.SelectedIndex = ddl;
        }
        else 
        {
            Label6.ForeColor = Color.Red;
            Label6.Text = "there is no data";
        }
        dr.Close() ;
        connection.Close();
    }



please help me
Posted
Updated 12-Oct-15 9:32am
v2
Comments
rezaeti 12-Oct-15 15:44pm    
the error :
An expression of non-boolean type specified in a context where a condition is expected, near ','.

For starters, don't do it like that!
Concatenating strings took form an SQL command is an invitation to SQL Injection, which can damage or destroy your database.
Try:

C#
string searchstr = string.Format("SELECT * From shakhes WHERE year = @YR AND month=@MN";
SqlCommand cmd = new SqlCommand(searchstr,scon);
cmd.Parameters.AddWithValue("@YR",x3.Text);
cmd.Parameters.AddWithValue("@MN",DropDownList1.SelectedIndex);
 
Share this answer
 
Comments
rezaeti 12-Oct-15 15:54pm    
hi thanks for reply
how diplay record to gridview?
The result will depend on your query.
If the same record is returned you could use the DISTINCT keyword.
If you just want the top most record use TOP keyword.

Last but not least filter your query on the primary key so you get a single record.
Of course, your user would not know the primary key unless it is a user defined field.
 
Share this answer
 

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