This is a migrated thread and some comments may be shown as answers.

GridAttachmentColumn on edit

11 Answers 162 Views
Grid
This is a migrated thread and some comments may be shown as answers.
dev79
Top achievements
Rank 1
dev79 asked on 18 Jan 2011, 04:44 PM
Hello,

I noticed in the Grid / Attachment Column demo from here - http://demos.telerik.com/aspnet-ajax/grid/examples/generalfeatures/gridattachmentcolumn/defaultcs.aspx - that if I select the 'Edit' link, it requires me to attach the original file again.  But what if I only needed to update the other columns and not the actual file? Is there a way to do this?

Many thanks

11 Answers, 1 is accepted

Sort by
0
Marin
Telerik team
answered on 19 Jan 2011, 02:27 PM
Hi Ivy,

The field for the upload is empty because the path to the previously uploaded file from the client machine is not known during edit and cannot be automatically set in the field. However you can modify the example to avoid update of the download field if a new file is not entered. This way you can modify and update the other fields without necessarily specifying a file path and another file will be uploaded only when a new file is specified. Here is a code snippet on the needed modifications:

protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            //...
 
            if (e.CommandName == RadGrid.UpdateCommandName ||
                e.CommandName == RadGrid.PerformInsertCommandName)
            {
                GridEditableItem item = e.Item as GridEditableItem;
 
                if (!(item is GridEditFormInsertItem))
                {
                    fileId = (int)item.GetDataKeyValue("ID");
                }
 
                string fileName = (item.EditManager.GetColumnEditor("FileName") as GridTextBoxColumnEditor).Text;
                fileData = (item.EditManager.GetColumnEditor("AttachmentColumn") as GridAttachmentColumnEditor).UploadedFileContent;
 
                //if (fileData.Length == 0 || fileName.Trim() == string.Empty)
                //{
                //    e.Canceled = true;
                //    RadGrid1.Controls.Add(new LiteralControl("<b style='color:red;'>No file uploaded. Action canceled.</b>"));
                //}
            }
        }

protected void SqlDataSource1_Updated(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (fileData.Length > 0 || fileId>0)
            {
                UpdateInsertFileData("UPDATE [FileData] SET [BinaryData] = @BinaryData WHERE [ID] = @ID", fileId, fileData);
            }
        }


Regards,
Marin
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
dev79
Top achievements
Rank 1
answered on 19 Jan 2011, 04:17 PM
That worked, Marin.  Thanks very much.
0
Kjell
Top achievements
Rank 1
Iron
answered on 28 Aug 2013, 06:46 AM
Have the same problem and have to convert the code to VB on converter.telerik.com.
But my problem is when I update a field without uploading a new file, I get the message that the file can not be read when I click on the link to open it after the update.
Create new record works, but not to update without having to upload a new file.
0
Marin
Telerik team
answered on 30 Aug 2013, 02:28 PM
Hi,

 You should also make sure you have modified the Update command as shown in the SqlDataSource1_Updated event, this way after update the SqlDataSource will not modify the data in the column that holds the file, and it will still be available after you click the link.
To resolve the issue you simple need to ensure that the update command of the SqlDatasource does not modify the contents of the field holding the file:

Protected Sub SqlDataSource1_Updated(sender As Object, e As SqlDataSourceStatusEventArgs)
    If fileData.Length > 0 OrElse fileId > 0 Then
        UpdateInsertFileData("UPDATE [FileData] SET [BinaryData] = @BinaryData WHERE [ID] = @ID", fileId, fileData)
    End If
End Sub

Regards,
Marin
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Kjell
Top achievements
Rank 1
Iron
answered on 01 Sep 2013, 02:07 PM
I get Error Message: BC30057: Too many arguments to 'Private Function UpdateInsertFileData(command As String, fileId As Integer) As Integer'.

Private fileId As Integer
Private fileData As Byte()
 
Protected Sub SqlDataSource1_Updated(ByVal sender As Object, ByVal e As SqlDataSourceStatusEventArgs)
    If fileData.Length > 0 OrElse fileId > 0 Then
        UpdateInsertFileData("UPDATE [rapp_FileData] SET [BinaryData] = @BinaryData WHERE [ID] = @ID", fileId, fileData)
    End If
End Sub

