Thanks in advance...
I am using asp.net 2.0 c# , the project that I am working on is uploading a document and saving it into database (SQL Server 2005).
The error message that i am getting when executing the query in the InsertFile function and the UpdateFile function is:" No mapping exists from object type System .Web.HttpinputStream to a known Managed provider native type"
I am using layering technique so i have a Class library that contain the functions that connect to the database and do all the transactions.Function:
(by the way how can access upload file from layering )
Expand|Select|Wrap|Line Numbers
- public int InsertFile(String FileName, Stream upload)
- {
- SqlConnection conn = new SqlConnection(ConnectionString);
- conn.Open();
- SqlCommand cmd = new SqlCommand("INSERT INTO Files( FileName,FileBytes ) VALUES ( @FileName ,@upload)", conn);
- cmd.CommandType = CommandType.Text;
- cmd.Parameters.Add(new SqlParameter("@FileName", FileName));cmd.Parameters.Add(new SqlParameter("@upload", upload));
- cmd.ExecuteNonQuery();
- //get the autonumber of the record that was just inserted
- cmd.CommandText = "SELECT @@identity";int id = int.Parse(cmd.ExecuteScalar().ToString());
- conn.Close();
- return id;
- }
- public void UpdateFile(Int32 Id, Stream upload)
- {
- int bufferLen = 8040;
- BinaryReader br = new BinaryReader(upload);
- byte[] chunk = br.ReadBytes(bufferLen);
- SqlConnection conn = new SqlConnection(ConnectionString);
- SqlCommand cmd = new SqlCommand("UPDATE Files SET FileBytes=@Buffer WHERE Id = @Id", conn);cmd.CommandType = CommandType.Text;
- cmd.Parameters.Add("@Buffer", SqlDbType.VarBinary, bufferLen).Value = chunk;
- cmd.Parameters.AddWithValue("@Id", Id);
- conn.Open();
- cmd.ExecuteNonQuery();
- SqlCommand cmdAppend = new SqlCommand("UPDATE Files SET FileBytes.WRITE(@Buffer, NULL,0) WHERE Id=@Id", conn);
- cmdAppend.Parameters.AddWithValue("@Id", Id);
- cmdAppend.Parameters.Add("@Buffer", SqlDbType.VarBinary, bufferLen);
- chunk = br.ReadBytes(bufferLen);
- conn.Close();
- while (chunk.Length > 0)
- {
- cmdAppend.Parameters["@Buffer"].Value = chunk;
- cmdAppend.ExecuteNonQuery();
- chunk = br.ReadBytes(bufferLen);
- }
- br.Close();
- }
- And where i am using these function is in the .cs file "Upload.aspx.cs"
- Function:
- private void LoadData()
- {
- rptFiles.DataSource = FilesAccess.Instance.GetAll();
- rptFiles.DataBind();
- }
- protected void btnAdd_Click(object sender, EventArgs e)
- {
- if (upFile.FileName != string.Empty)
- {
- if (CheckFileType(upFile.FileName))
- {
- AddFile(upFile.FileName.ToString(), upFile.FileContent);
- LoadData();
- }
- }
- }
- private bool CheckFileType(string fileName)
- {
- return Path.GetExtension(fileName).ToLower() == ".doc";
- }
- private void AddFile(String fileName,Stream upload)
- {
- int newFileId = FilesAccess.Instance.InsertFile(fileName, upload);FilesAccess.Instance.UpdateFile(newFileId, upFile.FileContent);
- LoadData();
- }