2011-04-15 15 views
0
protected void Button1_Click(object sender, EventArgs e) 
{ 
    SqlConnection myConnection = new SqlConnection("Data Source=DELL-PC\\SQLEXPRESS;Initial Catalog=eclass;Persist Security Info=True;integrated security = true"); 
    myConnection.Open(); 
    string key = txtsearchkey.Text.ToString(); 

    SqlCommand q1 = new SqlCommand("select cat_id from category where cat_name='" + (ddsearchcat.SelectedItem.ToString() + "'"), myConnection); 
    string cat = q1.ExecuteScalar().ToString(); 

    SqlCommand q2 = new SqlCommand("select subcat_id from subcategory where subcat_name= '" + (ddsearchsubcat.SelectedItem.ToString() + "'"), myConnection); 
    string subcat = q2.ExecuteScalar().ToString(); 

    SqlCommand q3 = new SqlCommand("select adid from adType where adtype= '" + (ddsearchtype.SelectedItem.ToString()) + "'", myConnection); 
    string adtype = q3.ExecuteScalar().ToString(); 

    String date = ddsearchdays.SelectedItem.ToString(); 

    if (chkAdimg.Checked) 
    { 
     if (chkAdVideo.Checked) 
     { 
      SqlCommand query = new SqlCommand("select title,ad_description from postad where ad_description like " + txtsearchkey + " and category_id=" + cat + " and subcategory_id=" + subcat + " and ad_id=" + adtype + " and video is not null and img_id is not null and adType INNER JOIN adType AS adType_1 ON adType.adid = adType_1.adid CROSS JOIN category CROSS JOIN subcategory CROSS JOIN userdetails", myConnection);   

      DataSet ds = new DataSet(); 
      SqlDataAdapter ad = new SqlDataAdapter(query); 
      ad.Fill(ds); 
      foreach (DataRow dr in ds.Tables[0].Rows) 
      { 
        Response.Write(dr[0].ToString()); 
      } 
     } 
    } 
} 

このクエリは、「近く、SQLクエリの問題

非論理型 条件が期待されているコンテキストで指定 の発現を言って、私に問題を与えているINNER ...

私は私のクエリで行う必要がありますどのような変化を

+0

があなたの 'SqlCommandオブジェクトのquery'ラインのように見えるの後に持っています。私の推測は、あなたが 'SqlCommand query 'をビルドする際に値を使って無効な値を返しているというあなたのクエリの1つです。あなたは正しい値を取得していることを確認するためにそれを実行し、おそらくこれらの変数を使用して別のクエリを構築する前に検証する必要があります – Prescott

+1

**まず最初に、あなたは[SQLインジェクション](http://xkcd.com/327/)を知っていますか?それをしないでください - これまでではありません。代わりに**パラメータ化クエリ**を使用してください! –

答えて

1
select title,ad_description from postad where ad_description like " + txtsearchkey + " and category_id=" + cat + " and subcategory_id=" + subcat + " and ad_id=" + adtype + " and video is not null and img_id is not null and adType INNER JOIN adType AS adType_1 ON adType.adid = adType_1.adid CROSS JOIN category CROSS JOIN subcategory CROSS JOIN userdetails", myConnection);  

ここで、条件の後に内部結合を使用していますか?

私は、これは私はあなたがあなたの加入は、WHERE句の前に

...and adType INNER JOIN adType... 

を行わなければならないんだ、ではないに言及、あなたが本当にあるべき場所、それがここにあると仮定し、右

select title,ad_description 
from postad 
INNER JOIN adType AS adType_1 ON adType.adid = adType_1.adid 
CROSS JOIN category 
CROSS JOIN subcategory 
CROSS JOIN userdetails 
where ad_description like " + txtsearchkey + " 
    and category_id=" + cat + " 
    and subcategory_id=" + subcat + " 
    and ad_id=" + adtype + " 
    and video is not null 
    and img_id is not null 
+0

adType INNER JOIN adType AS adType.adid = atType_1.adid – shweta

+1

where節で内部結合を使用しようとしていますか? where節の前にそれを使ってみてください – Nighil

+0

もう一度:これは**パラメータ化されたクエリ**を利用すべきです - そしてなぜそれらの 'CROSS JOIN's?それは狂気のような応答の行の量を爆破するつもりです! –

1

かもしれないと思いますSQLインジェクションのようなものを避けるためにプレーンテキストの代わりに値のパラメータを使用し、あなたはLIKEをしたい値におそらく%の値を必要としますが、私は脱出します...

+0

はいいいえ.....私はそれを忘れてしまった – shweta

0

varch ar列には、値を囲む引用符を付ける必要があります。例えば

where ad_description like " + txtsearchkey + " and 

また

where ad_description like '" + txtsearchkey + "' and 

img_id is not null and adType INNER JOIN 

なければならないが

img_id is not null INNER JOIN 

すなわちを有するべきであるADTYPEを置き忘れているように見えます。

これは動的SQLを作成する良い方法ではありません。 SQLインジェクションにさらされるだけでなく、維持することもほとんど不可能です。

0

あなたがあなたのWHERE句の後に加わり、それがWHERE句の前にする必要があり、犯人はおそらくですFROM

select 
    title, 
    ad_description 
from postad 
INNER JOIN adType AS adType_1 ON adType.adid = adType_1.adid 
CROSS JOIN category 
CROSS JOIN subcategory 
CROSS JOIN userdetails 
where ad_description like " + txtsearchkey + " 
and category_id=" + cat + " 
and subcategory_id=" + subcat + " 
and ad_id=" + adtype + " 
and video is not null 
and img_id is not null 
and adType "