private void button2_Click(object sender, EventArgs e)
{
// my sql server connection
con = new SqlConnection(@"Data Source=dasranrajlui\sqlexpress;Initial Catalog=SESoriginal;Integrated Security=True");
con.Open();
// this is to save my values to sql
com = new SqlCommand(" insert into VoterRegistration (SALUTATION, NAME, SEX, ETHNICITY, MARITALSTATUS, ICNUMBER, HPNUMBER, DOB, ADDRESS, STATE, CITY, POSTCODE, VoterPic) VALUES ('"
+ SALUTATION.Text + "','"
+ NAME.Text + "','"
+ SEX.Text + "','"
+ ETHNICITY.Text + "','"
+ MARITALSTATUS.Text + "','"
+ ICNUMBER.Text + "','"
+ HPNUMBER.Text + "','"
+ DOB.Text + "','"
+ ADDRESS.Text + "','"
+ STATE.Text + "','"
+ CITY.Text + "','"
+ POSTCODE.Text + "',"
+ "@VoterPic" + ")", con);
conv_photo();
try
{
com.ExecuteNonQuery();
MessageBox.Show("Registered...");
// return back to admin page after registered
this.Hide();
AdminVoterREUP RETURNTOREUP = new AdminVoterREUP();
RETURNTOREUP.Show(); ;
}
catch (Exception EX)
{
MessageBox.Show(EX + "Not Registered");
}
finally
{
con.Close();
}
}
void conv_photo()
{
//to convernt my image
if (VOTERPIC.Image != null)
{
ms = new MemoryStream();
VOTERPIC.Image.Save(ms, ImageFormat.Jpeg);
byte[] photo_aray = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo_aray, 0, photo_aray.Length);
com.Parameters.AddWithValue("@VoterPic", photo_aray);
}
}
}
私はこのコードを実行すると、私はエラーを取得する:挿入値
System.Data.SqlClient.SqlExeption (0x80131904): Must declare the scalar variable "@VoterPic".
voterPic
は私の画像を保存するためにSQL Serverでの私のコラム名で、私はまたVOTERPICとしての私のPictureBox
命名。
誰も私を助けてくれますか?
SQLインジェクションとフォーマットエラーを避けるためにパラメータを使用します。 – LarsTech