problem with fileData......
0
Kjell
Top achievements
Rank 1
Iron
answered on 03 Sep 2013, 09:57 AM
Any solution to why it is not working when I convert it to VB?
0
Marin
Telerik team
answered on 03 Sep 2013, 11:21 AM
Hello,

 fileData is the third argument of the UpdateInsertFileData function and second parameter in the SQL query that will actually perform the update of the database. Depending on the custom logic in the procedure you may wish to use this argument or not if the user has not uploaded a new file and you do not wish to update the corresponding BinaryData field with new values.

Private Function UpdateInsertFileData(command As String, fileId As Integer, fileData As Byte()) As Integer
    Using conn As New SqlConnection(connStr)
        Using comm As New SqlCommand(command, conn)
            comm.Parameters.Add(New SqlParameter("ID", fileId))
 
            If fileData.Length > 0 Then
                'if the user has not attached a new file the SQL query will not have a BianryData parameter and it will not change the content of the uploaded file
                comm.Parameters.Add(New SqlParameter("BinaryData", fileData))
            End If
 
            conn.Open()
            Return comm.ExecuteNonQuery()
        End Using
    End Using
End Function

Regards,
Marin
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Kjell
Top achievements
Rank 1
Iron
answered on 03 Sep 2013, 12:37 PM
Now it looks to work to update a field without uploading a new file.
Still a small problem remains. When I save the edited form not collapse, its open. But the update was performed.
But I have to click cancel to collapse

This my code:
Imports Telerik.Web.UI
 
