A problem with vomiting blood: the parameter for uploading images is invalid.

Source: Internet
Author: User

C # upload the image to the database in binary format. when reading the image and displaying it in picturebox, the system prompts "invalid parameter". After searching for a long time, the system finally finds the problem:

PostCode: Modified code

Openfiledialog ofd = new openfiledialog ();
If (OFD. showdialog () = dialogresult. OK)
{
This. picturebox1.image = image. fromfile (OFD. filename );
This. picturebox1.sizemode = pictureboxsizemode. Zoom;

Filestream FS = new filestream (OFD. filename, filemode. Open, fileaccess. Read );
Byte [] buffbyte = new byte [fs. Length];
FS. Read (buffbyte, 0, (INT) fs. Length );
FS. Close ();

Stringbuilder strsql = new stringbuilder ();
Strsql. append ("insert into eiis_evidence_imgs (");
Strsql. append ("evidence_no, evidence_img, organization_id, delete_flag, create_user, create_datetime, current_img, transfer_id )");
Strsql. append ("values (");
Strsql. append ("@ evidence_no, @ evidence_img, @ organization_id, @ delete_flag, @ create_user, @ create_datetime, @ current_img, @ transfer_id )");
Strsql. append ("; select @ identity ");
Sqlparameter [] parameters = {

New sqlparameter ("@ evidence_no", sqldbtype. varchar, 50 ),
New sqlparameter ("@ evidence_img", sqldbtype. Image ),
New sqlparameter ("@ organization_id", sqldbtype. Int, 4 ),
New sqlparameter ("@ delete_flag", sqldbtype. Char, 1 ),
New sqlparameter ("@ create_user", sqldbtype. varchar, 30 ),
New sqlparameter ("@ create_datetime", sqldbtype. datetime ),
New sqlparameter ("@ current_img", sqldbtype. Char, 1 ),
New sqlparameter ("@ transfer_id", sqldbtype. INT)
};
Parameters [0]. value = Evi. evidence_no;
Parameters [1]. value = buffbyte;
Parameters [2]. value = globalclass. User. organization_id;
Parameters [3]. value = convert. toint32 (deleteenum. undelete). tostring ();
Parameters [4]. value = globalclass. User. user_no;
Parameters [5]. value = datetime. now;
Parameters [6]. value = convert. toint32 (yesorno. Yes). tostring ();
Parameters [7]. value = evitransfer. transfer_id;
Database. setbusinessconnection (globalclass. busiconnstr );
Object OBJ = database. getscalar (strsql. tostring (), parameters );
}

Code after modification:

 

openfiledialog ofd = new openfiledialog ();
If (OFD. showdialog () = dialogresult. OK)
{< br> This. picturebox1.image = image. fromfile (OFD. filename);
This. picturebox1.sizemode = pictureboxsizemode. zoom;

Filestream FS = new filestream (OFD. filename, filemode. Open, fileaccess. Read );
Byte [] buffbyte = new byte [fs. Length];
FS. Read (buffbyte, 0, (INT) fs. Length );
FS. Close ();

Stringbuilder strsql = new stringbuilder ();
Strsql. append ("insert into eiis_evidence_imgs (");
Strsql. append ("evidence_no, evidence_img, organization_id, delete_flag, create_user, create_datetime, current_img, transfer_id )");
Strsql. append ("values (");
Strsql. append ("@ evidence_no, @ evidence_img, @ organization_id, @ delete_flag, @ create_user, @ create_datetime, @ current_img, @ transfer_id )");
Strsql. append ("; select @ identity ");
Sqlparameter [] parameters = {

New sqlparameter ("@ evidence_no", sqldbtype. varchar, 50 ),
New sqlparameter ("@ evidence_img", sqldbtype. Image, 16 ),
New sqlparameter ("@ organization_id", sqldbtype. Int, 4 ),
New sqlparameter ("@ delete_flag", sqldbtype. Char, 1 ),
New sqlparameter ("@ create_user", sqldbtype. varchar, 30 ),
New sqlparameter ("@ create_datetime", sqldbtype. datetime ),
New sqlparameter ("@ current_img", sqldbtype. Char, 1 ),
New sqlparameter ("@ transfer_id", sqldbtype. INT)
};
Parameters [0]. value = Evi. evidence_no;
Parameters [1]. value = buffbyte;
Parameters [2]. value = globalclass. User. organization_id;
Parameters [3]. value = convert. toint32 (deleteenum. undelete). tostring ();
Parameters [4]. value = globalclass. User. user_no;
Parameters [5]. value = datetime. now;
Parameters [6]. value = convert. toint32 (yesorno. Yes). tostring ();
Parameters [7]. value = evitransfer. transfer_id;
Database. setbusinessconnection (globalclass. busiconnstr );
Object OBJ = database. getscalar (strsql. tostring (), parameters );
}

 

The parameter sqlparameter [] parameters is defined to write new sqlparameter ("@ evidence_img", sqldbtype. Image) as new sqlparameter ("@ evidence_img", sqldbtype. Image, 16 ),
Dizzy to death. It's really low.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.