Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my program, when a user enters a new record and uploads a file as attachment, fileupload function is working just perfect.

It is when user clicks on Edit button in a gridview to edit a record, the attached file update function is not working.

When user clicks on Edit button to edit a record, a new View of the form, from MultiView control opens where all the fields of the record are already placed and data is rendered into those fields. On that view (vRecord) , a fileupload control is also placed. I have another button that is outside GridView called "UPDATE" for users to click and save record into SQL server after they finish editing. This button (btnUPDATE) also works to check if fileupload has a file, saves that file into sql server. I have put all the code related to uploading file in the click event of this button (btnUPDATE). I regret to inform here, that when user click btnUPDATE, it updates all other information except what contains in the fileupload control. Code to update event is as following:

C#
protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string filePath = fileupload1.PostedFile.FileName;
            string filename = Path.GetFileName(filePath);

            if (fileupload1.HasFile)
            {
                Stream fs = fileupload1.PostedFile.InputStream;
                BinaryReader br = new BinaryReader(fs);
                Byte[] bytes = br.ReadBytes((Int32)fs.Length);

            }

            using (SqlConnection con = new SqlConnection("Data Source=MEHDI-PC\\SQLEXPRESS;Initial Catalog=PIMS;Integrated Security=True"))
            {
                using (SqlCommand cmd = new SqlCommand())
                {

                    string sql = "UPDATE dbo.Documents1 SET Ref = @Ref, Subject = @Subject, Src = @Src, Dst = @Dst, Medium = @Medium, Date_Printed = @Date_Printed, Date_Received = @Date_Received, Document_Type = @Document_Type,Action_Required = @Action_Required, Due_Date = @Due_Date, Actual_Date = @Actual_Date, Content = @Content, Tag = @Tag, Issue_No = @Issue_No, Notes = @Notes, Assigned_To = @Assigned_To, Reply_Ref = @Reply_Ref, Priority = @Priority, Status = @Status, Response = @Response, Physical_File_No = @Physical_File_No, Physical_Rack_Location = @Physical_Rack_Location WHERE DocumentID = @DocumentID ";


                    cmd.Connection = con;
                    cmd.CommandText = sql;

                    cmd.Parameters.Add(new SqlParameter("@Ref", txtRef.Text));
                    cmd.Parameters.Add(new SqlParameter("@Subject", txtSubject.Text));
                    cmd.Parameters.Add(new SqlParameter("@Src", ddlSource.Text));
                    cmd.Parameters.Add(new SqlParameter("@Dst", ddlDestination.Text));
                    cmd.Parameters.Add(new SqlParameter("@Medium", ddlMedium.Text));
                    cmd.Parameters.Add(new SqlParameter("@Date_Printed", txtDatePrinted.Text == "" ? DBNull.Value : (object)txtDatePrinted.Text));
                    cmd.Parameters.Add(new SqlParameter("@Date_Received", txtDateReceived.Text == "" ? DBNull.Value : (object)txtDateReceived.Text));
                    cmd.Parameters.Add(new SqlParameter("@Document_Type", ddlDocumentType.Text));
                    cmd.Parameters.Add(new SqlParameter("@Action_Required", cbxAction.Checked));
                    cmd.Parameters.Add(new SqlParameter("@Due_Date", txtDueDate.Text == "" ? DBNull.Value : (object)txtDueDate.Text));
                    cmd.Parameters.Add(new SqlParameter("@Actual_Date", txtActualDate.Text == "" ? DBNull.Value : (object)txtActualDate.Text));
                    cmd.Parameters.Add(new SqlParameter("@Content", txtContent.Text));
                    cmd.Parameters.Add(new SqlParameter("@Tag", txtTag.Text));
                    cmd.Parameters.Add(new SqlParameter("@Issue_No", txtIssue.Text == "" ? DBNull.Value : (object)txtIssue.Text));

                    cmd.Parameters.Add(new SqlParameter("@Notes", txtNotes.Text));
                    cmd.Parameters.Add(new SqlParameter("@Assigned_To", ddlAssignedTo.Text));
                    cmd.Parameters.Add(new SqlParameter("@Reply_Ref", txtReplyRef.Text));
                    cmd.Parameters.Add(new SqlParameter("@Priority", ddlPriority.Text));
                    cmd.Parameters.Add(new SqlParameter("@Status", ddlStatus.Text));
                    cmd.Parameters.Add(new SqlParameter("@Response", ddlResponse.Text));
                    cmd.Parameters.Add(new SqlParameter("@Physical_File_No", txtPhysicalFileNo.Text));
                    cmd.Parameters.Add(new SqlParameter("@Physical_Rack_Location", txtPhysicalRackLocation.Text));

                    cmd.Parameters.Add(new SqlParameter("@DocumentID", int.Parse(lblSet.Text)));

                    con.Open();
                    cmd.ExecuteNonQuery();
                }
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }

                GridView1.EditIndex = -1;
                BindGrid();

                MultiView1.SetActiveView(vGrid1);
                lblStatus.Visible = true;
                lblStatus.Text = "Record updated successfully!";
                btnBacktoHome.Visible = true;
                clr();


            }
        }


