2017-01-13 11 views
-1

ピクチャボックスを使用してPictureとThumbの印象を使用するアプリケーションを作成していますが、私のコードでこのエラーが発生しました。スカラー変数 "@img"を宣言しなければなりません。 c#

cmd.Parameters.Clear(); 

それはParametersコレクションからすべてのパラメータをクリアします:あなたは(ms.Close()の後に)ん

con = new SqlConnection(DBHelper.Conectionstring()); 
con.Open(); 
cmd = new SqlCommand(); 
cmd.Parameters.Add("@img",SqlDbType.Image); 
cmd.Parameters.Add("@thmb", SqlDbType.Image); 
cmd.Connection = con; 
MemoryStream ms = new MemoryStream(); 
MemoryStream ms1 = new MemoryStream(); 
this.pbxaddpic.Image.Save(ms, this.pbxaddpic.Image.RawFormat); 
this.pbxthumb.Image.Save(ms, this.pbxthumb.Image.RawFormat); 
byte[] b = ms.GetBuffer(); 
byte[] t = ms.GetBuffer(); 
ms.Close(); 
cmd.Parameters.Clear(); 
cmd.Parameters.AddWithValue("@img", b); 
cmd.Parameters.AddWithValue("@thmb", t); 
int maxpartycode = getmaxpartycode(); 
cmd.CommandText = "insert into tbl_partyinfo values(" + maxpartycode + ",'" + this.txtname.Text 
     + "','" + this.txtfathername.Text + "','" + this.txtcnic.Text +"','"+this.txtaddress.Text+ "','"+this.txtcontactno.Text 
     + "','" + this.txtbusinessname.Text + "','" + this.txtbusinessaddress.Text +"','"+this.txtemail.Text+"', '"+this.txtfax.Text 
     +"',@img,@thmb)"; 
cmd.ExecuteNonQuery(); 
con.Close(); 
cmd = null; 
MessageBox.Show("Details Added") 
+0

あなたは明らかにパラメータを認識しています。なぜあなたはすべてのデータに対してそれらを使用していませんか?特にここではSQLインジェクションの脆弱性に関する実際の教科書の例を示しています(ユーザコントロールからの直接的な安全でない入力を直接受け取り、文字列連結でそれを信頼してクエリを構築する) –

+0

実際には私は学習者ではありません。 –

答えて

0

。したがって、値を追加しようとすると、パラメータは使用できず、したがってエラーになります。私はあなたがなぜパラメータをクリアする必要があるのか​​分かりませんが、実際にそれを行う必要がある場合は、値を追加する前にパラメータを追加する必要があります。同様に

cmd.Parameters.Add("@img", SqlDbType.Image); 
    cmd.Parameters.Add("@thmb", SqlDbType.Image) 
    cmd.Parameters.AddWithValue("@img", b); 
    cmd.Parameters.AddWithValue("@thmb", t); 
+0

それを試しましたが、同じエラー –

+0

どの行番号(コードスニペットのどの行)がエラーを参照していますか? – NoSaidTheCompiler

+0

cmd.Parameters.Clear()を削除/コメントしてみることはできますか? – NoSaidTheCompiler

関連する問題