Uploading documents to SQL with vb.net [modified]
-
Ok, I have been searching for a while on this, and just cant work it out. I have web form with a INPUT type='file' called fileToAdd, and a textbox called tbTitle, and a button called btnUp. The IDE is complaining about the line 'objStream.Read (bDocTemp, 0, iDocLength)', saying "'1-dimensional array of Byte' cannot be converted to '1-dimensional array of Char' because 'Byte' is not derived from 'Char'." Fair enough, but I just cant figure out how to get it as a byte, all the examples I have found on the net for uploading images are the same as below, but it just wont work! If I DIM bDocTemp as a CHAR, the error goes away, but it wont work at run time. Argghh! Any help will be appreciated please! Thanks
Protected Sub btnUp_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUp.Click Dim iDocLength As Integer = fileToAdd.PostedFile.ContentLength Dim sExt As String = fileToAdd.PostedFile.FileName.ToString() sExt = sExt.Substring(sExt.IndexOf(".", 0), 4) Dim objConn As New SqlConnection("Data Source=XXXXX;Initial Catalog=XXXX;Persist Security Info=True;User ID=XXXXX;Password=XXXXX") objConn.Open() Dim objCmd As New SqlCommand("spUploadFile", objConn) objCmd.CommandType = Data.CommandType.StoredProcedure Dim pTitle As New SqlParameter pTitle = objCmd.Parameters.Add("@Title", Data.SqlDbType.VarChar, 200) pTitle.Direction = Data.ParameterDirection.Output pTitle.Value = tbTitle.Text Dim pType As New SqlParameter pTitle = objCmd.Parameters.Add("@Type", Data.SqlDbType.VarChar, 3) pType.Value = sExt.ToString Dim bDocTemp(iDocLength - 1) As Byte Dim objStream As StreamReader = New StreamReader(fileToAdd.PostedFile.InputStream) objStream.Read (bDocTemp, 0, iDocLength) '<----Error here Dim pDocument As New SqlParameter pDocument = objCmd.Parameters.Add("@Document", Data.SqlDbType.Image) pDocument.Value = bDocTemp Dim pUser As New SqlParameter pUser = objCmd.Parameters.Add("@user", Data.SqlDbType.VarChar, 50) pUser.Value = Request.ServerVariables("AUTH_USER").ToString objCmd.ExecuteNonQuery() objConn.Close() End Sub
-- modified at 1:26 Monday 16th July, 2007