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 ...
私は私のクエリで行う必要がありますどのような変化を
があなたの 'SqlCommandオブジェクトのquery'ラインのように見えるの後に持っています。私の推測は、あなたが 'SqlCommand query 'をビルドする際に値を使って無効な値を返しているというあなたのクエリの1つです。あなたは正しい値を取得していることを確認するためにそれを実行し、おそらくこれらの変数を使用して別のクエリを構築する前に検証する必要があります – Prescott
**まず最初に、あなたは[SQLインジェクション](http://xkcd.com/327/)を知っていますか?それをしないでください - これまでではありません。代わりに**パラメータ化クエリ**を使用してください! –