2016-08-27 3 views
1

を与えます。データベースには、データに特別なチャーター(ソフト、管理など)があります。私はショー]ボタンをクリックしていた場合、それは次のエラーを与えている、選択した値に基づいて、レコード..「/」アプリケーションでAsp.net特殊文字私は、データベースからドロップダウンリストボックスを移入していますエラー

サーバーエラーを表示します。 's'の近くに構文が正しくありません。 文字列 ''の後ろに閉じられていない引用符。 説明:現在のWeb要求の実行中に、未処理の例外が発生しました。エラーの詳細とコード内のどこで発生したのかについては、スタックトレースを参照してください。

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near 's'. 
Unclosed quotation mark after the character string ' '. 

Source Error: 
Line 208:  SqlDataAdapter da = new SqlDataAdapter(cmd); 
Line 209:  DataSet ds = new DataSet(); 
Line 210:  da.Fill(ds); 
Line 211:  gvUsrEdit.DataSource = ds; 
Line 212:  gvUsrEdit.DataBind(); 

助けてください。

+0

は、DBからデータを取得しても、ドロップダウンに – Webruster

+0

感謝を結合するためのウルのコードをポストもあり、私は両方 –

+0

を置く私は両方を掲載しています。 –

答えて

0

この

protected void btnShow_Click(object sender, EventArgs e) 
{ 
string d1 = ddlEmpName.Text; 
string d2 = ddlQuater.Text; 
string d3 = ddlyear.Text; 
string d4 = ddlKRA.Text; 
string strquery = "select * from btaprs2 where [email protected] and [email protected] and [email protected] and [email protected] and v10='Active' "; 

if (con.State != ConnectionState.Closed) 
{ 
con.Close(); 
} 
con.Open(); 

SqlCommand cmd = new SqlCommand(strquery, con); 
cmd.Parameters.AddWithValue("@d1", d1); 
cmd.Parameters.AddWithValue("@d2", d2); 
cmd.Parameters.AddWithValue("@d3", d3); 
cmd.Parameters.AddWithValue("@d4", d4);  
SqlDataAdapter da = new SqlDataAdapter(cmd); 
DataSet ds = new DataSet(); 
da.Fill(ds); 
gvUsrEdit.DataSource = ds; 
gvUsrEdit.DataBind(); 
con.Close(); 
} 

のようなパラメータを送信しようとあなたは常にあなたのクエリでパラメータを使用する必要があります - 一緒にあなたのSQLステートメントを連結しないでください。

SQLインジェクション

SQLインジェクションは、アプリケーションのデータベース層で発生したセキュリティ 脆弱性を悪用コードインジェクションの技術です。ユーザ入力がどちらか間違ってSQL文 またはユーザー入力に埋め込まれた文字列リテラルのエスケープ文字についてフィルタリング が強く型付けされていないですし、それによって が予期せずに実行したときに 脆弱性が存在しています。これは、1つのプログラミングまたはスクリプト 言語が別の内部に埋め込まれているときはいつでも発生する可能性が 脆弱性のより一般的なクラスのインスタンスです。 SQLインジェクション攻撃SQL挿入攻撃として知られている

+0

ありがとうクリシュナ、素晴らしい! –

0
//ON page Load Binding all 4 DDL 
protected void Page_Load(object sender, EventArgs e) 
{ 
string ses1 = Session["s"].ToString(); 
if (!IsPostBack) 
{ 
string str5 = "SELECT distinct v_UserID FROM tblUsrMain WHERE v_UserID= '" + ses1 + "'"; 
da1 = new SqlDataAdapter(str5, con); 
dt1 = new DataTable(); 
da1.Fill(dt1); 
ddlEmpName.DataSource = dt1; 
ddlEmpName.DataTextField = "v_UserID"; 
ddlEmpName.DataValueField = "v_UserID"; 
ddlEmpName.DataBind(); 
} 
} 

//Current Code whcih is giving error 

protected void btnShow_Click(object sender, EventArgs e) 
{ 
string strquery = "select * from btaprs2 where vEmpID='" + ddlEmpName.Text + "' and vQuarter='" + ddlQuater.Text + "' and vyear1='" + ddlyear.Text + "' and tKRA='" +   ddlKRA.Text + "' and v10='Active'"; 
SqlCommand cmd = new SqlCommand(strquery, con); 
SqlDataAdapter da = new SqlDataAdapter(cmd); 
DataSet ds = new DataSet(); 
da.Fill(ds); 
gvUsrEdit.DataSource = ds; 
gvUsrEdit.DataBind(); 
con.Close(); 
} 


//New Code i am thinking about 
protected void btnShow_Click(object sender, EventArgs e) 
{ 
string d1 = ddlEmpName.Text; 
string d2 = ddlQuater.Text; 
string d3 = ddlyear.Text; 
string d4 = ddlKRA.Text; 
string strquery = "select * from btaprs2 where [email protected] and [email protected] and [email protected] and [email protected] and v10='Active' "; 

if (con.State != ConnectionState.Closed) 
{ 
con.Close(); 
} 
con.Open(); 

SqlCommand cmd = new SqlCommand(strquery, con); 
cmd.Parameters.AddWithValue("@d1", d1); 
cmd.Parameters.AddWithValue("@d2", d2); 
cmd.Parameters.AddWithValue("@d3", d3); 
cmd.Parameters.AddWithValue("@d4", d4);  
SqlDataAdapter da = new SqlDataAdapter(cmd); 
DataSet ds = new DataSet(); 
da.Fill(ds); 
gvUsrEdit.DataSource = ds; 
gvUsrEdit.DataBind(); 
con.Close(); 
} 
関連する問題