Code for Editing event is :

C#
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {


            lblSet.Text = GridView1.Rows[e.NewEditIndex].Cells[3].Text;
            MultiView1.SetActiveView(vRecord);

            btnSave.Visible = false;
            btnBacktoHome.Visible = true;
            //this.lblMedium.Text = GridView1.Rows[e.NewEditIndex].Cells[1].Text;


            using (SqlConnection con = new SqlConnection("Data Source=MEHDI-PC\\SQLEXPRESS;Initial Catalog=PIMS;Integrated Security=True"))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    String sql = "select [DocumentID],[Ref],[Subject],[Src],[Dst],[Medium],[Date_Printed],[Date_Received],[Document_Type],[Action_Required],[Due_Date],[Actual_Date],[Content],[Tag],[Issue_No],[Attachment],[Notes],[Assigned_To],[Reply_Ref],[Priority],[Status],[Response],[Physical_File_No],[Physical_Rack_Location] from dbo.Documents1 where [DocumentId]=N'" + GridView1.Rows[e.NewEditIndex].Cells[3].Text + "'";
                    cmd.Connection = con;
                    cmd.CommandText = sql;
                    con.Open();

                    DataSet ds = new DataSet();
                    using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
                    {
                        adp.Fill(ds);

                    }


                    this.txtRef.Text = ds.Tables[0].Rows[0][1].ToString();
                    this.txtSubject.Text = ds.Tables[0].Rows[0][2].ToString();
                    this.ddlSource.Text = ds.Tables[0].Rows[0][3].ToString();
                    this.ddlDestination.Text = ds.Tables[0].Rows[0][4].ToString();
                    this.ddlMedium.Text = ds.Tables[0].Rows[0][5].ToString();
                    this.txtDatePrinted.Text = ds.Tables[0].Rows[0][6].ToString();
                    this.txtDateReceived.Text = ds.Tables[0].Rows[0][7].ToString();
                    this.ddlDocumentType.Text = ds.Tables[0].Rows[0][8].ToString();
                    this.cbxAction.Checked = ds.Tables[0].Rows[0][9].Equals(cbxAction.Checked);
                    this.txtDueDate.Text = ds.Tables[0].Rows[0][10].ToString();
                    this.txtActualDate.Text = ds.Tables[0].Rows[0][11].ToString();
                    this.txtContent.Text = ds.Tables[0].Rows[0][12].ToString();
                    this.txtTag.Text = ds.Tables[0].Rows[0][13].ToString();
                    this.txtIssue.Text = ds.Tables[0].Rows[0][14].ToString();

                    this.txtNotes.Text = ds.Tables[0].Rows[0][16].ToString();
                    this.ddlAssignedTo.Text = ds.Tables[0].Rows[0][17].ToString();
                    this.txtReplyRef.Text = ds.Tables[0].Rows[0][18].ToString();
                    this.ddlPriority.Text = ds.Tables[0].Rows[0][19].ToString();
                    this.ddlStatus.Text = ds.Tables[0].Rows[0][20].ToString();
                    this.ddlResponse.Text = ds.Tables[0].Rows[0][21].ToString();
                    this.txtPhysicalFileNo.Text = ds.Tables[0].Rows[0][22].ToString();
                    this.txtPhysicalRackLocation.Text = ds.Tables[0].Rows[0][23].ToString();

                    if (con != null)
                    {
                        con.Close();
                    }
                    btnUpdate.Visible = true;
                    btnSearch.Visible = false;
                    BindGrid();
                }
            }
        }


I must be going wrong somewhere, maybe in referring fileupload control field to SQL field or something. I am falling behind submitting this project by weeks. Please help if possible for you.Thanks in advance.
Posted
Updated 24-Sep-13 23:26pm
v2

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