c# - Must declare the scalar variable "@photo"? -
i'm trying save image in sql database in image format. error must declare scalar variable @photo
when call executenonquery cmd
.
using system; using system.io; using system.collections.generic; using system.componentmodel; using system.data; using system.data.oledb; using system.drawing; using system.drawing.imaging; using system.linq; using system.text; using system.windows.forms; namespace library { public partial class moviepagemain : form { public moviepagemain() { initializecomponent(); byte[] photo_aray; } private void picmovie_click(object sender, eventargs e) { openfiledialog1.title = "select image"; openfiledialog1.showdialog(); } private void openfiledialog1_fileok(object sender, canceleventargs e) { textpic.text = openfiledialog1.filename; picmovie.image = image.fromfile(textpic.text); } private void buttonsave_click(object sender, eventargs e) { oledbconnection con = new oledbconnection("provider=sqloledb;user id=sa;password =12345678; initial catalog=library; server=raj; trusted_connection=true;"); oledbcommand cmd = new oledbcommand("insert movie ([title],[genere],[size],[resolution],[frame],[language],[subtitle],[runtime],[imdb],[synopsis],[cast],[director],[date],[picturebox]) values ('" + texttitle.text + "', '" + textgenere.text + "','" + textsize.text + "','" + textres.text + "','" + textframe.text + "','" + textlang.text + "','" + textsub.text + "','" + texttime.text + "','" + textimdb.text + "','" + textsynop.text + "','" + textcast.text + "','" + textdirector.text + "','" + datetimepicker1.value.tostring("mm/dd/yyyy hh:mm").trim() + "',@photo)", con); conv_photo(); con.open(); int = cmd.executenonquery(); // here exception error accrued // must declare scalar variable "@photo" messagebox.show("done"); } void conv_photo() { if (picmovie.image != null) { memorystream mst = new memorystream(); picmovie.image.save(mst, imageformat.jpeg); byte[] photo_aray = new byte[mst.length]; mst.position = 0; mst.read(photo_aray, 0, photo_aray.length); oledbcommand cmd = new oledbcommand(); cmd.parameters.addwithvalue("@photo", photo_aray); } } } }
the issue caused because:
- you not using correct placeholder parameter inside command text
- you adding
@photo
parameter irrelevantoledbcommand
object
you have replace @photo
?
in command text, i.e. replace + "',@photo)"
+ "',?)"
. how specify parameter placeholders oledbcommand
.
you should pass cmd
object parameter conv_photo()
:
void conv_photo(oledbcommand cmd) { if (picmovie.image != null) { memorystream mst = new memorystream(); picmovie.image.save(mst, imageformat.jpeg); byte[] photo_aray = new byte[mst.length]; mst.position = 0; mst.read(photo_aray, 0, photo_aray.length); cmd.parameters.addwithvalue("@photo", photo_aray); } }
then invoke conv_photo
within buttonsave_click
this:
private void buttonsave_click(object sender, eventargs e) { ... conv_photo(cmd); con.open(); int = cmd.executenonquery(); // cmd setup messagebox.show("done"); }
Comments
Post a Comment