Partial Class admin_rapportartikel
    Inherits System.Web.UI.Page
 
    Private fileId As Integer
    Private fileData As Byte()
 
    '........................................
 
    Protected Sub SqlDataSource1_Inserted(ByVal sender As Object, ByVal e As SqlDataSourceStatusEventArgs)
 
        fileId = CType(e.Command.Parameters("@InsertedID").Value, Integer)
        UpdateInsertFileData("INSERT INTO [test_FileData] ([ID], [BinaryData]) VALUES (@ID, @BinaryData)", fileId, fileData)
 
    End Sub
 
    '....................................
 
    Protected Sub SqlDataSource1_Updated(ByVal sender As Object, ByVal e As SqlDataSourceStatusEventArgs)
 
        'If fileData.Length > 0 OrElse fileId > 0 Then
            UpdateInsertFileData("UPDATE [test_FileData] SET [BinaryData] = @BinaryData WHERE [ID] = @ID", fileId, fileData)
        'End If
 
    End Sub
 
    '....................................
 
    Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
        If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
            Dim editItem As GridEditableItem = TryCast(e.Item, GridEditableItem)
            Dim txtId As TextBox = DirectCast(editItem("ID").Controls(0), TextBox)
            txtId.Visible = False
        End If
 
        If (TypeOf e.Item Is GridEditableItem) AndAlso (e.Item.IsInEditMode) Then
            Dim item As GridEditableItem = DirectCast(e.Item, GridEditableItem)
            Dim txtReportYear As TextBox = DirectCast(item("ReportYear").Controls(0), TextBox)
            txtReportYear.Width = Unit.Pixel(180)
        End If
 
        If (TypeOf e.Item Is GridEditableItem) AndAlso (e.Item.IsInEditMode) Then
            Dim item As GridEditableItem = DirectCast(e.Item, GridEditableItem)
            Dim txtFileName As TextBox = DirectCast(item("FileName").Controls(0), TextBox)
            txtFileName.Width = Unit.Pixel(180)
        End If
 
        If TypeOf e.Item Is GridEditableItem AndAlso TryCast(e.Item, GridEditableItem).IsInEditMode Then
            Dim edititem As GridEditableItem = TryCast(e.Item, GridEditableItem)
            Dim ddl As RadComboBox = DirectCast(edititem("GridDropDownColumn").Controls(0), RadComboBox)
            ddl.Width = Unit.Pixel(180)
        End If
    End Sub
 
    '....................................
 
    Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs)
 
        If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
            Dim item As GridEditableItem = TryCast(e.Item, GridEditableItem)
            Dim upload As RadUpload = (TryCast(item.EditManager.GetColumnEditor("AttachmentColumn"), GridAttachmentColumnEditor)).RadUploadControl
            upload.OnClientFileSelected = "uploadFileSelected"
 
        End If
 
    End Sub
 
    '....................................
 
    Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)
 
        If e.CommandName = RadGrid.DownloadAttachmentCommandName Then
 
            Dim args As GridDownloadAttachmentCommandEventArgs = TryCast(e, GridDownloadAttachmentCommandEventArgs)
            Dim fileName As String = args.FileName
            Dim attachmentId As Integer = DirectCast(args.AttachmentKeyValues("ID"), Integer)
 
            Dim MyConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("JunisConnectionString").ConnectionString)
            Dim comm As New SqlCommand("SELECT [BinaryData] FROM [test_FileData] WHERE [ID] = @ID", MyConnection)
 
            comm.Parameters.Add(New SqlParameter("@ID", attachmentId))
 
            Dim adapter As New SqlDataAdapter(comm)
            Dim data As New DataSet()
 
            adapter.Fill(data)
 
            Dim binaryData As Byte() = DirectCast(data.Tables(0).Rows(0)("BinaryData"), Byte())
 
            Response.Clear()
            Response.ContentType = "application/octet-stream"
            Response.AddHeader("content-disposition", "attachment; filename=" + fileName)
            Response.BinaryWrite(binaryData)
            Response.[End]()
 
        End If
 
        '....................................
 
        If e.CommandName = RadGrid.UpdateCommandName OrElse e.CommandName = RadGrid.PerformInsertCommandName Then
 
            Dim item As GridEditableItem = TryCast(e.Item, GridEditableItem)
 
            If Not (TypeOf item Is GridEditFormInsertItem) Then
                fileId = DirectCast(item.GetDataKeyValue("ID"), Integer)
            End If
 
            Dim fileName As String = (TryCast(item.EditManager.GetColumnEditor("FileName"), GridTextBoxColumnEditor)).Text
 
            fileData = (TryCast(item.EditManager.GetColumnEditor("AttachmentColumn"), GridAttachmentColumnEditor)).UploadedFileContent
 
            Dim ReportYear As String = (TryCast(item.EditManager.GetColumnEditor("ReportYear"), GridTextBoxColumnEditor)).TextBoxControl.Text
 
            'If fileData.Length = 0 OrElse fileName.Trim() = String.Empty Then
                'e.Canceled = True
                'RadGrid1.Controls.Add(New LiteralControl("<b style='color:red;'>Ingen fil vald. Uppladdning avbröts..</b>"))
            'End If
 
        End If
 
    End Sub
 
    '....................................
 
    Private Function UpdateInsertFileData(command As String, fileId As Integer, fileData As Byte()) As Integer
 
        Using MyConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("JunisConnectionString").ConnectionString)
 
            Using comm As New SqlCommand(command, MyConnection)
                comm.Parameters.Add(New SqlParameter("ID", fileId))
 
                If fileData.Length > 0 Then
                    comm.Parameters.Add(New SqlParameter("BinaryData", fileData))
                End If
 
                MyConnection.Open()
 
                Return comm.ExecuteNonQuery()
            End Using
 
        End Using
 
    End Function
 
    '....................................
 
    Protected Sub Logout_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
 
        Dim CommandName As String = CType(e, System.Web.UI.WebControls.CommandEventArgs).CommandName.ToString()
 
        If CommandName = "Logout" Then
            FormsAuthentication.SignOut()
            Response.Write("<script>window.open('../default.aspx','_top');<" & Chr(47) & "script>")
        End If
 
    End Sub
 
End Class
0
Marin
Telerik team
answered on 04 Sep 2013, 10:44 AM
Hello,

 You can try disabling the automatic operations for the grid to see if this helps:

<telerik:RadGrid ID="RadGrid1" runat="server" AllowAutomaticUpdates="false" AllowAutomaticInserts="false"

Regards,
Marin
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Kjell
Top achievements
Rank 1
Iron
answered on 04 Sep 2013, 11:24 AM
Yes it helps, the edited form collapse.
But the database is not update with insert or update.....
0
Marin
Telerik team
answered on 05 Sep 2013, 06:35 AM
Hello,

 When using automatic operations in the grid (AllowAutomaticUpdates="true"), the grid automatically calls the update query / method of the relevant datasource control and closes the edit form.
The other option is manually handling the update / insert operations of the control (in this case AllowAutomaticUpdates / Inserts = "false") then in the grid's Update / Insert command event you perform the operation to the database and rebind the control (RadGrid1.Rebind()) - this will also close the edit form.
The above are the two recommended ways to save grid's data to the database and have the edit form properly closed.

Regards,
Marin
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
dev79
Top achievements
Rank 1
Answers by
Marin
Telerik team
dev79
Top achievements
Rank 1
Kjell
Top achievements
Rank 1
Iron
Share this question
or