1

In my program, when user wants to edit a record, and presses Edit button, a new window opens up with all the fields and record information is rendered into the respective fields giving user an option to edit any field information they require.

I have added a fileupload control to my webform fields. But I am not sure how to reference fileupload control on my new popped up window .. I am not sure if I am explaining my problem very clearly or not but I will try to explain it with the help of following code:

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
     lblSet.Text = GridView1.Rows[e.NewEditIndex].Cells[2].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[2].Text + "'";
             cmd.Connection = con;
             cmd.CommandText = sql;
             con.Open();

             //SqlDataAdapter da = new SqlDataAdapter(sql,con);
             //DataTable dt = new DataTable();
             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.fileupload1 = ds.Tables[0].Rows[0][15] ;

             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();
         }
     }
}

Basically when user clicks edit, what my code does is, reads the relevant record in the sql server and loads it from there to a new popped up window in my webform .. puts all the information in the related fields. I read online that reading varbinary data from sql and binding it into the webform is not as simple as calling text data. (maybe I am wrong, please correct me if i am). I am not really worried about fetching data from sql server into the webform, I am worried about referring to the upload control in the new window because if user add a new file in fileupload control in the popped up window and if its not referenced in my code, my program ignores the new uploaded file which is a big flaw in my code. Problem is with this line of code:

//this.fileupload1 = ds.Tables[0].Rows[0][15] ;

I have commented it out for other code to run. I am stuck with it for a whole week. Any help will be so much appreciated. Thanks in advance.

makim
  • 3,154
  • 4
  • 30
  • 49

1 Answers1

0

You can't bind a record to a file upload control, this control is for uploading files not downloading.

Have a look at this link for how to download files.

The upload control should be used to replace the existing file,when the user chooses to replace it i.e. the user will upload a new file and in your business logic you need to update this record using the existing record ID.

In your case I'd bind the ID of the attachment to a hidden field in the grid and leave the upload control alone. When the record is updated check if the file upload control has a file and then using the value of the attachment update the attachment.

Edit: From here I believe you would need to add something along the lines of:

FileUpload file = ((FileUpload)(GridView1.Rows[e.NewEditIndex].FindControl("myFileUploadControl")));

You need to give your file upload control an ID of myFileUploadControl (or whatever you want)

This question also discusses using a fileupload control in a gridview.

Community
  • 1
  • 1
Damon
  • 3,004
  • 7
  • 24
  • 28
  • Yes I know I cant bind a record to a fileupload control since its for uploading not downloading. I dont really need to download the file. I am happy with the fileupload removing the old file if user adds a new file like you said "The upload control should be used to replace the existing file". But how do I refer to fileupload control on the new popped up window? –  Sep 16 '13 at 10:08
  • as you can see, fileupload field in not referenced in the new popped up window that pops up when user clicks Edit button, which means fileupload control doesnt really exist on the new window because our code has no reference to it. so when user adds a file, it is not being read. –  Sep 16 '13 at 10:13
  • Thank you so much Damon for being the biggest help possible. I will go through the material you provided and get back to you if I have any queries. Thanks once again. appreciate it. –  Sep 16 '13 at 10:25
  • Sorry I couldnt find any help from those articles given by you. Can you please help me more with something ? Thank you. –  Sep 16 '13 at 15:16
  • The links you provided give help for rowediting which takes place within the grid. but as my code shows, I have taken the row editing into a new window and referred all the fields through the code. If someone somehow help me how to refer to a Fileupload through code, I think I can make this function work. –  Sep 18 '13 at 04:17
  • You can't do this, looking at http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.postedfile.aspx, this only has a Get and thus cannot be set by your code. I suggest you consider changing your design. – Damon Sep 18 '13 at 08:04
  • I changed the design, added a template field and an item field (LABEL) and in that Item field's editItem mode , I added a "FileUpload" .. and tried to access fileupload that way .. all the code compiles alright but doesn't produce the output. Should I paste code to you if you have time and knowledge? Thanks –  Sep 18 '13 at 09:51
  • If you put the code somewhere for me to download, I'll try and have a look a bit later. – Damon Sep 18 '13 at 10:21
  • Thanks. I will do that and provide you the link. Thanks a lot. –  Sep 18 '13 at 10:38