I wanted to add files uploaded to datatable then show it on gridview.When i upload files, nothing happens and gridview is not updated too.I have the code that uploads the file to sql database but now i only want to upload the files to a datatable and show it on the gridview then later the end user can decide whether to insert the files into the sql server database. I am not really sure why i am unable to upload the files, i also tried displaying the variables that i am trying to insert into the datable on a label but nothing happens too. It would be nice if someone could help me with this.
Frontend code
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />
<hr />
<asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
RowStyle-BackColor="#A1DCF2" ShowHeaderWhenEmpty="True" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
AutoGenerateColumns="false" Height="338px" Width="449px">
<Columns>
<asp:BoundField DataField="filename" HeaderText="File Name"/>
</Columns>
</asp:GridView>
</div>
</form>
Backend
Private dt As DataTable
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
'BindGrid()
fill()
Session("table") = dt
Else
' this is a page post back - re-load the active table into our
' forms level dt.
dt = Session("table")
End If
End Sub
Protected Sub fill()
dt = New DataTable()
Dim dc As DataColumn = New DataColumn("file")
dc.DataType = System.Type.[GetType]("System.Byte[]")
dt.Columns.Add(dc)
dt.Columns.Add("filename", GetType(String))
dt.Columns.Add("filetype", GetType(String))
End Sub
Protected Sub Upload(sender As Object, e As EventArgs) Handles btnUpload.Click
Dim filename As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
Dim contentType As String = FileUpload1.PostedFile.ContentType
Dim fs As Stream = FileUpload1.PostedFile.InputStream
Dim br As New BinaryReader(fs)
Dim bytes As Byte() = br.ReadBytes(DirectCast(fs.Length, Long))
Dim R As DataRow = dt.NewRow()
R("file") = bytes
R("filename") = filename
R("filetype") = contentType
dt.Rows.Add(R)
GridView1.DataSource = dt
GridView1.DataBind()
Response.Redirect(Request.Url.AbsoluteUri)
Label1.Text = contentType
End Sub
question from:
https://stackoverflow.com/questions/65559688/unable-to-upload-file-to-datatable-and-display-on-the-gridviewnothing-happens-w 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…