Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When i click on the select button all other fields are populated with data except the fileupload button
C#
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();
        }
    }
}
Posted
Updated 1-Jan-16 17:49pm
v2
Comments
[no name] 1-Jan-16 23:51pm    
Provide the code which is relevant to you only, not complete code base.

Secondly please describe when you say "Image Isn't Loaded To The Fileupload Field"
BillWoodruff 2-Jan-16 4:56am    
Have you put break-points into this code, and single-stepped to find where it first becomes obvious what you expect to find is not there ?

1 solution

Replace this:
C#
FileUpload1.PostedFile.InputStream.Read(imagebyte, 0, imagelength);

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

note that
C#
// the 0 is for the offset in the buffer, not the start position of the stream
Read(imagebyte, 0, imagelength);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900