当我单击选择按钮时,我的图像未加载到Fileupload字段 [英] When I Click On The Select Button My Image Isn't Loaded To The Fileupload Field

查看:49
本文介绍了当我单击选择按钮时,我的图像未加载到Fileupload字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击选择按钮时,所有其他字段都填充了除文件上传按钮之外的数据

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Web.UI.WebControls.Expressions;
使用 System.Data;
使用 System.Data.OleDb;
使用 System.Configuration;
使用 System.Collections;
使用 System.IO;
使用 LMCSS.P;

命名空间 Langkuk_Memorial_Sech.School.pages.setup
{
public partial class istudent:System.Web.UI.Page
{

受保护 void Page_Load( object sender,EventArgs e)
{
GridView1.AutoGenerateSelectButton = true ;
GridView1.AutoGenerateDeleteButton = false ;
GridView1.PageSize = 4 ;
showdata();


}
public void showdata()
{
string cs = ConfigurationManager.ConnectionStrings [ CONNSTRING]的ConnectionString。
OleDbConnection conn = new OleDbConnection(cs);
conn.Open();
OleDbDataAdapter adap = new OleDbDataAdapter( stuSelectAll ,conn);
adap.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
adap.Fill(ds);
GridView1.DataSource = ds.Tables [ 0 ];
GridView1.DataBind();
conn.Close();
}
public void btnReset_Click( object sender,ImageClickEventArgs e)
{
// set
txtRegno.Text = ;
txtSurname.Text = ;
txtOthernames.Text = ;
txtDOB.Text = ;
txtPhoneno.Text = ;
txtContactaddress.Text = ;
txtEmail.Text = ;
mbox1.Visible = false ;
}

受保护 void btnSave_Click( object sender,ImageClickEventArgs e)
{
// 获取imagelength
int imagelength = FileUpload1.PostedFile.ContentLength;
// 获取imagebyte

byte [] imagebyte = new byte [imagelength];
// 获取输入流
FileUpload1.PostedFile.InputStream.Read( imagebyte, 0 ,imagelength);
string cs = ConfigurationManager.ConnectionStrings [ CONNSTRING]的ConnectionString。
OleDbConnection conn = new OleDbConnection(cs);
conn.Open();
OleDbCommand cmd = new OleDbCommand( stuInsert ,conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue( @ regno,txtRegno.Text);
cmd.Parameters.AddWithValue( @ surname,txtSurname.Text);
cmd.Parameters.AddWithValue( @ othernames,txtOthernames.Text);
cmd.Parameters.AddWithValue( @ dob,txtDOB.Text);
cmd.Parameters.AddWithValue( @ genderid,ddlGender.SelectedValue);
cmd.Parameters.AddWithValue( @ religionid,ddlReligion.SelectedValue);
cmd.Parameters.AddWithValue( @ nationalityid,ddlNationlity.SelectedValue);
cmd.Parameters.AddWithValue( @ stateid,ddlState.SelectedValue);
cmd.Parameters.AddWithValue( @ lgaid,ddlLga.SelectedValue);
cmd.Parameters.AddWithValue( @ phonenumber,txtPhoneno.Text);
cmd.Parameters.AddWithValue( @ contactaddress,txtContactaddress.Text);
cmd.Parameters.AddWithValue( @ email,txtEmail.Text);
cmd.Parameters.AddWithValue( @ classid,ddlClass.SelectedValue);
cmd.Parameters.AddWithValue( @ picture,imagebyte);

int result = cmd.ExecuteNonQuery();
if (结果> 0)
{
btnReset_Click(sender,e);
mbox1.showmsg( 1 学生已成功保存);
}
else
{
bnnReset_Click(sender,e);
mbox1.showmsg( 3 学生未成功保存);
}
conn.Close();
showdata();
}
protected void GridView1_PageIndexChanging( object sender,GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
showdata();
}
protected void GridView1_SelectedIndexChanged( object sender,EventArgs e)
{

}

protected void GridView1_SelectedIndexChanged1( object sender,EventArgs e)
{
// //获取imagelength
// < span class =code-comment> int imagelength = FileUpload1.PostedFile.ContentLength;

// //获取imagebyte

// byte [] imagebyte = new byte [imagelength];
// //获取输入流
// < span class =code-comment> FileUpload1.PostedFile.InputStream.Read(imagebyte,0,imagelength);



// string cs = ConfigurationManager.ConnectionStrings [connstring]。ConnectionString;
// OleDbConnection conn = new OleDbConnection(cs);
// string query =从学生中选择图片,其中id =+ txtID.Text;
// OleDbCommand cmd = new OleDbCommand(query,conn);
// conn.Open();
// OleDbDataReader reader = cmd.ExecuteReader();
// reader.Read();
// byte [] bs =(byte []读者[图片];

txtID.Text = GridView1.SelectedRow.Cells [ 2 ]。文字;
txtRegno.Text = GridView1.SelectedRow.Cells [ 3 ]。文字;
txtSurname.Text = GridView1.SelectedRow.Cells [ 4 ]。文字;
txtOthernames.Text = GridView1.SelectedRow.Cells [ 5 ]。文字;
txtDOB.Text = GridView1.SelectedRow.Cells [ 6 ]。文字;
ddlGender.Text = GridView1.SelectedRow.Cells [ 7 ]。Text.ToString();
ddlReligion.Text = GridView1.SelectedRow.Cells [ 8 ]。Text.ToString();
ddlNationlity.Text = GridView1.SelectedRow.Cells [ 9 ]。Text.ToString();
ddlState.Text = GridView1.SelectedRow.Cells [ 10 ]。Text.ToString();
ddlLga.Text = GridView1.SelectedRow.Cells [ 11 ]。Text.ToString();
txtPhoneno.Text = GridView1.SelectedRow.Cells [ 12 ]。文字;
txtContactaddress.Text = GridView1.SelectedRow.Cells [ 13 ]。文字;
txtEmail.Text = GridView1.SelectedRow.Cells [ 14 ]。文字;
ddlClass.Text = GridView1.SelectedRow.Cells [ 15 ]。Text.ToString();

// FileUpload1.Visible = false;
// FileUpload1.FileBytes = GridView1.SelectedRow.Cells [16] .Text.ToString();
// FileUpload1.FileName.ToString()= Convert.ToByte((byte [])reader [Picture] .ToString());

// conn.Close();
}

protected void btnUPdate_Click(< span class =code-keyword> object sender,ImageClickEventArgs e)
{
// 获取imagelength
int imagelength = FileUpload1.PostedFile.ContentLengt H;
// 获取imagebyte

byte [] imagebyte = new byte [imagelength];
// 获取输入流
FileUpload1.PostedFile.InputStream.Read( imagebyte, 0 ,imagelength);
string cs = ConfigurationManager.ConnectionStrings [ CONNSTRING]的ConnectionString。
OleDbConnection conn = new OleDbConnection(cs);
conn.Open();
OleDbCommand cmd = new OleDbCommand( stuUpdate ,conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue( @ id,txtID.Text.ToString() );
cmd.Parameters.AddWithValue( @ regno,txtRegno.Text);
cmd.Parameters.AddWithValue( @ surname,txtSurname.Text);
cmd.Parameters.AddWithValue( @ othernames,txtOthernames.Text);
cmd.Parameters.AddWithValue( @ dob,txtDOB.Text);
cmd.Parameters.AddWithValue( @ genderid,ddlGender.SelectedValue);
cmd.Parameters.AddWithValue( @ religionid,ddlReligion.SelectedValue);
cmd.Parameters.AddWithValue( @ nationalityid,ddlNationlity.SelectedValue);
cmd.Parameters.AddWithValue( @ stateid,ddlState.SelectedValue);
cmd.Parameters.AddWithValue( @ lgaid,ddlLga.SelectedValue);
cmd.Parameters.AddWithValue( @ phonenumber,txtPhoneno.Text);
cmd.Parameters.AddWithValue( @ contactaddress,txtContactaddress.Text);
cmd.Parameters.AddWithValue( @ email,txtEmail.Text);
cmd.Parameters.AddWithValue( @ classid,ddlClass.SelectedValue);
cmd.Parameters.AddWithValue( @ picture,imagebyte);
int result = cmd.ExecuteNonQuery();
if (结果> 0
{
btnReset_Click(sender,e);
mbox1.showmsg( 1 学生成功更新);
}
else
{
bnnReset_Click(sender,e);
mbox1.showmsg( 3 学生未成功更新);
}
conn.Close();
showdata();
}

protected void GridView1_RowDeleting( object sender,GridViewDeleteEventArgs e)
{

}

protected void btnDelete_Click( object sender,ImageClickEventArgs e)
{
// 获取imagelength
int imagelength = FileUpload1.PostedFile.ContentLength;
// 获取imagebyte
byte [] imagebyte = new byte [imagelength];
// 获取输入流
FileUpload1.PostedFile.InputStream.Read( imagebyte, 0 ,imagelength);
string cs = ConfigurationManager.ConnectionStrings [ CONNSTRING]的ConnectionString。
OleDbConnection conn = new OleDbConnection(cs);
conn.Open();
OleDbCommand cmd = new OleDbCommand( stuDelete ,conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue( @ id,txtID.Text.ToString() );
int result = Convert.ToInt32(cmd.ExecuteNonQuery()。ToString());
if (结果> 0
{
mbox1.showmsg( 1 学生成功删除);
}
else
{
mbox1.showmsg( 3 学生未成功删除);
}
conn.Close();
}
}
}

解决方案

替换为:

 FileUpload1.PostedFile.InputStream.Read(imagebyte, 0 ,imagelength); 



with

 FileUpload1.PostedFile.InputStream.Seek( 0 ,SeekOrigin.Begin); 
FileUpload1.PostedFile.InputStream.Read(imagebyte, 0 ,imagelength);



请注意

  //   0表示偏移量缓冲区,而不是流的起始位置 
读取(imagebyte, 0 ,imagelength);


When i click on the select button all other fields are populated with data except the fileupload button

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;   
using System.Web.UI.WebControls.Expressions;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
using System.IO;
using LMCSS.P;

namespace Langkuk_Memorial_Sech.School.pages.setup
{
    public partial class istudent : System.Web.UI.Page
    {
      
        protected void Page_Load(object sender, EventArgs e)
        {
            GridView1.AutoGenerateSelectButton = true;
            GridView1.AutoGenerateDeleteButton = false;
            GridView1.PageSize = 4;
            showdata();

            
        }
        public void showdata()
        {
              string cs = ConfigurationManager.ConnectionStrings["connstring"].ConnectionString;
                OleDbConnection conn = new OleDbConnection(cs);
                conn.Open();
                OleDbDataAdapter adap = new OleDbDataAdapter("stuSelectAll", conn);
                adap.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataSet ds = new DataSet();
                adap.Fill(ds);
                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();
                conn.Close();
        }
        public void btnReset_Click(object sender, ImageClickEventArgs e)
        {
            //set
            txtRegno.Text = "";
            txtSurname.Text = "";
            txtOthernames.Text = "";
            txtDOB.Text = "";
            txtPhoneno.Text = "";
            txtContactaddress.Text = "";
            txtEmail.Text = "";
            mbox1.Visible = false;
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            //get the imagelength
            int imagelength = FileUpload1.PostedFile.ContentLength;
            //get the imagebyte
         
            byte[] imagebyte = new byte[imagelength];
            //get the input stream
            FileUpload1.PostedFile.InputStream.Read(imagebyte, 0, imagelength);
            string cs = ConfigurationManager.ConnectionStrings["connstring"].ConnectionString;
            OleDbConnection conn = new OleDbConnection(cs);
            conn.Open();
            OleDbCommand cmd = new OleDbCommand("stuInsert", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@regno", txtRegno.Text);
            cmd.Parameters.AddWithValue("@surname", txtSurname.Text);
            cmd.Parameters.AddWithValue("@othernames", txtOthernames.Text);
            cmd.Parameters.AddWithValue("@dob", txtDOB.Text);
            cmd.Parameters.AddWithValue("@genderid", ddlGender.SelectedValue);
            cmd.Parameters.AddWithValue("@religionid", ddlReligion.SelectedValue);
            cmd.Parameters.AddWithValue("@nationalityid", ddlNationlity.SelectedValue);
            cmd.Parameters.AddWithValue("@stateid", ddlState.SelectedValue);
            cmd.Parameters.AddWithValue("@lgaid", ddlLga.SelectedValue);
            cmd.Parameters.AddWithValue("@phonenumber", txtPhoneno.Text);
            cmd.Parameters.AddWithValue("@contactaddress", txtContactaddress.Text);
            cmd.Parameters.AddWithValue("@email", txtEmail.Text);
            cmd.Parameters.AddWithValue("@classid", ddlClass.SelectedValue);
            cmd.Parameters.AddWithValue("@picture", imagebyte);

            int result = cmd.ExecuteNonQuery();
            if(result>0)
            {
                btnReset_Click(sender, e);
                mbox1.showmsg(1, "student saved successfully");
            }
            else
            {
                btnReset_Click(sender, e);
                mbox1.showmsg(3, "student not-saved successfully");
            }
            conn.Close();
            showdata();
        } 
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            showdata();
        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }

        protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
        {
        //    //get the imagelength
        //    int imagelength = FileUpload1.PostedFile.ContentLength;
        //    //get the imagebyte
         
        //    byte[] imagebyte = new byte[imagelength];
        //    //get the input stream
        //    FileUpload1.PostedFile.InputStream.Read(imagebyte, 0, imagelength);


        //    string cs = ConfigurationManager.ConnectionStrings["connstring"].ConnectionString;
        //    OleDbConnection conn = new OleDbConnection(cs);
        //    string query = "select Picture from student where id = " + txtID.Text;
        //    OleDbCommand cmd = new OleDbCommand(query,conn);
        //     conn.Open();
        //     OleDbDataReader reader = cmd.ExecuteReader();
        //    reader.Read();
        //    byte[] bs = (byte[])reader["Picture"];

            txtID.Text = GridView1.SelectedRow.Cells[2].Text;
            txtRegno.Text = GridView1.SelectedRow.Cells[3].Text;
            txtSurname.Text = GridView1.SelectedRow.Cells[4].Text;
            txtOthernames.Text = GridView1.SelectedRow.Cells[5].Text;
            txtDOB.Text = GridView1.SelectedRow.Cells[6].Text;
            ddlGender.Text = GridView1.SelectedRow.Cells[7].Text.ToString();
            ddlReligion.Text = GridView1.SelectedRow.Cells[8].Text.ToString();
            ddlNationlity.Text = GridView1.SelectedRow.Cells[9].Text.ToString();
            ddlState.Text = GridView1.SelectedRow.Cells[10].Text.ToString();
            ddlLga.Text = GridView1.SelectedRow.Cells[11].Text.ToString();
            txtPhoneno.Text = GridView1.SelectedRow.Cells[12].Text;
            txtContactaddress.Text = GridView1.SelectedRow.Cells[13].Text;
            txtEmail.Text = GridView1.SelectedRow.Cells[14].Text;
            ddlClass.Text = GridView1.SelectedRow.Cells[15].Text.ToString();

            //FileUpload1.Visible = false;
           // FileUpload1.FileBytes = GridView1.SelectedRow.Cells[16].Text.ToString();
            //FileUpload1.FileName.ToString() = Convert.ToByte((byte[])reader["Picture"].ToString());

                 //conn.Close();
        }

        protected void btnUPdate_Click(object sender, ImageClickEventArgs e)
        {
            //get the imagelength
            int imagelength = FileUpload1.PostedFile.ContentLength;
            //get the imagebyte

            byte[] imagebyte = new byte[imagelength];
            //get the input stream
            FileUpload1.PostedFile.InputStream.Read(imagebyte, 0, imagelength);
            string cs = ConfigurationManager.ConnectionStrings["connstring"].ConnectionString;
            OleDbConnection conn = new OleDbConnection(cs);
            conn.Open();
            OleDbCommand cmd = new OleDbCommand("stuUpdate", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@id", txtID.Text.ToString());
            cmd.Parameters.AddWithValue("@regno", txtRegno.Text);
            cmd.Parameters.AddWithValue("@surname", txtSurname.Text);
            cmd.Parameters.AddWithValue("@othernames", txtOthernames.Text);
            cmd.Parameters.AddWithValue("@dob", txtDOB.Text);
            cmd.Parameters.AddWithValue("@genderid", ddlGender.SelectedValue);
            cmd.Parameters.AddWithValue("@religionid", ddlReligion.SelectedValue);
            cmd.Parameters.AddWithValue("@nationalityid", ddlNationlity.SelectedValue);
            cmd.Parameters.AddWithValue("@stateid", ddlState.SelectedValue);
            cmd.Parameters.AddWithValue("@lgaid", ddlLga.SelectedValue);
            cmd.Parameters.AddWithValue("@phonenumber", txtPhoneno.Text);
            cmd.Parameters.AddWithValue("@contactaddress", txtContactaddress.Text);
            cmd.Parameters.AddWithValue("@email", txtEmail.Text);
            cmd.Parameters.AddWithValue("@classid", ddlClass.SelectedValue);
            cmd.Parameters.AddWithValue("@picture", imagebyte);
            int result = cmd.ExecuteNonQuery();
            if (result > 0)
            {
                btnReset_Click(sender, e);
                mbox1.showmsg(1, "student updated successfully");
            }
            else
            {
                btnReset_Click(sender, e);
                mbox1.showmsg(3, "student not-updated successfully");
            }
            conn.Close();
            showdata();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            
        }

        protected void btnDelete_Click(object sender, ImageClickEventArgs e)
        {
            //get the imagelength
            int imagelength = FileUpload1.PostedFile.ContentLength;
            //get the imagebyte
            byte[] imagebyte = new byte[imagelength];
            //get the input stream
            FileUpload1.PostedFile.InputStream.Read(imagebyte, 0, imagelength);
            string cs = ConfigurationManager.ConnectionStrings["connstring"].ConnectionString;
            OleDbConnection conn = new OleDbConnection(cs);
            conn.Open();
            OleDbCommand cmd = new OleDbCommand("stuDelete", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@id", txtID.Text.ToString());
            int result = Convert.ToInt32(cmd.ExecuteNonQuery().ToString());
            if (result > 0)
            {
                mbox1.showmsg(1, "student deleted successfully");
            }
            else
            {
                mbox1.showmsg(3, "student not-deleted successfully");
            }
            conn.Close();
        }
    }
}

解决方案

Replace this:

FileUpload1.PostedFile.InputStream.Read(imagebyte, 0, imagelength);


with

FileUpload1.PostedFile.InputStream.Seek(0, SeekOrigin.Begin);
FileUpload1.PostedFile.InputStream.Read(imagebyte, 0, imagelength);


note that

// the 0 is for the offset in the buffer, not the start position of the stream
Read(imagebyte, 0, imagelength);


这篇关于当我单击选择按钮时,我的图像未加载到Fileupload字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