0

hoow to inset into database default image in byte formate when not selecting image from fileupload.

protected void subbtn_Click(object sender, EventArgs e)
{
if (fp.FileContent == null)
{
Byte[] imgByte = new Byte["image/1.jpg".Length];
}
else
{


FileUpload img = (FileUpload)fp;


Byte[] imgByte = null;
if (img.HasFile && img.PostedFile != null)
{
//To create a PostedFile
HttpPostedFile File = fp.PostedFile;


//Create byte Array with file len
imgByte = new Byte[File.ContentLength];
//force the control to load data in array
File.InputStream.Read(imgByte, 0, File.ContentLength);


}
}
int k = bs.usrdtinsert(txtfname.Text, txtlname.Text, txtemail.Text, txtpwd.Text, txtdb.Text, txtmob.Text, countrylist.Text, statelist.Text, citylist.Text, RadioButtonList1.Text, imgByte, desiglist.Text, txtkey.Text);

Edited by happygeek: fixed formatting

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by rohand
0

hoow to inset into database default image in byte formate when not selecting image from fileupload.

 protected void subbtn_Click(object sender, EventArgs e)
    {
        if (fp.FileContent == null)
        {
            Byte[] imgByte = new Byte["image/1.jpg".Length];
        }
        else
        {

            FileUpload img = (FileUpload)fp;

            Byte[] imgByte = null;
            if (img.HasFile && img.PostedFile != null)
            {
                //To create a PostedFile
                HttpPostedFile File = fp.PostedFile;

                //Create byte Array with file len
                imgByte = new Byte[File.ContentLength];
                //force the control to load data in array
                File.InputStream.Read(imgByte, 0, File.ContentLength);

            }
        }
        int k = bs.usrdtinsert(txtfname.Text, txtlname.Text, txtemail.Text, txtpwd.Text, txtdb.Text, txtmob.Text, countrylist.Text, statelist.Text, citylist.Text, RadioButtonList1.Text, imgByte, desiglist.Text, txtkey.Text);

end quote.

I say, declare Byte[] imageByte = null before starting of If condition in your subbtn_Click event. then write below some line of code in first if part :

Byte[] imageByte = null
if (fp.FileContent == null)
{
FileInfo fInfo = new FileInfo("image/1.jpg");
imageByte = new byte[fInfo.Length];
FileStream fStream = fInfo.OpenRead();
fStream.Read(imageByte , 0, imageByte .Length);
fStream.Close();
}

here your else part remain as it is just remove Byte[] imageByte = null as we have declared it starting of If condition.

I hope it will help you.

Please mark it as solved, if your problem get solved.

Edited by mike_2000_17: Fixed formatting

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.