Hai im new in c# i have doubt with my code. Actually what im trying to do here is when i upload an attachment on my screen level, so this function will save it in my DB table. But what happen now is i dont know which value coming as nvarchar.
This is my C# code
public ActionResult SaveFile(HttpPostedFileBase file, int senderId, int pictureNumber, DocumentUpload docUpload)
{
if (file != null && file.ContentLength > 0)
{
byte[] fileData;
using (var binaryReader = new BinaryReader(file.InputStream))
{
fileData = binaryReader.ReadBytes(file.ContentLength);
}
switch (pictureNumber)
{
case 1:
docUpload.Picture1 = fileData;
break;
case 2:
docUpload.Picture2 = fileData;
break;
case 3:
docUpload.Picture3 = fileData;
break;
case 4:
docUpload.Picture4 = fileData;
break;
case 5:
docUpload.Picture5 = fileData;
break;
}
var result = _storedProcedureService.GetWithRawSql<int>(
"exec sp_UpdateDocumentUpload @p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7",
senderId,
docUpload.Picture1 ?? (object)DBNull.Value,
docUpload.Picture2 ?? (object)DBNull.Value,
docUpload.Picture3 ?? (object)DBNull.Value,
docUpload.Picture4 ?? (object)DBNull.Value,
docUpload.Picture5 ?? (object)DBNull.Value,
Convert.ToInt32(Session["EntityId"]),
DateTime.Now
);
return Json(new { success = true });
}
return Json(new { success = false, message = "No file provided" });
}
And this is my store procedure
ALTER PROCEDURE [dbo].[sp_UpdateDocumentUpload]
@SenderId INT,
@Picture1 VARBINARY(MAX) = NULL,
@Picture2 VARBINARY(MAX) = NULL,
@Picture3 VARBINARY(MAX) = NULL,
@Picture4 VARBINARY(MAX) = NULL,
@Picture5 VARBINARY(MAX) = NULL,
@CreatedBy INT = null,
@CreationDate DATETIME = null
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS (SELECT * FROM DocumentUpload WHERE SenderId = @SenderId)
BEGIN
UPDATE DocumentUpload
SET
Picture1 = COALESCE(@Picture1, Picture1),
Picture2 = COALESCE(@Picture2, Picture2),
Picture3 = COALESCE(@Picture3, Picture3),
Picture4 = COALESCE(@Picture4, Picture4),
Picture5 = COALESCE(@Picture5, Picture5),
CreatedBy = @CreatedBy,
CreationDate = @CreationDate
WHERE SenderId = @SenderId;
END
ELSE
BEGIN
INSERT INTO DocumentUpload (SenderId, Picture1, Picture2, Picture3, Picture4, Picture5, CreatedBy, CreationDate)
VALUES (@SenderId, @Picture1, @Picture2, @Picture3, @Picture4, @Picture5, @CreatedBy, @CreationDate);
END
END
In my module also i did the same datatype
public class DocumentUpload : Entity
{
public int SenderId { get; set; }
public byte[] Picture1 { get; set; }
public byte[] Picture2 { get; set; }
public byte[] Picture3 { get; set; }
public byte[] Picture4 { get; set; }
public byte[] Picture5 { get; set; }
public Nullable<int> CreatedBy { get; set; }
public Nullable<System.DateTime> CreationDate { get; set; }
}
}
New contributor
Ikmal Shahfiz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2